Recent

Author Topic: [SOLVED] Variant issue  (Read 2722 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1266
[SOLVED] Variant issue
« on: July 16, 2018, 10:32:26 am »
Can anybody confirm followings?
I think all the "commented out" commands must be executed, but not. Are these natural result from variant type?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   v1, v2: variant;
  4.   ts : string;
  5.  
  6. procedure DisplayIntValue(iv: integer);
  7. begin
  8.   ShowMessage(Format('Value of int const = %d', [iv]));
  9. end;
  10.  
  11. procedure DisplayIntVar(var iv: integer);
  12. begin
  13.   ShowMessage(Format('Value of int var = %d', [iv]));
  14. end;
  15.  
  16. procedure DisplayVarValue(vv: Variant);
  17. begin
  18.   // ShowMessage(Format('Value of variant const (int format) = %d', [vv])); // runtime error
  19.   ShowMessage(Format('Value of variant const (str format) = %s', [vv]));
  20. end;
  21.  
  22. procedure DisplayVarVar(var vv: Variant);
  23. begin
  24.   // ShowMessage(Format('Value of variant var (int format) = %d', [vv]));  // runtime error
  25.   ShowMessage(Format('Value of variant var (str format) = %s', [vv]));
  26. end;
  27.  
  28.  
  29. begin
  30.   v1 := 352;  v2 := '352';
  31.   // ts := 'Number is ' + v1;  ShowMessage(ts);  // EVariant Error
  32.   // ts := concat ('Number is', v1);  ShowMessage(ts);  // EVariantError
  33.  
  34.   ts := 'Number is ' + v2;   ShowMessage(ts);
  35.   ts := concat ('Number is ', v2);   ShowMessage(ts);
  36.  
  37.   DisplayIntValue(v2);
  38.   // DisplayIntVar(v2);   // Compile error
  39.   DisplayVarValue(v2);
  40.   DisplayVarVar(v2);
  41.  
  42.   DisplayIntValue(v1);
  43.   // DisplayIntVar(v1);    // compile error
  44.   DisplayVarValue(v1);
  45.   DisplayVarVar(v1);
  46.  
  47.  
  48. end;                
  49.  
« Last Edit: July 20, 2018, 10:21:31 am by egsuh »

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Variant issue
« Reply #1 on: July 16, 2018, 10:58:18 am »
If you want integer output in your message, simply cast the variant:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   v1: variant;
  4. begin
  5.   v1 := 5;
  6.   ShowMessage(Format('Value of int = %d', [integer(v1)]));
  7. end;
keep it simple

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Variant issue
« Reply #2 on: July 16, 2018, 02:22:50 pm »
@munair:
Well, that's a hard cast.
But I can solve part of it like this:
Code: Pascal  [Select][+][-]
  1. procedure DisplayVarValue(vv: Variant);
  2. begin
  3.   ShowMessage(Format('Value of variant const (int format) = %d', [vv.AsInteger])); // no runtime error
  4. end;
  5.  
  6. procedure DisplayVarVar(var vv: Variant);
  7. begin
  8.   ShowMessage(Format('Value of variant var (int format) = %d', [vv.AsInteger]));  // no runtime error
  9. end;

Variants are mired with trouble and very slow. Do you need them?
« Last Edit: July 16, 2018, 02:26:01 pm by Thaddy »
Specialize a type, not a var.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Variant issue
« Reply #3 on: July 16, 2018, 03:34:11 pm »
@munair:
Well, that's a hard cast.
Yes it is. Your solution may be more elegant and in line with using variants, but the result is the same. :D Anyway I agree, avoid using variants. There's no programming job that cannot be done without them. They may seem convenient at times, but it isn't worth the trouble. I learned it the hard way (performance) in the old days of VisualBasic 6.
keep it simple

egsuh

  • Hero Member
  • *****
  • Posts: 1266
Re: Variant issue
« Reply #4 on: July 20, 2018, 10:21:13 am »
Well... If I have to typecast in anyway, it is meaningless.  Just to remove the need to write StrToInt & IntToStr functions.

 

TinyPortal © 2005-2018