Recent

Author Topic: [SOLVED] Sorry about Variant  (Read 2290 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1820
[SOLVED] Sorry about Variant
« on: August 01, 2018, 03:58:05 am »
I don't like to use Variant type, but sometimes this is convenient type in web programming and when I use TStringList. In TStringList, I can save like

AStringList.Values['id'] := V; 

V is Variant type, but is integer value here.


Also assignments like

I := V;  // I is an integer-type variable, V is a variant type variable

works fine.

But 

ShowMessageFmt('The value is %d', [V]);

raises error. Well specifying type of variant isn't a big deal, like:

ShowMessageFmt('The value is %d', [Integer(V)]);


but shouldn't variants be interpreted as "expected" type?  It's not different from using StrToInt function, etc.




« Last Edit: August 01, 2018, 01:13:11 pm by egsuh »

mse

  • Sr. Member
  • ****
  • Posts: 286
Re: Sorry about Variant
« Reply #1 on: August 01, 2018, 07:51:12 am »
ShowMessageFmt is defined as
Code: Pascal  [Select][+][-]
  1. procedure ShowMessageFmt(const aMsg: string; Params: array of const);
  2.  
-> the compiler can not know what types are expected in "Params" parameter.

Thaddy

  • Hero Member
  • *****
  • Posts: 19468
  • Glad to be alive.
Re: Sorry about Variant
« Reply #2 on: August 01, 2018, 08:25:58 am »
Well, I see the point, since this works:
Code: Pascal  [Select][+][-]
  1. var v:variant;
  2. begin
  3.   v:=100;
  4.   writeln(v);
  5. end.
Anyway, variants are a last resort and very slow.
Any "programmer" that knows only one programming language is not a programmer

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Sorry about Variant
« Reply #3 on: August 01, 2018, 08:46:39 am »
This isn't much more complicated
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils, Classes;
  5.  
  6. var
  7.   S: TStringList;
  8.   ints: array of Integer;
  9.   i: Integer;
  10. begin
  11.   SetLength( ints, 10 );
  12.   for i:= 0 to 9 do
  13.     ints[ i ]:= i;
  14.   S:= TStringList.Create;
  15.   for i:= low( ints ) to high( ints ) do begin
  16.     S.Append( ints[ i ].ToString );
  17.     WriteLn( S[ i ]);
  18.   end;
  19.  
  20.   //---
  21.   SetLength( ints, S.Count );
  22.   for i:= 0 to S.Count - 1 do begin
  23.     ints[ i ]:= S[ i ].ToInteger;
  24.     WriteLn( ints[ i ]);
  25.   end;
  26. end.
  27.              

However you can always do this:
Code: Pascal  [Select][+][-]
  1. WriteStr( SomeString, v );
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Thaddy

  • Hero Member
  • *****
  • Posts: 19468
  • Glad to be alive.
Re: Sorry about Variant
« Reply #4 on: August 01, 2018, 08:54:51 am »
Yo. If writeln works, writestr also works...indeed. 8-)
Any "programmer" that knows only one programming language is not a programmer

egsuh

  • Hero Member
  • *****
  • Posts: 1820
Re: Sorry about Variant
« Reply #5 on: August 01, 2018, 09:31:34 am »
Does this mean that the type-conversion of variant is done at compilation, not at runtime?

 

TinyPortal © 2005-2018