Autor Beitrag
Tower
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Fr 21.02.03 11:31 
Ähm...

mal eine ganz blöde Frage: Folgendes Code-Segment...

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type
  TAufzaehlung = (abc, def, ghi, jkl);

var
  Aufzaehlung: TAufzaehlung

[...]
  Aufzaehlung := def;
  IrgendeinInteger := Ord(Aufzaehlung);
[...]


... setzt ja IrgendeinInteger auf 1, sprich: Ord gibt mir die "Position" in einem Aufzählungstyp zurück.

Wie mache ich das denn umgekehrt? Wie kann ich "Aufzaehlung" auf zB den dritten Eintrag von TAufzaehlung setzen? (soll natürlich über die "3" funktionieren, nicht über das "jkl" :-) )


Danke!
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: Fr 21.02.03 11:47 
Hi,

setzen kannst du zur Laufzeit gar nichts (du kannst deinen Typ ja nicht einfach zur Laufzeit ändern) - wenn du das meinst.

AXMD
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Fr 21.02.03 11:55 
So:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  Aufzaehlung: TAufzaehlung;
  Idx: Integer;
begin
  Aufzaehlung := ghi;

  Idx := Ord (Aufzaehlung);

  Aufzaehlung := TAufzaehlung (Idx);
end;

Sinnvoll wäre es auch die Variable Idx vor dem Typecast [also: TAufzaehlung (Idx) ] auf einen gültigen Wert zu überprüfen.

Gruß
TINO
Tower Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Fr 21.02.03 11:55 
Nein, den Typ will ich gar nicht anfassen. Ich möchte, dass nach sowas wie...

ausblenden Quelltext
1:
  Aufzaehlung := IrgendeineFunktion(3);					


... Aufzaehlung=jkl ist.
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: Fr 21.02.03 12:03 
Hallo Tower,

das ist ja eigentlich der Sinn von Aufzählungen, dass ich Sie über den symbolischen Namen ansprechen kann.

ausblenden Quelltext
1:
  Aufzaehlung:=TAufzaehlung(3);					


Achtuing: Zählung beginnt mit 0.

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



BeitragVerfasst: Fr 21.02.03 12:04 
Mpf. So einfach... alles klar, danke!

@smiegel: Hast prinzipiell natürlich völlig recht. Ich brauche das in dem Fall aber, weil ich den Inhalt von ein paar Aufzählungsvariablen in der Registry speichern will. Und das geht so halt ganz gut...