Autor Beitrag
ggehrma
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

WinXP
D2005 Pers.
BeitragVerfasst: So 04.12.05 14:34 
Hallo,

Ich habe einen Record, der so aussieht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
TYPE
  TBonsais = RECORD
    NameDe: String[255];
    NameLa: String[255];
    Art: Integer;
    Umtopfen: RECORD
      Start: TDateTime;
      Ende: TDateTime;
    END;
    Drahten: RECORD
      Start: TDateTime;
      Ende: TDateTime;
    END;
  END;
  PBonsais = ^TBonsais;

Dazu habe ich diese StringList:
ausblenden Delphi-Quelltext
1:
BonsaiList: TStringList;					

Nun möchte ich beides zusammen in eine Datei speichern. Dies mache ich bisher so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
PROCEDURE Save;
VAR
  Bonsai: TBonsais;
  f: File OF TBonsais;
  f2: File OF TStringList;
BEGIN
  AssignFile(f,Datei);
  ReWrite(f);
  Write(f,Bonsai);
  CloseFile(f);
  BonsaiList.SaveToFile(Datei2);
END;

Datei und Datei2 sind vorher als globale Constanten definiert worden. Bis hier funktioniert alles problemlos. Wenn ich jetzt aber hierüber
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
PROCEDURE Load;
Var
  Bonsai: TBonsais;
  f: File OF TBonsais;
  f2: File OF TStringList;
BEGIN
  AssignFile(f,Datei);
  Reset(f);
  Read(f,Bonsai);
  CloseFile(f);
  BonsaiList.LoadFromFile(Datei2);
END;

beides wieder auslesen will, feht die Objektverknüpfung zwischen der StringList und dem Record die ich vor dem Speichern über
ausblenden Delphi-Quelltext
1:
2:
3:
4:
    BonsaiList.Add(frmAddTree.lbledtDeutsch.Text);
    New(Bonsai);
    i := BonsaiList.Count-1;
    BonsaiList.Objects[i] := TObject(Bonsai);

angelegt habe. Wie kann ich diesen Fehler beheben oder wie kann ich das gesamte Problem umgehen?

mfg, ggehrma.

_________________
"...To boldly go where no one has gone before."
root_at_localhost
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 191



BeitragVerfasst: So 04.12.05 15:11 
Das ist nicht wirklich ein Fehler, ein Pointer zeigt auf eine Position im speicher. Wenn du die Daten speicherst und wieder lädst kommen sie aber nicht wieder da im Speicher zu liegen, wo sie vorher waren. Du must die Verknüpfung irgendwie anders speichern, beispielsweise indem du im Array noch ein Feld für den Dateinamen der Strings einbaust und dann nach dem laden den Pointer neu setzt.
ggehrma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

WinXP
D2005 Pers.
BeitragVerfasst: Mo 05.12.05 09:31 
Gibt es da nicht vielleicht noch eine anderen Möglichkeit? Ich steig da nämlich grad nicht so ganz durch wie root_at_localhost das meint.

_________________
"...To boldly go where no one has gone before."
root_at_localhost
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 191



BeitragVerfasst: Mo 05.12.05 13:07 
OK, ich versuchs nochmal genauer zu erklären:
Nach der Zuweisung
BonsaiList.Objects[i] := TObject(Bonsai);
steht in BonsaiList.Objects[i] nix anderes drin als "die Daten liegen an Position XXX im Arbeitsspeicher" wobei XXX ein Integer ist. Jetzt speicherst du das ganze, soweit kein Problem. Wenn du die Daten jetzt aber wieder lädsts sind zwar die Daten die gleichen, aber dein PC legt sie halt nicht wieder an Position XXX in den Speicher, sondern jetzt an Position YYY. Die Verknüpfung is also weg.
Stattdessen musst du die Verbindung irgendwie anders speichern, z.B. indem du deklarierst:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
TYPE
  TBonsais = RECORD
    NameDe: String[255];
    NameLa: String[255];
    Art: Integer;
    Umtopfen: RECORD
      Start: TDateTime;
      Ende: TDateTime;
    END;
    Drahten: RECORD
      Start: TDateTime;
      Ende: TDateTime;
    END;
    ListenDatei : String[255];
  END;

speichern und laden tust du jetzt per
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
PROCEDURE Save;
VAR
  Bonsai: TBonsais;
  f: File OF TBonsais;
  f2: File OF TStringList;
BEGIN
  AssignFile(f,Datei);
  ReWrite(f);
  Bonsai.ListenDatei := Datei2;
  Write(f,Bonsai);
  CloseFile(f);
  BonsaiList.SaveToFile(Datei2);
END;

PROCEDURE Load;
Var
  Bonsai: TBonsais;
  f: File OF TBonsais;
  f2: File OF TStringList;
BEGIN
  AssignFile(f,Datei);
  Reset(f);
  Read(f,Bonsai);
  CloseFile(f);
  BonsaiList.LoadFromFile(Bonsai.ListenDatei);
  i := BonsaiList.Count-1;
  BonsaiList.Objects[i] := TObject(Bonsai);

END;

Sollte dann eigentlich wieder funktionieren.
ggehrma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

WinXP
D2005 Pers.
BeitragVerfasst: Mo 05.12.05 16:04 
Hm, jetzt gibt er mir bei der Stelle
ausblenden Delphi-Quelltext
1:
BonsaiList.Objects[i] := TObject(Bonsai);					

die Fehlermeldung [Fehler] Main.pas(110): E2089 Ungültige Typumwandlung.

_________________
"...To boldly go where no one has gone before."
root_at_localhost
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 191



BeitragVerfasst: Mo 05.12.05 16:08 
Jetzt wo dus sagst, das kann aber vorher auch nicht funktioniert haben, Bonsai is ja ein Array und kein TObject.
ggehrma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

WinXP
D2005 Pers.
BeitragVerfasst: Di 06.12.05 16:51 
Doch, vorher funktioniert das völlig problemlos. Erst nach dem Laden nicht mehr.

_________________
"...To boldly go where no one has gone before."
bockwurst
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 191

win98 /XP
D1 D5 Prof DE2005PE
BeitragVerfasst: Mi 07.12.05 09:47 
Hallo,
ein anderer Weg wäre es, alles in einer Datenbank abzuspeichern. Dein Redord als Felder und deine Stringliste als Blobfeld. Dazu mußt du dann mind. 2 Functionen selbst neu implementieren.

ausblenden Delphi-Quelltext
1:
2:
Procedure LoadFromBlob(Name: TBlobField);
Procedure SaveToBlob(Name: TBlobField);
ggehrma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

WinXP
D2005 Pers.
BeitragVerfasst: Mi 07.12.05 14:22 
Wie du vielleicht gesehen hast, habe ich nur D2005Pers. Da gibt es doch keine Datenbanken so weit ich das weiß. Oder irre ich mich da?

_________________
"...To boldly go where no one has gone before."
root_at_localhost
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 191



BeitragVerfasst: Mi 07.12.05 14:40 
Also im Delphi 4 Standard gibts die Komponenten auf den Seiten Datenzugriff/Datensteuerung.
ggehrma Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

WinXP
D2005 Pers.
BeitragVerfasst: Mi 07.12.05 15:21 
Aber in D2005 nicht mehr. sorry

_________________
"...To boldly go where no one has gone before."