Ver Mensaje Individual
  #5  
Antiguo 23-01-2014
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 38
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola geolife.
Cita:
Empezado por geolife Ver Mensaje
¿Se podría hacer esto creando una clase con objetos tipo variable y luego recorrer todos los objetos de la clase de inicio a fin?
Si claro. Se complica un poco debido a que el compilador convierte los nombres de variables en direcciones de memoria, por lo que no es posible acceder a ellas por su nombre en tiempo de ejecución.

Aunque se podría hacer con TList o [i]TObjectList] usando un puntero a record, pero me parece mas simple usar TStrings ya que podemos aprovechar sus propiedades vectoriales String y Objects para almacenar el nombre de variable y el valor respectivamente.

Como para darte una pauta, te pongo un ejemplo no muy pulido aunque operativo :
Código Delphi [-]
unit Unit2;

interface

uses Classes, SysUtils;

type
  TListDT = class(TStringList)
  public
    procedure InitList(VecArray: array of string; const InitValue: TDateTime);
    function GetVarValue(const VarName: string): TDateTime;
    procedure ChangeVarValue(const VarName: string; Value: TDateTime);
  end;

implementation

type
  TCDateTime = class(TObject)
    Value : TDateTime;
  end;

procedure TListDT.InitList(VecArray: array of string; const InitValue: TDateTime);
var
  i: Integer;
  dt: TCDateTime;
begin
  if not Assigned(Self) then
    raise Exception.Create('La lista aún no fue creada.');
  Clear;
  for i:= Low(VecArray) to High(VecArray) do
  begin
    dt := TCDateTime.Create;
    dt.Value := InitValue;
    AddObject(VecArray[i], TObject(dt));
  end;
end;

function TListDT.GetVarValue(const VarName: string): TDateTime;
var
  ix: Integer;
begin
  if not Assigned(Self) then
    raise Exception.Create('La lista aún no fue creada.');
  ix := IndexOf(VarName);
  if ix = -1 then
    raise Exception.Create('Nombre de variable inexistente.');
  Result := TCDateTime(Objects[ix]).Value;
end;

procedure TListDT.ChangeVarValue(const VarName: string; Value: TDateTime);
var
  ix: Integer;
  dt: TCDateTime;
begin
  if not Assigned(Self) then
    raise Exception.Create('La lista aún no fue creada.');
  ix := IndexOf(VarName);
  if ix = -1 then
    raise Exception.Create('Nombre de variable inexistente.');
  dt := TCDateTime.Create;
  dt.Value := Value;
  Objects[ix] := TObject(dt);
end;
end.

Ejemplo de uso:
Código Delphi [-]
...
type
  TForm1 = class(TForm)
    btnShowAll: TButton;
    ListBox1: TListBox;
    btnShowtByName: TButton;
    Edit1: TEdit;
    btnChangeValue: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnShowAllClick(Sender: TObject);
    procedure btnShowtByNameClick(Sender: TObject);
    procedure btnChangeValueClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation {$R *.dfm}

uses Unit2;

var
  List: TListDT;

procedure TForm1.FormCreate(Sender: TObject);
begin
  List:= TListDT.Create;
  List.InitList(['fec1','fec2','fec3','fec4','fec5'],  // ,'fec6',...'fecN'],
     StrToDate('01/01/2014'));
end;

procedure TForm1.btnShowAllClick(Sender: TObject);
var
  i: Integer;
begin
  ListBox1.Clear;
  for i:= 0 to List.Count-1 do
    ListBox1.Items.Add(Format('%s = %s',[List[i],DateTimeToStr(List.GetVarValue(List[i]))]));
end;

procedure TForm1.btnShowtByNameClick(Sender: TObject);
begin
  ShowMessage(Format('%s = %s',
      [Edit1.Text, DateTimeToStr(List.GetVarValue(Edit1.Text))]));
end;

procedure TForm1.btnChangeValueClick(Sender: TObject);
begin
   List.ChangeVarValue(Edit1.Text, StrToDate('31/12/2015'));
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  List.Free;
end;
end.

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita