Einzelnen Beitrag anzeigen

Andreas L.
(Gast)

n/a Beiträge
 
#1

Kann auf Funktion nicht zugreifen obwohl Objekt erstellt...

  Alt 6. Nov 2008, 17:06
Hi,

wenn ich auf die Funktion Count meiner ObjectList zugreife bekomme ich folgende Access Violation

Zitat:
---------------------------
XXX
---------------------------
Zugriffsverletzung bei Adresse 0045EFB4 in Modul 'XXX.exe'. Lesen von Adresse 00000030.
---------------------------
OK
---------------------------
Hier der Code:

Delphi-Quellcode:
unit XXX;

interface

uses
  SysUtils, Classes, ContNrs, IniFiles, XXXUtils;

type
  { forward declarations }
  TStores = class;
  TStore = class;

  TXXX = class(TComponent)
  private
    FStores: TStores;
  protected
    { Protected-Deklarationen }
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure LoadFromFile(FileName: String);
    procedure SaveToFile(FileName: String);
    property Stores: TStores read FStores;
  published
    { Published-Deklarationen }
  end;

  TStores = class(TComponent)
  private
    FStores: TObjectList;
  protected
    procedure SetStore(Index: Integer; Value: TStore);
    function GetStore(Index: Integer):TStore;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Add:Integer;
    function Count:Integer;
    procedure Delete(Index: Integer);
    property Stores[Index: Integer]: TStore read GetStore write SetStore; default;
  end;

  TStore = class(TComponent)
  private
    FName: String;
    FAdress: String;
    FWebsite: String;
    FFavorite: Boolean;
    FTelephone: String;
  published
    property Name: String read FName write FName;
    property Adress: String read FAdress write FAdress;
    property Website: String read FWebsite write FWebsite;
    property Favorite: Boolean read FFavorite write FFavorite;
    property Telephone: String read FTelephone write FTelephone;
  end;

implementation

{ TXXX }
constructor TXXX.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FStores := TStores.Create(Self);
end;

destructor TXXX.Destroy;
begin
  FStores.Free;
  inherited Destroy;
end;

procedure TXXX.LoadFromFile(FileName: string);
var
  ini: TMemIniFile;
  i: Integer;
begin
  ini := TMemIniFile.Create(FileName);

  //read stores
  for i := 0 to ini.ReadInteger('Stores', 'Count', 0) -1 do
  begin
    with Stores[Stores.Add] do
    begin
      Name := ini.ReadString('Stores', 'Name' + IntToStr(i), '');
      Adress := UnEscapeLineBreaks(ini.ReadString('Stores', 'Adress' + IntToStr(i), ''));
      Website := ini.ReadString('Stores', 'Website' + IntToStr(i), '');
      Favorite := ini.ReadBool('Stores', 'Favorite' + IntToStr(i), False);
      Telephone := ini.ReadString('Stores', 'Telephone' + IntToStr(i), '');
    end;
  end;

  ini.Free;
end;

procedure TXXX.SaveToFile(FileName: string);
var
  ini: TMemIniFile;
  i: Integer;
begin
  ini := TMemIniFile.Create(FileName);
  ini.Clear;

  //write stores
  ini.WriteInteger('Stores', 'Count', Stores.Count);
  for i := 0 to Stores.Count -1 do
  begin
    with Stores[i] do
    begin
      ini.WriteString('Stores', 'Name' + IntTostr(i), Name);
      ini.WriteString('Stores', 'Adress' + IntTostr(i), Adress);
      ini.WriteString('Stores', 'Website' + IntTostr(i), Website);
      ini.WriteBool('Stores', 'Favorite' + IntToStr(i), Favorite);
      ini.WriteString('Stores', 'Telephone' + IntTostr(i), Telephone);
    end;
  end;

  ini.UpdateFile;
  ini.Free;
end;

{ TStores }
constructor TStores.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FStores := TObjectList.Create;
end;

destructor TStores.Destroy;
begin
  FStores.Free;
  inherited Destroy;
end;

procedure TStores.SetStore(Index: Integer; Value: TStore);
begin
  FStores[Index] := Value;
end;

function TStores.GetStore(Index: Integer):TStore;
begin
  Result := FStores[Index] as TStore;
end;

function TStores.Add:Integer;
var
  NewStore: TStore;
begin
  NewStore := TStore.Create(Self);
  Result := FStores.Add(NewStore);
end;

function TStores.Count:Integer;
begin
  Result := FStores.Count;
end;

procedure TStores.Delete(Index: Integer);
begin
  FStores.Delete(Index);
end;

end.
FStores wird doch ordentlich erstellt, warum kann ich auf Count nicht zugreifen?
  Mit Zitat antworten Zitat