Ver Mensaje Individual
  #1  
Antiguo 30-06-2006
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Reputación: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
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;
Responder Con Cita