Question

Under Delphi 2010 (and probably under D2009 also) the default string type is UnicodeString.

However if we declare...

const
 s  :string = 'Test';
 ss :string[4] = 'Test';

... then the first string s if declared as UnicodeString, but the second one ss is declared as AnsiString!

We can check this: SizeOf(s[1]); will return size 2 and SizeOf(ss[1]); will return size 1.

If I declare...

var
  s  :string;
  ss :string[4];

... than I want that ss is also UnicodeString type.

  1. How can I tell to Delphi 2010 that both strings should be UnicodeString type?
  2. How else can I declare that ss holds four WideChars? The compiler will not accept the type declarations WideString[4] or UnicodeString[4].
  3. What is the purpose of two different compiler declarations for the same type name: string?
Was it helpful?

Solution

The answer to this lies in the fact that string[n], which is a ShortString, is now considered a legacy type. Embarcadero took the decision not to convert ShortString to have support for Unicode. Since the long string was introduced, if my memory serves correctly, in Delphi 2, that seems a reasonable decision to me.

If you really want fixed length arrays of WideChar then you can simply declare array [1..n] of char.

OTHER TIPS

  1. You can't, using string[4] as the type. Declaring it that way automatically makes it a ShortString.

  2. Declare it as an array of Char instead, which will make it an array of 4 WideChars.

  3. Because a string[4] makes it a string containing 4 characters. However, since WideChars can be more than one byte in size, this would be a) wrong, and b) confusing. ShortStrings are still around for backward compatibility, and are automatically AnsiStrings because they consist of [x] one byte chars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top