Autor Beitrag
Niels_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 117



BeitragVerfasst: Sa 04.12.04 00:28 
Ich habe ein dynamisches array von Timage (eigentlich eine klasse, mit noch ein paar extra variablen).
Nun möchte ich ein bild herausnehmen (löschen!), jedoch weiß ich nciht wie ich das machen soll, da immer noch müll übrig bleibt:

nr gibt an, ab wo er aufschieben soll
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var a:integer;
   a:=nr;
   while a+1<length(k) do
    begin
      k[a]:=k[a+1];
      inc(a);
    end;
//   k[length(k)-1].Free;
   setlength(k,length(k)-1);


free funzt nicht, da er dann immer abstürzt!!!
Und irgendwie wird die tatsächliche arraylänge (letzte zeile) hier nicht richtig geändert! Das ist echt nerviges Delphi!
Hat jemand ein lösungsvorschlag, wie ich das wieder freigebe?

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Sa 04.12.04 01:23 
Warum eigentlich ein Array? Warum nicht TList oder TObjectList?

_________________
Ciao, Sprint.
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Sa 04.12.04 07:47 
Hallo,

anstatt Length musst Du in diesem Fall High benutzen. Näheres dazu in der Delphi-Hilfe.

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 04.12.04 09:35 
smiegel hat folgendes geschrieben:
anstatt Length musst Du in diesem Fall High benutzen. Näheres dazu in der Delphi-Hilfe.

Wieso muss? Wenn man weiß, dass dynamische Arrays bei 0 anfangen, dann kann man Schleifen auch bis length() - 1 laufen lassen. ;)
Niels_ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 117



BeitragVerfasst: Sa 04.12.04 19:07 
Wieso denn Tlist? Ändert das was?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
type
  Ttest=class(Timage)
    a,b:integer;
    ...
  end;


Und das funzt nicht, wenn ich das versuche zu freen oder zu destroyen. Und eine eigene destroy zu schreiben bringt ja auch nichts, da ja keine anderen classen o.ä. erstellt werden. Sind "ganz normale" variablen drinne!
Wieso geht das denn nicht? Und weiß niemand, wie man das lösen kann?
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 04.12.04 19:10 
Er meint eine Liste statt dem Array, nicht statt der Klasse ;)

AXMD
Niels_ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 117



BeitragVerfasst: Sa 04.12.04 19:47 
Sorry.. Verstehe gerade nur Bahnhof!
Wie mache ich das denn mit der Liste?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
type 
  Ttest=class(image)
   a,b:integer;
  end; 
  test=list of Ttest


so etwa?!!!
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 04.12.04 19:56 
Nein. Schau mal in der Delphi-Hilfe nach: "TList". Einziger Nachteil: Pointer.

AXMD
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Sa 04.12.04 20:44 
Hallo,

@Luckie:
In diesem Fall ja. Aber (evtl.) besser ist das, wenn man High benutzt.

@Niels_:
Wenn Du D7 (evtl. reicht schon D6) hast, dann benutze TObjectList. Die ist in der Unit Contnrs versteckt.

Folgendes Beispiel soll die Funktionsweise einer TObjectList veranschaulichen:
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:
var fMyObjList:TObjectList;
    i:Integer;
    aImg:TImage;
...
  fMyObjList:=TObjectList.Create;
  try
    // so, mal 5 ImageObjekte hinzufügen
    for i:=1 to 5 do fMyObjList.Add(TImage.Create(Self));
    // oder
    for i:=1 to 5 do
    begin
      aImg:=TImage.Create(Self);
      aImg.Width:=32;
      aImg.Height:=32;
      ...
      fMyObjList.Add(aImg);
    end; // for i
    ShowMessage(Format('Die Liste enthält %d Elemente!', [fMyObjectList.Count]);

    // 2. Element in der Liste löschen
    fMyObjList.Delete(1);
    ShowMessage(Format('Die Liste enthält %d Elemente!', [fMyObjectList.Count]);
  
    // 1. Element in der Liste löschen
    fMyObjList.Delete(0);
    ShowMessage(Format('Die Liste enthält %d Elemente!', [fMyObjectList.Count]);

    // Das letzte Image in der Liste auswählen
    aImg:=TImage(fMyObjList.Items[fMyObjectList.Count-1]);
    ShowMessage(Format('Das Image ist %d Pixel breit und %d Pixel hoch.', [aImg.Width, aImg.Heigth]));
  finally
    fMyObjList.Free;
  end; // try
...

Das schöne an der ObjectList ist, dass man sich um die Speicherverwaltung keine Gedanken machen muss, solange es sich um Abkömmlinge von TObjekt handelt.

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
Niels_ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 117



BeitragVerfasst: Di 07.12.04 19:49 
danke smiegel,
das war genau das, was ich gebraucht hab :D :D
Vielen Dank