Customizing toString()
By sascha, September 8, 2008 – 2:15 am -When writing classes for a framework I usually put a custom toString method into important classes so that debugging becomes easier. Normally they would go like something along the lines of:
override public function toString():String { return "[ClassName]"; }
… Sometimes adding properties to the returned String that give back information about the class, e.g.
override public function toString():String { return "[ImageClass, size=" + _size + "]"; }
But writing them rigid like that is a disadvantage when you decide later to refactor class names. Admittedly it’s also not a very elegant way so I got the idea to take the class name that is returned by getQualifiedClassName(). The only problem is that getQualifiedClassName not only provides the type name but also the whole package String of the class. Regular Expressions to the rescue! After twiddling around with them for a while (I’m by no means a RegExp expert) I got my toString method into the shape that I desired:
override public function toString():String { return "[" + getQualifiedClassName(this).match("[^:]*$")[0] + ", size=" + _size + "]"; }
This way it matches the String returned by getQualifiedClassName with the Regular Expression [^:]*$ which checks from the right end for an arbitrary text up to the first occurring colon, but without including the colon. Taking the first element of the Array returned by match() and you got what you need!
A nice way of using this is when writing abstract classes that contain the toString method and any subclass can use that toString method without the need to override it … that is of course unless you want ot add other output information.
Featured
FEAT (Freelancer’s Estimation Assistance Tool)
FEAT (abbreviation for Freelancer’s Estimation Assistance Tool) is a tool to help freelancers calculate hourly rates and project pricing estimates. It is inspired by a very similar calculation PDF sheet which was created by Lauren of creativecurio.com and by some other useful freelancer pricing resources on the net.
The tool uses the same calculations like the ones from the links mentioned above plus it stores your values so you don’t have to enter them again every time. Version 1.0 features hourly rate calculation, a project pricing wizard and an option to change the visual theme of the tool. It also resides nicely on your desktop or wherever you put it and is there whenever you need to make an estimation.
Continue Reading »16Bit-Mercenary Mess Up
December 13, 2003 – 1:07 pmDownload: Custom VEB Missions and Campaign for Jagged Alliance: Deadly Games My big move has been delayed for a short time! I will leave here at the end of December and ...
Game Music Oracle
December 5, 2003 – 12:11 pmfinally found an excellent sorted database site about all sorts of game music! on www.mirsoft.info is a well worked out database containing info on all kinds of video games ...
What happened to the Hired Guns?
October 11, 2003 – 11:07 amAbout four years ago there was a game in development that I highly anticipated but that never actually got released. The now disbanded software company Psygnosis was about to ...
The cool days of the Fembots
October 10, 2003 – 11:02 amRecently I'm tending to shun most of what runs nowadays on TV! While today in the twothousands all kinds of TV series theme seems to be only about sex and interpersonal ...
Camouflage Sleep
September 30, 2003 – 10:55 amCamoArt, a german company is manufacturing bed blankets and other stuff with various camo patterns. A pretty nice idea when thinking about that good looking blankets are a rare ...
From Endorian Creature Hunts and Mon Calamarian Captivators
September 7, 2003 – 10:05 amReview: Star Wars Galaxies (PC)First of all I'm not that far yet and played this game now for one week so you will not get any too detailed information about ...
MMORPGwhat??
August 28, 2003 – 9:11 amToday my copy of Star Wars Galaxies arrived and I was more than enthusiastic to push the CD in and run it up. I have to admit that I'm a ...
The anachronism of Assembler coding
August 22, 2003 – 8:42 amI've been selling and buying antique computer stuff on Ebay and also managed to get an old Commodore C64G! The G model was the one with the brown bread ...
When 16 colors were enough
August 9, 2003 – 8:31 amDownload: C64 Tools Disk images "SMS Tools 1 & 2" After finally having the change to access some of my over ten years old C64 floppy disks relics with the XE1541 ...
Diary Of A Day In The Life Of An Insane
July 22, 2003 – 8:19 amBecause the weather is so good now and all people seem to went into holiday and scenes of empty and lethargic streets are regular, a friend of mine and ...
| Filed under »