Fórum Retornar classe na função #456198

23/09/2013

0

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

Responder

Posts

23/09/2013

Joel Rodrigues

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

Gostei + 0

23/09/2013

Devjunior

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.
Responder

Gostei + 0

23/09/2013

Joel Rodrigues

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

Gostei + 0

24/09/2013

Devjunior

Obrigado!
Consegui implementar.
Responder

Gostei + 0

24/09/2013

Joel Rodrigues

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

Estou marcando o tópico como concluído.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar