ZEROBASEDSTRINGS, Just Don’t

Ack!  I cannot believe that it has been 6 months since my last post!  I apologize for the long silence but I have been juggling 3 major upgrades (the RiverSoftAVG Charting Component Suite and 2 others I am not yet ready to blog about) and it has swamped my time completely.

This is a small blog post about ZEROBASEDSTRINGS, in my opinion a truly moronic change Embarcadero introduced in XE3 for mobile.  I know I have said that I feel sometimes like Embarcadero is out to get me, but here is a case where Embarcadero seems to be out to get everyone. 🙂  Jeroen provides an excellent little blog post about it and I recommend you read that first to understand the issue.

However, I would go farther than Jeroen.  My advice, never use ZEROBASEDSTRINGS. Even if you have only started using Object Pascal now and have no experience with 1 based strings.  There are millions of lines of Delphi code and examples out there, most of it based on the traditional 1-based strings.  You will use some of that code, if you haven’t already.  You will copy and paste some of that code.  Mixing and matching 0 based and 1 based strings is a recipe for disaster.  Eventually, if you do mobile, this change will bite you in the a**.  And it will probably be a very subtle bug that will take way too long to track down (I know I wasted a day on it 🙁 ).  The worst part is that your mobile app will work perfectly fine on desktop, where you probably do most of your testing.  It is only when you move to mobile that strange things will start happening.

The first thing I recommend you do whenever you create a new unit is add your company/copyright header and add a {$IFDEF NEXTGEN}{$ZEROBASEDSTRINGS OFF}{$ENDIF} right below it.  Alternatively, as a component developer, I have to support multiple Delphi versions.  As such, I have a RSDefines.inc file (defining things like DELPHIXE7ORHIGHER, etc) which is included in every unit.  In that unit, I have the following lines:

{$IFDEF NEXTGEN}
 {$ZEROBASEDSTRINGS OFF}
{$ENDIF}

You may want to do the same thing, which gets applied no matter which version of Delphi and which compiler (mobile, desktop, etc).

By just adopting this habit of always prefacing every unit with ZEROBASEDSTRINGS OFF, you never have to worry about whether you are using 0-based or 1-based strings.  You won’t have to surround any more of your code with IFDEFs or Lows/High as detailed in Jeroen’s post.

Of course, this is my opinion.  But, when it comes to ZEROBASEDSTRINGS, my advice is JUST DON’T.  Friends Don’t Let Friends Use Zero Based Strings 🙂 (in Delphi at least).  It will save you a lot of time and heartache later.

Happy CodeSmithing!

 

Leave a Reply

Your email address will not be published. Required fields are marked *