Ver Mensaje Individual
  #7  
Antiguo 04-09-2013
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
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 Nelson.

Como funciones independientes:
Código Delphi [-]
procedure SaveComponent(CP: TComponent; aFileName: TFileName);
begin
  if not Assigned(CP) then
    raise Exception.Create('Objeto inexistente');
  with TFileStream.Create(aFileName, fmCreate) do
  try
    WriteComponent(CP);
  finally
    Free;
  end;
end;

procedure LoadComponent(CP: TComponent;aFileName: TFileName);
begin
  if not Assigned(CP) then
    raise Exception.Create('Objeto inexistente');
  if not FileExists(aFileName) then
    raise Exception.Create('Archivo inexistente');
  with TFileStream.Create(aFileName, fmOpenRead) do
  try
    ReadComponent(CP);
  finally
    Free;
  end;
end;

Como métodos de clase:
Código Delphi [-]
...
implementation 

type
  TMiClase = class(TComponent)
  private
    FStr : string;
    FEnt : integer;
    FDob : Double;
  published
    property Str : string read FStr write FStr;
    property Ent : Integer read FEnt write FEnt;
    property Dob : Double read FDob write FDob;
  public
    constructor Create(AOwner: TComponent); override;
    procedure SaveToFile(const aFileName: TFileName);
    procedure LoadFromFile(const aFileName: TFileName);
  end;

var
  MC : TMiClase;

constructor TMiClase.Create;
begin
  inherited;
  FStr := 'Una frase cualquiera';
  FEnt := 1234;
  FDob := 3.141592654
end;

procedure TMiClase.SaveToFile(const aFileName: TFileName);
begin
 if not Assigned(Self) then
    raise Exception.Create('Objeto inexistente');
  with TFileStream.Create(aFileName, fmCreate) do
  try
    WriteComponent(Self);
  finally
    Free
  end
end;

procedure TMiClase.LoadFromFile(const aFileName: TFileName);
begin
  if not Assigned(Self) then
    raise Exception.Create('Objeto inexistente');
  if not FileExists(aFileName) then
    raise Exception.Create('Archivo inexistente');
  with TFileStream.Create(aFileName, fmOpenRead) do
  try
    ReadComponent(Self);
  finally
    Free
  end
end;


{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
  MC : TMiClase;
begin
  MC := TMiClase.Create(Self);
  try
    MC.SaveToFile('C:\MiComp.dat');
  finally
    MC.Free
  end
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  MC : TMiClase;
begin
  MC := TMiClase.Create(Self);
  try
    MC.LoadFromFile('C:\MiComp.dat');
    ShowMessage(Format('%s %s%d %s%f',[MC.Str,#10,MC.Ent,#10,MC.Dob]));
  finally
    MC.Free
  end
end;
...

Saludos
__________________
Daniel Didriksen

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

Última edición por ecfisa fecha: 04-09-2013 a las 19:56:30.
Responder Con Cita