Declare const array of variant

3

When I need to use const of array I usually do this:

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

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

Example:

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

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

    
asked by anonymous 25.05.2018 / 17:20

2 answers

5
If you have the types and the data will be structured, it is best to get away from variant , as you do not want to use variable, indicate "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;
    
25.05.2018 / 21:15
3

I would use a record and then create an Array from 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 from modifying to an Array.

    
25.05.2018 / 19:14