Autor Beitrag
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Do 28.07.05 10:04 
Gegeben ein Set:
ausblenden Delphi-Quelltext
1:
type TMySet = (msA,msB,msC,msD);					


und z.B. einige Strings

ausblenden Delphi-Quelltext
1:
const TMyString = ('myA','myB','myC','myD');					


Kann man irgendwie eleganter die Strings in das entsprechende Set-Element konvertieren als mit if-else Anweisungen?

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.


Zuletzt bearbeitet von Spaceguide am Do 28.07.05 17:16, insgesamt 1-mal bearbeitet
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 28.07.05 10:13 
Eine Case-Struktur ist die Lösung.
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Do 28.07.05 10:17 
Seit wann geht case mit nicht-ordinalen Typen?
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 28.07.05 10:25 
Gar nicht.
Ich dachte du hättest das Set für die If-Abfrage und weist dann die Strings zu. Das war leider nicht klar. Du machst es demnach umgekehrt. Du hast also Strings und musst die den Wert aus dem Set zuweisen.
Da hast du wohl Pech gehabt. :mrgreen:
IMHO gibt es keine andere Möglichkeit als if-then-else.
Eventull noch mit einem Array of Record... .
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Do 28.07.05 10:39 
Hmm, man könnte ja eigentlich die Set-Elemente in einen integer casten und dann in einer Konverter-Klasse die Zuordnungen speichern. Beim Herausholen muss man es halt zurück ins Set casten. Ist aber schon ein Hack.
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 28.07.05 10:52 
user profile iconSpaceguide hat folgendes geschrieben:
Hmm, man könnte ja eigentlich die Set-Elemente in einen integer casten...

Du nimmst doch die Set-Elemente für die Bedingung? Falls ja: Das ist ordinal.
Spaceguide Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Do 28.07.05 11:02 
Da hab ich ja nichts davon. Ich mein eher was in der Richtung:

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:
type
 TSetLinkerElement = class
  private
   FName : string;
   FValue : integer;
  public
   constructor Create(const aName : string; aValue : integer);
   property Name : string read FName;
   property Value : integer read FValue default 0;
 end;

 TSetLinker = class(TList)
  private
   function GetElement(i : integer) : TSetLinkerElement;

  public
   procedure Link(const aName : string; aValue : integer);
   destructor Destroy; override;

   function GetName(aValue : integer) : string;
   function GetValue(const aName : string) : integer;

   property Element[i : integer] : TSetLinkerElement read GetElement;
 end;


Dann kann ich elegant einmal linken mit

SetLinker.Link('MeinTypA',integer(myA));

und danach mit

TMySet(SetLinker.GetValue('MeinTypA')) => myA

bzw.

SetLinker.GetName(integer(myA)) => 'MeinTypA'

konvertieren. Das spart 50% Tipparbeit und ist wesentlich leichter zu warten. Zudem kann man bei grossen Sets mittels Hashtable noch Performanz herausholen.
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 28.07.05 11:11 
Wenn Du für die Strings die gleichen Namen wie im Source haben willst, kannst Du in der OIncProcs.pas vom Projekt Omorphia die Funktion Function SetValueToStr(AValue: Integer; ATypeInfo: PTypeInfo; Brackets: Boolean): String; nutzen. Aufgerufen wird diese dann z.B. so hier: SetStr := SetValueToStr(Ord(DeineSetVar), TypeInfo(TDeinSetTypr), True); (Ereugt ein Set, so wie der Objekt-Inspektor von Delphi)

_________________
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.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Do 28.07.05 11:38 
user profile iconjasocul hat folgendes geschrieben:
Ich dachte du hättest das Set für die If-Abfrage und weist dann die Strings zu. Das war leider nicht klar. Du machst es demnach umgekehrt. Du hast also Strings und musst die den Wert aus dem Set zuweisen.
Da hast du wohl Pech gehabt. :mrgreen:
IMHO gibt es keine andere Möglichkeit als if-then-else.
Eventull noch mit einem Array of Record... .

Mit RTTI geht das recht einfach.

ausblenden Delphi-Quelltext
1:
2:
3:
type
  TMySet = (msA, msB, msC, msD);
  TMySets = set of TMySet;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function StrToSet(const S: array of String): TMySets;
var
  I: Integer;
begin

  Result := [];
  for I := Low(S) to High(S) do
    Result := Result + [TMySet(GetEnumValue(TypeInfo(TMySet), S[I]))];

end;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
var
  X: TMySets;
begin

  X := StrToSet(['msA', 'msD']);
  if msD in X then
    ShowMessage('msD ist in X');
end;



Edit: \\
ausblenden Delphi-Quelltext
1:
2:
uses
  TypInfo;

_________________
Ciao, Sprint.
Klabautermann
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Veteran
Beiträge: 6366
Erhaltene Danke: 60

Windows 7, Ubuntu
Delphi 7 Prof.
BeitragVerfasst: Do 28.07.05 11:59 
Hallo Leute,

warum so umständlich, der erste Ansatz von Spaceguide war doch gar nicht so schlecht.

Lediglich die Konstantendefinition muss ein wenig ausgearbeitet werden.

Setzen wir also folgendes vorraus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
type 
  TMySet = (msA,msB,msC,msD);
const
  MyString : array[tMySet] of string[3] = ('myA','myB','myC','myD');
var
  MySet : tMySet;


Dann kann man mit
ausblenden Delphi-Quelltext
1:
Label1.Cation := MyString[MySet];					

den der Variable entsprechenden String ausgeben.

Allerdings möchte ich darauf hinweisen, das die Typbezeichniung TMySet sehr ungünstig ist, da es sich hier um keine Menge sondern um einen Aufzählungstypen handelt.

//Edit: Wodurch auch der Titel deses Themas irreführend wird.

Gruß
Klabautermann