How to avoid "Implicit string cast"

The stable tester's forum for ZeosLib 7.0.x series

Report problems concerning our Delphi 2009+ version and new Zeoslib 7.0 features here.
Post Reply
AndreasAtMBS
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 08.03.2013, 14:30

How to avoid "Implicit string cast"

Post by AndreasAtMBS »

I just migrated from Delphi 7 to XE3 and am testing the database (Using ver. 7.0.3). I have a TZConnection with the following parameters set:

AutoEncodeStrings := true;
ClientCodePage := 850;
ControlsCodePage := cGET_ACP;

My app works mostly with string type AnsiString(850).

So, I have a variable str: string850;

When I do the following:
query.SQL.Text := str;
the compiler complains with the warning "implicit string cast from string850 to string".

Does this mean:
1) The compiler is converting from ansistring(850) to Unicode and Zeos is converting back to ansistring(850)?
2) The compiler is converting to unicode and Zeos assumes it is ansistring(850)?
3) Other?

How would I do this correctly? Set up zeos, post and read?

Regards,
Andreas
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1918
Joined: 17.01.2011, 14:17

Post by marsupilami »

Hello Andreas,

in your case I suggest to just use regular Strings in your Delphi application. Zeos will do all the necessary conversions for you. Just set ClientCodePage := 850;
Also, depending on your RDBMS, you could ask the RDBMS to do the necessary conversions by asking the RDBMS to deliver all Data using UTF8 or UTF16.

If you just want to get rid of the warning you could do something like:
query.SQL.Text := string(str);
This way you do en explicit type cast and the compiler silently assumes this is exactly what you want.
The warning has nothing to do with Zeos. It is a pure Delphi warning. Most probably they want you to know that you change the character set in your application and also that this kind of operation (converting from one character set to another one) is quite expensive.

Best regards,

Jan
AndreasAtMBS
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 08.03.2013, 14:30

Post by AndreasAtMBS »

Thanks for the reply Jan.

Actually I don´t want to get rid of the message, but not need the message. When I receive the message it means that the compiler will convert my ansi850 string to unicode and send this to Zeos which will convert it back to ansi850. Since Zeos knows that I will be storing the data in ansi850 I just thought there might be a way to send it directly to Zeos without all the conversions.

Andreas
AndreasAtMBS
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 08.03.2013, 14:30

Post by AndreasAtMBS »

Maybe I'm missing something or maybe my assumptions are wrong.

Lets say I have a database that is ansi (8-bit encoded) and a variable of type fDepartment: ansistring. When I have the following statement:
fDepartment := q.FieldByName('Department').AsString;
I get the compiler message:
W1058 Implicit string cast with potential data loss from 'string' to 'AnsiString'.
If I change the statement to:
fDepartment := AnsiString(q.FieldByName('Department').AsString);
I no longer receive the message.

My assumption is (and please correct me if I am wrong) that in both cases the data in the database is converted from ansi to unicode since AsString is unicode, and then from unicode back to ansi. This is what I am trying to avoid.

If the conversions do not exist, and this is why delphi thinks there will be data loss, then I will happily do the typecast.

Andreas
marcov
Senior Boarder
Senior Boarder
Posts: 95
Joined: 24.06.2010, 09:17

Post by marcov »

A string simply supports full UTF16 unicode.

The average ansistring does not, so the compiler is right that there _could_ be dataloss.
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1918
Joined: 17.01.2011, 14:17

Post by marsupilami »

Hello AndreasAtMBS,

Delphi is doing the typecast and so the conversion in both cases. The first time it issues a warning because you do an implicit conversion and conversions from unicode to ansi might cause data loss.
The second time it doesn't issue a warning because you do en explicit typecast and thus the compiler thinks you know that there might be data loss and you don't need to be warned about that.
Again - the conversions ANSI -> Unicode -> ANSI are done anyway.

I don't know where you get your Data from but I recommend you to convert it to unicode as soon as possible, work with the data using unicode and then to let ZEOS or your RDBMS do the conversion to the Format the database needs.

Best regards,

Jan
Post Reply