Board index » delphi » Variant Array to Variant?

Variant Array to Variant?

Here's what I'm trying to do:

Create a procedure...

  Procedure Xyz(data : Array of Const) ;
  Var
    V : Variant;
  begin
    V := VarArrayOf(Data);  // or something - this doesn't work
    // ...other code...
    doSomething(V);     // expecting a Variant
  end;

..so .I can write...

  Xyz([1.5,'thing',-8]);

...and send this data to doSomething.

What would be the "correct" approach?

Thanks,

Paul

 

Re:Variant Array to Variant?


<<Paul Johnson:
  Procedure Xyz(data : Array of Const) ;

Quote

Two problems: first, open array parameters can't be
accessed as entire arrays within a routine, you can only
access elements individually. Secondly, the word variant in
'variant open array parameters' is misleading -  it has
nothing to do with the variant data type. But if you change
your procedure to

  Procedure Xyz(data : Array of Variant) ;

you could do something like this

var
  ArrV: OleVariant;
  i: integer;
begin
  ArrV :=  
    VarArrayCreate([Low(Data), High(Data)], varVariant);
  for i := Low(Data) to High(Data) do
    ArrV[i] := Data[i];
  ...
  DoSomethingWith(ArrV);

--
Deborah Pate (TeamB)
http://delphi-jedi.org
Sorry, no email please.

Re:Variant Array to Variant?


Quote
<d.p...@cableinet.co.not-this-bit.uk> wrote:
><<Paul Johnson:
>  Procedure Xyz(data : Array of Const) ;

>you could do something like this

>var
>  ArrV: OleVariant;
>  i: integer;
>begin
>  ArrV :=  
>    VarArrayCreate([Low(Data), High(Data)], varVariant);
>  for i := Low(Data) to High(Data) do
>    ArrV[i] := Data[i];
>  ...
>  DoSomethingWith(ArrV);

I want to use this routine for debugging purposes, so there will be a
variable amount of data of varying data types in the calling
routine(s), which may be scattered throughout various parts of the
program.

I think I have found my solution, which is:

 Procedure XYZ(data : Variant);
 begin
   if VarIsArray(data)
     then ...
     else ...;
  end;

  ... then in calling routine(s)...

  XYZ(VarArrayOf(['Problem1',FloatValue,IntValue,BoolValue,Time]));
  XYZ(VarArrayOf([Problem2',Thing,Other]));
  XYZ(VarArrayOf(['I think I'll have:',Beans AND Franks]));

This seems to work great, and allows me to use Excel as a big debug
window.  Seems silly, perhaps, but I am developing a control system
and this allows me to look at large arrays of realtime data and
perform data analysis off-line.

Thanks for your many tips.

Paul

Re:Variant Array to Variant?


<<Paul Johnson:
This seems to work great, and allows me to use Excel as a
big debug window.  Seems silly, perhap
Quote

Not at all, I can't think of a better use for Excel. :)

--
Deborah Pate (TeamB)
http://delphi-jedi.org
Sorry, no email please.

Other Threads