Autor Beitrag
Garby
Hält's aus hier
Beiträge: 4

Win XP
Delphi 2005 Prof, VS 2005 Prof.
BeitragVerfasst: Mo 16.01.06 17:18 
Hallo,

Der Titel ist ein wenig komisch ;)

Ich möchte in C# einen Enum Eintrag mittels eines Klassen Arrays einer Klasse zuordnen.

In Delphi schaut das so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
type
  TEnum = (abButton, abListe);
const
  EnumTrans: array[TEnum] of TClass = (TButton, TList);
Anwendungsbeispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
var abc: TObject;
begin
  abc := TList.Create;
//    abc := TButton.Create(nil);
  try
    if abc is EnumTrans[abListe] then
      ShowMessage('Liste')
    else if abc is EnumTrans[abButton] then
      ShowMessage('Button');
  finally
    abc.Free;
  end// end finally
end;
Wie würdet ihr so was in C# realisieren?

Danke
Walter

_________________
Wenn zwei dasselbe tun, ist es noch lange nicht dasselbe
(Adelphi)
Xqgene
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 20



BeitragVerfasst: Mi 25.01.06 10:12 
hm..ich verstehe nicht ganz, wozu hier Enum überhaupt gut ist, liegt wohl aber an meinen verstaubten Delphi Kenntnissen.

ich würde es einfach so machen:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
object o = new Button();
.....

if (o is Button)
{
  // TODO
}
if (o is ArrayList)
{
  // TODO
}

_________________
There are only 10 types of people in the world:
Those who understand Binary and those who don`t.
Garby Threadstarter
Hält's aus hier
Beiträge: 4

Win XP
Delphi 2005 Prof, VS 2005 Prof.
BeitragVerfasst: Mi 25.01.06 11:04 
Hallo,

Dieses Anwendungsbeispiel zeigt den Nutzen des Enums besser:
ausblenden Delphi-Quelltext
1:
with EnumTrans[abListe].Create(nildo begin...					

_________________
Wenn zwei dasselbe tun, ist es noch lange nicht dasselbe
(Adelphi)
Xqgene
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 20



BeitragVerfasst: Mi 25.01.06 11:51 
wenn es nur um das Speichern der Objekte in einer Liste geht, kann man eine Hashtable nutzen oder noch besser (ab .NET 2.0) eine SortedList

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
        enum MyEnum
        {
            Button,
            Label,
            Form
        }
.....
        SortedList<MyEnum, object> list = new SortedList<MyEnum, object>();
.....
        list[MyEnum.Button] = new Button();
.....
        if (list[MyEnum.Button] is Button)
                MessageBox.Show("OK");


Problem ist das Erzeugen der Objekte a lá

ausblenden Delphi-Quelltext
1:
EnumTrans[abListe].Create(nil)					


so geht es in C# ohne Weiteres nicht.

eine Möglichkeit wäre die Reflection zu nutzen

_________________
There are only 10 types of people in the world:
Those who understand Binary and those who don`t.
Garby Threadstarter
Hält's aus hier
Beiträge: 4

Win XP
Delphi 2005 Prof, VS 2005 Prof.
BeitragVerfasst: Mi 25.01.06 14:53 
OK, Danke.

Ich werde mir halt was anderes ausdenken müssen.

Walter

_________________
Wenn zwei dasselbe tun, ist es noch lange nicht dasselbe
(Adelphi)
Xqgene
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 20



BeitragVerfasst: Mi 25.01.06 15:00 
was willst du denn erreichen?

_________________
There are only 10 types of people in the world:
Those who understand Binary and those who don`t.