Avoiding implicit cast warning using AsAnsiString?

  • In converting our old app from WPT5 with D2007 over to WPT8 with Delphi Rio 10.3.1, I'd like to resolve this warning during an assembly of multiple WPRichText data from a database into a single WPRIchText "report". Ignoring the warnings produces a good overall result, but just don't like warnings... too much noise in the build messages

    W1057 Implicit string cast from 'AnsiString' to 'string'

    when using statements like:

    WPRichText1.SelectionAsString := WPRichText3.AsANSIString('WPTOOLS');

    What is your best recommendation to avoid this warning? Do an explicit cast? I did try changing it to the following but it might not give me the same overall result (keep reading):

    WPRichText_ASTopic.SelectionAsString := WPRichText3.AsString;

    I also get the inverse warning of:

    W1058 Implicit string cast with potential data loss from 'string' to 'AnsiString' when doing a mailmerge:

    Contents.StringValue := WPRichText4.AsString;

    and changing it to the following might not produce the same overall result (keep reading):

    Contents.StringValue := WPRichText4.AsAnsiString;

    gain, ignoring the warnings produces a good "report", but have not investigated what goes wrong doing the alternate assignments as mentioned above, it's just the end "report" is not built correctly. It might be one or perhaps both of the alternate assignments causing the bad result, have not yet narrowed it down.

    So to save time I thought I'd just post to you and the community and ask for the best recommendations to avoid these warnings.

    Thanks,

    Eric

    • Offizieller Beitrag

    (unicode) strings are not ideal to store the contents of a TWPRichText in either RTF or WPTOOLS format.

    That format is per-se 8 bit, this means each high byte in the unicodes will be 0. Problems can show up when an implicit code page translation takes place. This can happen with embedded binary data, such as images in RTF format.

    AsANSIString creates an ANSIString and the warning if you assign it to a String is correct.

    Contents.StringValue := WPRichText4.AsAnsiString('ANSI')

    should work ok.

  • When I changed

    Contents.StringValue := WPRichText4.AsString;

    to

    Contents.StringValue := WPRichText4.AsAnsiString('WPTOOLS');

    The warnings were eliminated and the formatting looked good. However, when I changed

    Contents.StringValue := WPRichText4.AsAnsiString('WPTOOLS')

    to

    Contents.StringValue := WPRichText4.AsAnsiString('ANSI')

    The result had the formatting all screwed up. What could that mean? Do I store the WPRichText data into the database as ANSI or as Unicode? Currently, I seem to be storing it into a Blob as ANSI even though I save the WPRichText into a string variable using WPRichText.AsString and subsequently write the string variable to a blob field. This might stem from the fact that this is data that came from an XML file containing WPRichText from WPT5 and imported into WPT8.

  • Yes, this is in a OnMailMergeGetText event.

    I guess I don't know what I need, I just want to be sure I'm dealing with Unicode correctly with WPTools or if I even have to worry about it. I just want to make sure I'm loading and saving WPRichText correctly to/from a database. It seems the data being saved is single byte ANSI. It all seems to work but I'm questioning whether this might not be correct and should be saved into the database as Unicode.