MarkShark 27 Posted 7 hours ago Hi All, I was doing some work with database parameters containing a binary coded decimal and ran into an odd issue with variants. It appears that converting a variant containing a TBcd to a string works fine, but converting and concatenating to another string does not. Any thoughts? Thanks! (I'm using Delphi 12.3 (with the latest patch) but have not tested on earlier versions to see if it's version specific.) procedure TForm1.Button1Click(Sender: TObject); // Example of issue. Uses System.Variants and Data.FMTBcd var V: Variant; S: String; Bcd: TBcd; begin Bcd := IntegerToBcd(23); VarFMTBcdCreate(V, Bcd); // This assignment works. S := V; // This concatenation fails (Delphi 12.3) // Project VarTestVcl.exe raised exception class EBcdException with message // 'Testing: is not a valid BCD value'. S := 'Testing: ' + V; end; Easy to work around, but seemed an odd issue! Share this post Link to post
PeterBelow 251 Posted 5 hours ago 1 hour ago, MarkShark said: S := 'Testing: ' + V; The error message indicates that the compiler is not evaluating the right-hand side expression as you expect. Instead of converting V to a string and concatenating the result to the string literal it is trying to convert the literal to a variant containing a TBcd, adding the two numbers, and then convert the result to a string. If you have a masochistic streak put a breakpoint on the statement, run to it, call up the disassembly view and F7 through the generated code (debug dcus need to be enabled). I would not call that a bug, just unexpected behaviour. But as you know, in programming the compiler is always right... 🙂 1 Share this post Link to post
MarkShark 27 Posted 5 hours ago (edited) Peter you are 100% correct! S := '1' + V; works and returns 24 (both if V has a bcd, or if it just contains an integer.) It looks like this is just my own misinterpretation of how concatenating variants works. I always thought the conversion took into account the type of the first "item" in the concatenation. And agreed on the bug thing, I always go with "Issue" until someone else confirms things (or I learn something new, which is always appreciated!) Edited 5 hours ago by MarkShark Share this post Link to post
Remy Lebeau 1577 Posted 2 hours ago 3 hours ago, MarkShark said: It looks like this is just my own misinterpretation of how concatenating variants works. I always thought the conversion took into account the type of the first "item" in the concatenation. It does, but in this case, since the operation involves a native type (string) on the left and a custom user type (TBcd) on the right, the operation gives the custom type an opportunity to decide whether to cast the left side to the custom type, which it does in this situation, and then the failure occurs during that cast from string to TBcd. 1 Share this post Link to post