Declare const array of Variant

Asked

Viewed 1,095 times

3

When I need to use array const I usually do so:

    var
      Campos : array [0..2,0..1] of string = (('campoa','AAA'),
                                              ('campob','BBB'),
                                              ('campoc','CCC'));

I would like to declare the above const as of Variant.

Example:

  var
    Campos : array [0..2,0..1] of variant = (('campoa',ftString),
                                             ('campob',ftInteger),
                                             ('campoc',ftDate));

Would any of you have any idea how I should proceed?

2 answers

5


If you have the types and the data will be structured, the ideal is to escape from the Variant, as you do not want to use variable, index "const array of record":

type   
   TCampos = record
      CampoA: string;
      CampoB: Integer;
      CampoC: Double;
   end;

const
   cCampos: array[0..2] of TCampos = (
      (CampoA: 'campoa'; CampoB: 1; CampoC: 1.2),
      (CampoA: 'campob'; CampoB: 2; CampoC: 56.9),
      (CampoA: 'campoc'; CampoB: 3; CampoC: 32)
   );

var
   i: Integer;
begin
   for i := 0 to 2 do
   begin
      ShowMessage(Format('CampoA: %s, CampoB: %d, CampoC: %2f',
         [cCampos[i].CampoA, cCampos[i].CampoB, cCampos[i].CampoC]));
   end;
end;
  • Good morning @Melissa Thanks for your attention. Cool your tip. Junior Moreira had soiled using a TRECORD.. but I didn’t think to use it the way you showed it. I think it will work.. I’ll encode it here to see how it looks.. Thanks) Thanks :)

  • Good afternoon.. It worked perfectly for the proper purposes .. thank you :)

3

I would use a record and then create an Array of this record.

Something like:

var
  i      : Integer;
  Campos : Array of TCampos;
begin
  SetLength(Campos, 10);

  for i := Low(Campos) to High(Campos) do
  begin
    Campos[i].CampoA := 'Aluno';
    Campos[i].CampoB := 1;
    Campos[i].CampoC := Now;
  end;
end;

Here I created a simple Array of a Record, nothing prevents you to modify to an Array.

  • 1

    good morning @Júnior Moreira I could do it that way too, however every time I used the function CONST would have to mount the array of Tcampos and then read it again... "a lot" processing.. Bag?? With the CONST type VARIANT I only read it for the necessary purposes.. understands me?

  • In the present day "processing", even if the Array has 1000 positions it will be 1/10 blink of an eye to go through. Testing on an "AMD K62 500" the result is the same, something like 3/10 blink, understood?

  • Good morning @Junior Moreira. What I meant by "PROCESSING" was WORK :). I would have to LOAD the array every time I used it.. But as our colleague MELISSA has demonstrated, I think the solution I need will be better.. more. I’m coding here to see the final result. Thanks again for the attention :) Thanks even :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.