Autor Beitrag
Arno Nym
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Mi 13.07.05 23:10 
Hi,
ich hab versucht mal weg von statischen Arrays und record-Typen zu kommen, und mich in die dynamische objektorientierte Programmierung gewagt...seitdem stolpere ich von einem Problem ins nächste. Bei dem Folgendem bin ich stecken geblieben :

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 TGallery.DatafromText(Text : TStringlist);
var i, anzpictures : Integer;
    tempfoto: TFoto;
    temppainting : TPainting;
begin;
 Pictures.Clear;
 tempfoto := TFoto.Create();
 temppainting := TPainting.Create();
 anzpictures := gzahl(text[0]);
 text.Delete(0);
 For i := 0 to anzpictures -1 do begin;
  If text[0] = '[foto]' then begin;
   text.Delete(0);
   text := tempfoto.DatafromText(text);
   Pictures.Add(tempfoto);
  end
  else begin;
   text.Delete(0);
   text := temppainting.DatafromText(text);
   Pictures.Add(temppainting);
  end;
 end;
 Date := StrToDateTime(Text[0]);
 Description := Text[1];
 Filename := Text[2];
 tempfoto.Free();
 temppainting.Free();
end;


Wobei Pictures eine TObjektList und
Tpainting/TFoto eigene Klassen sind die von einer Überklasse TPicture erben.

Die sache ist das ja anscheinend die Objektlist auch nur mit Pointern arbeitet denn die add-Funktion ("Pictures.Add(temppainting);"
fügt nicht wirklich ein "neues" Objekt in die Liste ein. Sondern verweist einfach nur auf die temp-Objekte (temppainting/tempfoto) . Und das is natürlich mist denn ich will ja eigentlich anzpictures-namenlose Objekte in die Liste einfügen und nicht nur Links zu den 2 Objekten.
Also wie mach ich das am besten ? (ohne arrays, entweder mit TObjektlist oder TList)
(d.h. zur Zeit ist nach der Methode eh nix in Pictures drin , da die temporären Objekte ja "gefreet" werden).
Also wie erzeuge ich innerhalb der Objektlist echte Objekte ?

MFG Arno Nym
(Vor diesem post habe ich natürlich anständig nach bereits vorhanden ähnlichen Threads gesucht :)

Moderiert von user profile iconMotzi: Code- durch Delphi-Tags ersetzt.
Michael Stenzel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 133


D3 Prof, D7 PE
BeitragVerfasst: Do 14.07.05 01:39 
Hi Arno Nym!

Das Problem ist, daß Du deine Objekte Freigibst.

Habe ein kleines Programm gemacht um es Dir zu verdeutlichen.

Auf der Form ist eine Listbox, ein Memo drei Buttons.

ausblenden volle Höhe 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:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Contnrs, StdCtrls;

type
  // irgendein Testobjekt
  TMyObject = class(TObject)
    private
      FAnyInt : Integer;
      FOtherInt : Integer;
    public
      procedure SetAnyInt(const i : Integer);
      procedure SetOtherInt(const i : Integer);
      function GetAnyInt : Integer;
      function GetOtherInt : Integer;
    end;

  TForm1 = class(TForm)
    btnNeu: TButton;
    ListBox1: TListBox;
    btnLoeschen: TButton;
    btnBeenden: TButton;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure btnNeuClick(Sender: TObject);
    procedure btnLoeschenClick(Sender: TObject);
    procedure btnBeendenClick(Sender: TObject);
  private
    { Private-Deklarationen }
    MYObjectList : TObjectList; // <-- Liste
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
    MyObjectList := TObjectList.Create; // Liste erzeugen
end;

// Memberfunktionen des Testobjekts 
procedure TMyObject.SetAnyInt(const i : Integer);
begin
  FAnyInt := i;
end;

procedure TMyObject.SetOtherInt(const i : Integer);
begin
  FOtherInt := i;
end;

function TMyObject.GetAnyInt : Integer;
begin
  Result := FAnyInt;
end;

function TMyObject.GetOtherInt : Integer;
begin
  Result := FOtherInt;
end;

// Button Neu um ein neues Objekt zu erzeugen
procedure TForm1.btnNeuClick(Sender: TObject);
var  i : TMyObject;
begin
     i := TMyObject.Create; // <-- Hier Speicher belegen und 
                            //     Zeiger in i Speichern

     i.SetAnyInt(MyObjectList.Count); // Nummer des Objekts
     i.SetOtherInt(12);

     MyObjectList.Add(i);   // <-- In Liste mit dem Objekt

    // Anzeige in Listbox und Memo
 
    ListBox1.Items.Add(IntToStr(i.GetAnyInt));
     Memo1.Lines.Add('Eingefügt String Nr : '+IntToStr(i.GetAnyInt));
end;

procedure TForm1.btnLoeschenClick(Sender: TObject);
var i : Integer;
begin
    // Nur Löschen wenn eintrag in Listbox gewählt
    If Listbox1.ItemIndex > 0 then begin
      For i := 0 to MyObjectList.Count - 1 do begin
        // Zugriff auf dem Gespeicherten Objekt mit einen 
        // Typecast 
        if TMyObject(MyObjectList.Items[i]).GetAnyInt =
           StrToInt(Listbox1.Items.Strings[Listbox1.ItemIndex])then begin
             // Objekt wird entfernt und Speicher freigegeben
             MyObjectList.Delete(i);
             // und Anzeige aktualisieren
             Memo1.Lines.Add('Entfernt String Nr.:'+Listbox1.Items.Strings[Listbox1.ItemIndex]);
             Listbox1.Items.Delete(Listbox1.ItemIndex);
             Break;
        end;
      end;
    end;
end;

procedure TForm1.btnBeendenClick(Sender: TObject);
Var i : Integer;
begin
  // Vorm beenden die Liste noch mal anzeigen
  Memo1.Lines.Add('Liste ausgeben');
  For i := 0 to MyObjectList.Count - 1 do
      Memo1.Lines.Add(IntToStr(TMyObject(MyObjectList.Items[i]).GetAnyInt));
  ShowMessage('Fertig');
  MyObjectList.Clear; // Alles Freigeben
  Close;
end;

end.


Gruß
Michael.
Arno Nym Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Do 14.07.05 09:43 
Hallo Michael,
erstmal vielen Dank für deine Bemühungen und das Beispielprogramm (hab das gleich mal nachgebaut)!

Du hast recht, die Objekte dürfen nach der Erzeugung nicht "gefreet" werden, Ich hatte das auch schon mal ohne die frees ausprobiert gestern und es ging trotzdem nicht, aber heute morgen ist mir ein anderer Fehler aufgefallen: Die Create-methoden für die temp-Objekte müssen natürlich innerhalb der For-Schleife stehen :roll: damit auch anzpics-verschiedene Objekte erzeugt werden...
also so:

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 TGallery.DatafromText(Text : TStringlist);
var i, anzpictures : Integer;
    tempfoto: TFoto;
    temppainting : TPainting;
begin;
 Pictures.Clear;
 anzpictures := gzahl(text[0]);
 text.Delete(0);
 For i := 0 to anzpictures -1 do begin;
  If text[0] = '[foto]' then begin;
   tempfoto := TFoto.Create();
   text.Delete(0);
   text := tempfoto.DatafromText(text);
   Pictures.Add(tempfoto);
  end
  else begin;
   temppainting := TPainting.Create();
   text.Delete(0);
   text := temppainting.DatafromText(text);
   Pictures.Add(temppainting);
  end;
 end;
 Date := StrToDateTime(Text[0]);
 Description := Text[1];
 Filename := Text[2];
 //tempfoto.Free();
 //temppainting.Free();
end;


jetzt funktioniert es einwandfrei. Vielen Dank nochmal für deine schnelle Hilfe !
schönen Tag noch, Gruß Arno Nym.

Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt.