![]() |
Kann auf Funktion nicht zugreifen obwohl Objekt erstellt...
Hi,
wenn ich auf die Funktion Count meiner ObjectList zugreife bekomme ich folgende Access Violation Zitat:
Delphi-Quellcode:
FStores wird doch ordentlich erstellt, warum kann ich auf Count nicht zugreifen?
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. |
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
Zitat:
Delphi-Quellcode:
heißen?
with FStores[Stores.Add] do
|
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
Zitat:
|
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
In welcher Zeile genau kommt die Meldung? Und hast du mal einen Haltepunkt gesetzt und die verschiedenen Objecte in die Watchlist gepackt?
|
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
Zitat:
Delphi-Quellcode:
Wie soll mir die Liste der überwachten Ausdrücke helfen?
function TStores.Count:Integer;
begin Result := FStores.Count; end; |
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
Und an welcher Stelle rufst du das auf?
Die Liste der überwachten Ausdrücke soll dir in soweit helfen das du siehst welche Objecte und darin enthaltenen Objecte instanziert sind. Denn deine Zugriffsverletzung deutet sehr darauf hinn das auf ein nicht instanziertes Object zugegriffen wird. |
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
Der Fehler tritt beim Aufruf von SaveToFile auf. Beim überwachen von FStores wird mir folgendes angezeigt:
Zitat:
|
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
Und wenn du wie Detlef schon bemerkt hast direkt auf das Feld und nicht auf die Property zugreifst?
|
Re: Kann auf Funktion nicht zugreifen obwohl Objekt erstellt
Fehler gefunden. Hab im Demoprojekt die Instanz meiner Komponente zuerst als lokale Variable erzeugt und dann auf global umgestellt. Dabei hab ich vergessen die lokalen Variablen zu entfernen... *mehr Kaffee brauch*
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:14 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz