Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Crear "Array of Const" programáticamente (https://www.clubdelphi.com/foros/showthread.php?t=80572)

dec 30-06-2006 14:20:18

Crear "Array of Const" programáticamente
 
Hay funciones que toman parámetros del tipo "array of const". Quisiera saber como hacer una función de este tipo y sobre todo como crear un "array of const" mediante código. ¡¡No consigo declarar una variable como "array of const"!!

El siguiente código muestra como hacer ambas cosas.

Código Delphi [-]
function MakeStr(const Args: array of const): string;
const
 BoolChars: array[Boolean] of Char = ('F', 'T');
var
 I: Integer;
begin
 Result := '';
 for I := 0 to High(Args) do begin
   with Args[i] do
     case VType of
       vtInteger:    Result := Result + IntToStr(VInteger);
       vtBoolean:    Result := Result + BoolChars[VBoolean];
       vtChar:       Result := Result + VChar;
       vtExtended:   Result := Result + FloatToStr(VExtended^);
       vtString:     Result := Result + VString^;
       vtPChar:      Result := Result + VPChar;
       vtObject:     Result := Result + VObject.ClassName;
       vtClass:      Result := Result + VClass.ClassName;
       vtAnsiString: Result := Result + string(VAnsiString);
       vtCurrency:   Result := Result + CurrToStr(VCurrency^);
       vtVariant:    Result := Result + string(VVariant^);
     end;
   REsult := Result + ' '
 end;
end;

var
  ElArray: array[ 0..2 ] of TVarRec;
  Cadena: ShortString = 'Hola';

procedure TForm1.Button1Click(Sender: TObject);
begin
  ElArray[ 0 ].VType := vtInteger;
  ElArray[ 0 ].vInteger := 0;
  ElArray[ 1 ].VType := vtBoolean;
  ElArray[ 1 ].vBoolean := False;
  ElArray[ 2 ].VType := vtString;
  ElArray[ 2 ].vString := @Cadena;
  Caption := MakeStr( ElArray );
end;


La franja horaria es GMT +2. Ahora son las 20:12:28.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi