GARANTIR DESCONTO

Fórum Problema com código #352055

18/01/2008

0

Quem poderia me ajuar com o cógigo abaixo?

Unit:
unit UTabela;

interface

uses
  UCollection, Classes;

type
  TTabela = class
  private
    FCampos: TCampos;
  public
    constructor Create;
    property CamposTab: TCampos read FCampos write FCampos;
  end;

var
  FTabela: TTabela;

implementation

uses SysUtils;

{ TTabela }

constructor TTabela.Create;
begin
  inherited Create;
  FCampos := TCampos.Create(TCollectionItem);
end;

initialization
  if FTabela = nil then
    FTabela := TTabela.Create;

  //TTabela.Create;

finalization
  FreeAndNil(FTabela);

end.


Unit
unit URegistradas;

interface

uses
  UTabela, UCollection, Classes, SysUtils, Dialogs;

type
  TMercadoria = class(TTabela)
  public
    constructor Create;
  end;

var
  CampoTab: TCampo;

implementation

{ TMercadoria }

constructor TMercadoria.Create;
begin
  inherited Create;
  CampoTab := CamposTab.Add;
  CampoTab.Nome := ´Teste´;
  ShowMessage(IntToStr(CamposTab.Count));
end;


initialization
  if CampoTab = nil then
    CampoTab := TCampo.Create(nil);


finalization


end.


Unit
unit UCollection;

interface

uses
  Classes, SysUtils;

type
  TCampo = class(TCollectionItem)
  private
    FNome: string;
  public
    property Nome: string read FNome write FNome;
  end;

  TCampos = class(TCollection)
  private
    function GetItems(Index: Integer): TCampo;
  public
    function Add: TCampo;
    property Items[index: Integer]: TCampo read GetItems;
  end;

var
  Campo: TCampo;
  Campos: TCampos;

implementation

uses TypInfo;

function TCampos.GetItems(index: Integer): TCampo;
begin
  Result := TCampo(inherited GetItem(Index));
end;

function TCampos.Add: TCampo;
begin
  Result := TCampo(inherited Add);
end;

initialization
  if Campo = nil then
    Campo := TCampo.Create(nil);

  if Campos = nil then
    Campos := TCampos.Create(TCampo);

finalization
  FreeAndNil(Campo);
  FreeAndNil(Campos);

end.


No Button do Form tenho o seguinte código
  Campo := FTabela.CamposTab.Add;
  Campo.Nome := ´Teste´;
  ShowMessage(IntToStr(Campos.Count));


Ele está dando erro na linha Campo.Nome := ´Teste´;
da acces violation

oq pode ser?

Vlw


Luiz_aquino

Luiz_aquino

Responder

Posts

18/01/2008

Luiz_aquino

Corrigindo em UTabela

constructor TTabela.Create;
begin
  inherited Create;
  if FCampos = nil then
    FCampos := TCampos.Create(TCampo);
end;


Essa função em registradas deveria estar adicionando, mas não ocorre

constructor TMercadoria.Create;
begin
  inherited Create;
  CampoTab := CamposTab.Add;
  CampoTab.Nome := ´Teste´;
  //ShowMessage(IntToStr(CamposTab.Count));
end;



Responder

Gostei + 0

18/01/2008

Djjunior

Help do Delphi

Create constructor (TField)
Creates and initializes a field component.

Delphi syntax:

constructor Create(AOwner: TComponent); override;
[b]Do not create TField instances...[/b]


[/code]


Responder

Gostei + 0

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

Aceitar