Autor Beitrag
Martin13
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Do 20.01.05 21:02 
Kann ich Bezeichner mit Variablen ansprechen?
Ich habe in einem Programm Shape 1 bis 64. Wenn ich bei bei denen allen was ändern will, kann ich die dann über eine Schleife alle ansprechen? So nach dem Motto:

Shape'i':=...
inc(i);

Ist das möglich?


Moderiert von user profile iconTino: Topic aus Programmierwerkzeuge verschoben am Do 20.01.2005 um 22:25
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 20.01.05 21:26 
Siehe Suche in: Delphi-Forum, Delphi-Library FINDCOMPONENT, Suche in: Delphi-Library FINDCOMPONENT Suche in der Delphi-PRAXiS FINDCOMPONENT oder Suche im MSDN FINDCOMPONENT ansonsten [doh]FindComponent[/doh]

Uns fehlen hier noch die DOH-Tags :twisted:

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Vorax
Hält's aus hier
Beiträge: 3

Win XP Pro, Win Vista, Win 7
D7 Prof, Delphi 2006, Delphi XE, C#
BeitragVerfasst: Sa 22.01.05 18:47 
Hallo!

Wenn ich Deine Frage richtig verstanden habe, hast Du 64 Objekte vom gleichen Typ, nur der Bezeichner hat eine fortlaufende Nummer. Das ist etwas umständlich. :(

Versuch zum Beispiel, die Objekte alle in eine Liste zu hängen.
So in der Art:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
FShapeList := TList.Create;
FShapeList.Clear;

for li := 0 to 63 do begin
  FShapeList.Add(?TShape?.Create);
end;


Nun kannst Du jedes Objekt über die Liste ansprechen, z.B.:
ausblenden Delphi-Quelltext
1:
x := FShapeList[2].ClassName;  ( analog zu x := FShapeList.Items[2].ClassName; )					



Für typenabhängige Methoden (TList beinhaltet Objekte vom Typ TObject) musst Du ihm möglicherweise noch den Typ definieren ("Typecasting") ...

Das sieht dann so aus:

?TShape?(FShapeList[2]).MethodevonTShape;

Wobei mit ?TShape? der Type Deines Shape-Objektes gemeint ist ...


Vergiss nicht, die Objekte vor dem killen der Liste separat freizugeben!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
li := 0;

while li < FShapeList.Count do begin
  FShapeList[li].Free;
  inc(li);
end;

FShapeList.Free;




Viel Spaß,
Vorax

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 22.01.05 19:14 
Wenn Du TObjectList (Unit contnrs), kannst Du als OwnsObjects-Parameter True übergeben. In diesem Fall gibt die TObjectList die Objekte für Dich frei.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Benutzername
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 210

Win XP Pro
Delphi 7 PE, D2005 Prof. SSL
BeitragVerfasst: Sa 22.01.05 19:35 
Du könntest auch einfach die Shapes in ein Array packen und sie dann etwa so ansprechen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var ShapeArray:array[0..63] of TShape
    i:integer;
begin
for i:=0 to 63 do
begin
ShapeArray[i]:= TShape.Create;
ShapeArray[i].Name:='Ich bin Shape Nummer '+inttostr(i);
end;


Kenn mich jetzt net so mit Shapes aus, aber so in der Art müssts funktionieren ;-)