Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #7  
Antiguo 04-09-2013
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 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
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Como Obtener un Tipo de dato Variante berna Firebird e Interbase 2 02-11-2010 19:14:01
codigo para guardar dato tipo bit marareta SQL 4 07-08-2010 23:20:21
Tipo de dato Text se ve como MEMO DenisDiaz PostgreSQL 3 30-12-2009 15:11:19
Guardar cualquier tipo de archivo en un campo en sql server zafmanjp MS SQL Server 7 21-12-2007 18:34:52
Guardar cualquier tipo de datos en un mismo archivo metalfox6383 Varios 5 22-08-2005 21:35:54


La franja horaria es GMT +2. Ahora son las 01:34:14.


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
Copyright 1996-2007 Club Delphi