[DUG] [computing]XE2 string conversion question

John Bird johnkbird at paradise.net.nz
Tue Jan 17 21:27:02 NZDT 2012


Its the other way around - assigning an AnsiChar to a Char (WideChar) - I 
thought this should be OK.   In this case I expected maybe a warning, but 
there should be no loss of data, and hence not an error.

Also in defining a record with mixed types I had thought there is no 
alternative except to use sized strings - correct me if I am wrong, because 
I would be happy to lose the limiting sizes!

So a question - is if valid to declare a record like:

type
  TJUFRec = record
    JUFno:integer;
    JUFName:string;
    JUFHeds:string;
    JUFDesc:string;
  end;

or do I have to use sizes like

type
  TJUFRec = record
    JUFno:integer;
    JUFName:string[20];
    JUFHeds:string[10];
    JUFDesc:string[60];
  end;

If I can remove the sizes it would be a most convenient and elegant way to 
do this!

John Bird

-----Original Message----- 
From: Stefan Mueller
Sent: Tuesday, January 17, 2012 8:16 PM
To: 'NZ Borland Developers Group - Delphi List'
Subject: Re: [DUG] [computing]XE2 string conversion question

"string[1];" always reserves 256 bytes (the max size of a shortstring is 255
characters + 1byte for length) and is in fact an ansistring

So assigning a char(which is really a widechar in Delphi XE2) to an
ansistring/ansichar won't work without losing some information. I kind of
expected the compiler to return a warning about implicit type conversion
rather than a compilation error, but then again shortstrings are a legacy of
Delphi 1.0 (16 bit applications)  and can't expect things to be supported
forever. There is no point in using shortstrings anymore nowadays - just
replace all your shortstrings with normal strings in your code.


{$IFDEF UNICODE} Is automatically defined since delphi 2009 - it's a flag to
say that strings are now widestrings.


Kind Regards,
Stefan Mueller
_______________________
R&D Manager
ORCL Toolbox LLP, Japan
http://www.orcl-toolbox.com


-----Original Message-----
From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of John Bird
Sent: Tuesday, January 17, 2012 3:29 PM
To: ping_delphilist at 3days.co.nz; NZ Borland Developers Group - Delphi List
Subject: [DUG] [computing]XE2 string conversion question

I am tidying up some library stuff for XE2....

Assigning a AnsiChar to Char does not compile in this case below

Background - from what I have read not all variables declared string are
compiled as string in XE2 - for instance any sized strings as in the record
below are take to be AnsiString.

(I am also wondering if this applies to Dataset string fields, which are
often declared with a length)

To save me time in testing and experimenting and googling, which conversions
do work implicitly by assignment, and which do not?

Anyone who has already been there and done that suggest best practice for
this case?

type
  TJUFRec = record
    JUFno:integer;
    JUFName:string[20];
    JUFHeds:string[10];
    JUFPrompt:string[30];
    JUFArrNo:integer;
    JUFArrSz:integer;
    JUFType1:string[1];        //<===Ansistring
    JUFNB:integer;
    JUFType2:string[10];
    JUFSB:integer;
    JUFDecpl:integer;
    JUFUpc:integer;
    JUFMin:int64;
    JUFMax:int64;
    JUFReq:integer;
    JUFEdtName: string[25];
    JUFEdit:integer;
    JUFShow:integer;
    JUFRemake:integer;
    JUFDesc:string[60];
  end;

var
lJUFType1:char;

begin
//
  lJUFType1:=aJUFRec.JUFType1[1];        //<======== does not compile

error is [DCC Error]  E2010 Incompatible types: 'Char' and 'AnsiChar'



John Bird
JBCL
Contact:
johnkbird at paradise.net.nz
jbclnz at xtra.co.nz
(027 4844528)
http://www.jbcl.co.nz
http://jbclnz.googlepages.com



_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at listserver.123.net.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at listserver.123.net.nz with 
Subject: unsubscribe 



More information about the Delphi mailing list