Retornar classe na função

Delphi

23/09/2013

Olá, gostaria de saber se tem como o retorno da função ser classe.
Ex: function Pesquisar(Value:Integer):TClientes

Fiz dessa forma, mas se faço um teste como o abaixo,
if TClientes.Create(Self).Pesquisar(''1'').Codigo > 0 then ....
Sempre dá violação de acesso..
Devjunior

Devjunior

Curtidas 0

Respostas

Joel Rodrigues

Joel Rodrigues

23/09/2013

Poder, pode, poste aqui o código da sua função para analisarmos. Provavelmente o erro está nela.
GOSTEI 0
Devjunior

Devjunior

23/09/2013

unit Unit2;

interface

uses
Data.DB, Data.Win.ADODB,System.SysUtils;

type
TCliente = class
private
FCodigo: integer;
FNome: string;
FCpfCgc: string;
procedure SetCodigo(const Value: integer);
procedure SetNome(const Value: string);
procedure SetCpfCgc(const Value: string);
public
property Codigo : integer read FCodigo write SetCodigo;
property Nome : string read FNome write SetNome;
property CpfCgc : string read FCpfCgc write SetCpfCgc;

function Pesquisar(Value:integer):TCliente;
end;

implementation

{ TCliente }

uses Unit1, Unit3;

function TCliente.Pesquisar(Value: integer): TCliente;
var
qry : TADOQuery;
begin
qry := TADOQuery.Create(nil);
qry.Connection := DataModule3.ADOConnection1;
qry.SQL.Text := 'select cliid, clinome, cliCpfCgc from cliente where cliid='+IntToStr(Value);
qry.Open;
if not qry.IsEmpty then
begin
FCodigo := qry.FieldByName('Cliid').Value;
FNome := qry.FieldByName('Clinome').Value;
FCpfCgc := qry.FieldByName('cliCpfCgc').Value;
end;
end;

procedure TCliente.SetCodigo(const Value: integer);
begin
FCodigo := Value;
end;

procedure TCliente.SetCpfCgc(const Value: string);
begin
FCpfCgc := Value;
end;

procedure TCliente.SetNome(const Value: string);
begin
FNome := Value;
end;

end.
GOSTEI 0
Joel Rodrigues

Joel Rodrigues

23/09/2013

Na sua função você precisa instanciar um TClientes e retornar essa instância usando o result.
GOSTEI 0
Devjunior

Devjunior

23/09/2013

Obrigado!
Consegui implementar.
GOSTEI 0
Joel Rodrigues

Joel Rodrigues

23/09/2013

Que bom, fico feliz por ter ajudado. Abraço.

Estou marcando o tópico como concluído.
GOSTEI 0
POSTAR