Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Error con property leendo un metodo (https://www.clubdelphi.com/foros/showthread.php?t=72938)

Paulao 21-03-2011 14:54:52

Error con property leendo un metodo
 
Quando yo hago una property para leer directo de una funcion, el me lo da el error de Types Incompatibles. Si pongo una variable y pongo mi property para leer en esta variable, me lo da el error de: Missing Operator or Semicolon.
Código Delphi [-]
...
type
 Mi_Classe = class
 private
 function GetVersion(Mi_Parametros: string): String;
public
 propety Version: string read Getversion;
end;

Ariba me lo da el error de Types Incompatibles y abajo me lo da el error de Missing Operator or Semicolon.

Código Delphi [-]
...
type
 Mi_Classe = class
 FVersion: String;
private
 function GetVersion(Mi_Parametros: string): String;
public
 propety Version: string read FVersion;
end;
......
 function GetVersion(Mi_Parametros: string): String;
begin
  Result := las_cosas_de_la_funcion_aca;
  FVersion := Result;
end;

ecfisa 21-03-2011 15:24:40

Hola Paulao.

En una propiedad, el método de lectura tiene que ser una funcion sin parámetros y el método de escritura un procedimiento con un sólo parámetro.

Por ejemplo:
Código Delphi [-]
...
type
 TMi_Clase = class(TObject)
  private
    FVersion: string;
    function GetVersion: string;
    procedure SetVersion(Ver: string);
  public
    property Version: string read FVersion write FVersion;
  end;
...
implementation
...
function TMi_Clase.GetVersion: string;
begin
  Result:= FVersion; // (o código para obtener la versión)
end;

procedure TMi_Clase.SetVersion(Ver: string);
begin
  FVersion:= Ver;
end;

Ejemplo de uso:
Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
var
  MClase: TMi_Clase;
begin
  MClase:= TMi_Clase.Create;
  MClase.Version:= 'xx.xx.xx';
  if MClase.Version <> 'xx.xx.xx' then
    ShowMessage('¡ Imposible !')
  else
    ShowMessage('Todo Ok...');
end;

Edito: Aunque para ampliar el ejemplo utilizé una propiedad con lectura y escritura, es posible definirla de sólo lectura o sólo escritura. Y también podés prescindir de la variable privada.

Un saludo.

Paulao 21-03-2011 15:55:14

Esta es mi funcion que deberia traer el result en mi property
Código Delphi [-]
function TGeneralFiles.GetRet_Versao(NomeArq: string): string;
var
  VerInfoSize, VerValueSize, Dummy: DWORD;
  VerInfo: Pointer;
  VerValue: PVSFixedFileInfo;
  Maior, Menor, Release, Build: Word;
begin
  Result := '';

  VerInfoSize := GetFileVersionInfoSize( PChar(NomeArq), Dummy );
  GetMem( VerInfo, VerInfoSize );
  try
    GetFileVersionInfo( PChar(NomeArq), 0, VerInfoSize, VerInfo );
    VerQueryValue( VerInfo, '', Pointer(VerValue), VerValueSize );
    with VerValue^ do
    begin
      Maior := dwFileVersionMS shr 16;
      Menor := dwFileVersionMS and $FFFF;
      Release := dwFileVersionLS shr 16;
      Build := dwFileVersionLS and $FFFF;
    end;
  finally
    FreeMem( VerInfo, VerInfoSize );
  end;

  Result :=
    IntToStr(Maior) + '.' + IntToStr(Menor) + '.' +
    IntToStr(Release) + '.' + IntToStr(Build);
    FVersao := Result;
end;

ecfisa 21-03-2011 16:30:01

Hola Paulao.

Una forma posible sería:
Código Delphi [-]
...
type
  TMiClase = class(TObject)
  private
    FAppName: string;
    function GetAppVersion: string;
  public
    property Version: string read GetAppVersion write FAppName;
  end;
...

implementation
...

function TMiClase.GetAppVersion:string;
var
 Size, Size2: DWord;
 Pt, Pt2: Pointer;
begin
  Size := GetFileVersionInfoSize(PChar(FAppName), Size2);
  if Size > 0 then
  begin
    GetMem (Pt, Size);
    try
      GetFileVersionInfo (PChar (FAppName), 0, Size, Pt);
      VerQueryValue (Pt, '\', Pt2, Size2);
      with TVSFixedFileInfo (Pt2^) do
      begin
        Result:= ' Ver '+
                 IntToStr (HiWord (dwFileVersionMS)) + '.' +
                 IntToStr (LoWord (dwFileVersionMS)) + '.' +
                 IntToStr (HiWord (dwFileVersionLS)) + '.' +
                 IntToStr (LoWord (dwFileVersionLS));
      end;
    finally
      FreeMem (Pt);
    end;
  end;
end;
...

Llamada:
Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
var
  MC: TMiClase;
begin
  MC:= TMiClase.Create;
  MC.Version:= 'C:\Program Files\Borland\Delphi7\Bin\Delphi32.exe';
  ShowMessage(MC.Version);
end;
Para obtener la versión utilizé la función sugerida por el amigo rgstuamigo en el post Cargar Version en el Caption del Form .


Un saludo.

Paulao 21-03-2011 17:37:38

Gracias por la solucion. Asi si quedo mi Unit y la llamada
Código Delphi [-]
type
  TGeneralFiles = class
  FAppName: string;
  private
    function GetAppVersion: string;
  public
    property Versao: string read GetAppVersion write FAppName;
    constructor Create;
end;

implementation

{ GeneralFiles }

constructor TGeneralFiles.Create;
begin
  FAppName := Application.ExeName;
end;

function TGeneralFiles.GetAppVersion: string;
var
 Size, Size2: DWord;
 Pt, Pt2: Pointer;
begin
  Size := GetFileVersionInfoSize(PChar(FAppName), Size2);
  if Size > 0 then
  begin
    GetMem (Pt, Size);
    try
      GetFileVersionInfo (PChar (FAppName), 0, Size, Pt);
      VerQueryValue (Pt, '\', Pt2, Size2);
      with TVSFixedFileInfo (Pt2^) do
      begin
        Result:= ' Versão: '+
                 IntToStr (HiWord (dwFileVersionMS)) + '.' +
                 IntToStr (LoWord (dwFileVersionMS)) + '.' +
                 IntToStr (HiWord (dwFileVersionLS)) + '.' +
                 IntToStr (LoWord (dwFileVersionLS));
      end;
    finally
      FreeMem (Pt);
    end;
  end;
end;

end.

Código Delphi [-]
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FGeneralFiles := TGeneralFiles.Create;
end;
Código Delphi [-]
procedure TfrmMain.FormShow(Sender: TObject);
begin
  Self.Caption := Self.Caption + ' - ' + FGeneralFiles.Versao;
end;
Ya estas resuelto.


La franja horaria es GMT +2. Ahora son las 21:22:08.

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