Autor Beitrag
hanspeter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 44

Windows

BeitragVerfasst: Sa 14.10.06 11:10 
Hallo,
ich stelle gerade ein Projekt von Delphi nach C# um und bin wohl noch ein bischen in der Delphi-Syntax verfangen.

Ich habe globale Tabellen mit Initialisierungsdaten bisher als Array[1..n] of record global vereinbart.
Gut das muß jetzt in eine Klasse. Als static ja kein Problem.
Wie aber kann ich ein Array von Structuren zur Compilerzeit initialisieren?

Hier ein Codebeispiel aus Delphi.


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
type
TPruefungsArt = record
  Art : string;                      // Art der Prüfung
  ID  : Integer;                     // interne Prüfungs ID
  T   : Array [1..5of String;      // Eingabefeld Titel
  V   : boolean;                     // Wahr in der Auswahlbox visible
  A   : boolean;                     // Wahr Zeitfehlerautomatik
  N   : boolean;                     // Sortierung Note aufsteigend
  F   : boolean;                     // Sortierung Fehler absteigend
  Z   : boolean;                     // Sortierung Zeit
end;


const
  PrfgAnz = 27;
  Pruefungsart : Array[1..PrfgAnz] of TPruefungsart =
 (Art: 'Mannschaftsspr. mit/ohne Stechen';   ID: ceMannschaftspr; T:('Fehler','Zeit','','','');      V:true; A: true;  N: false; F: true;  Z: true),
 (Art: 'Mannschaftsspr. 2 Uml. Stechen';     ID: ceMannschaft2US; T:('Fehler','Zeit','','','');      V:true; A: true;  N: false; F: true;  Z: true),
    
...

Für einen Tip dankbar.

Gruß Peter

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

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 14.10.06 11:44 
Hallo!

Würde ich so machen:
ausblenden volle Höhe C#-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:
35:
36:
37:
    class Program
    {
        private static readonly Prüfungsart[] prüfungsart = {
            new Prüfungsart("Mannschaftsspr. mit/ohne Stechen"1),
            new Prüfungsart("Mannschaftsspr. 2 Uml. Stechen"2)};

        static void Main(string[] args)
        {
            foreach (Prüfungsart p in prüfungsart)
                Console.WriteLine(p.Art);

            Console.ReadLine();
        }
    }

    public struct Prüfungsart
    {
        private string art;

        public string Art
        {
            get { return art; }
        }

        private int id;

        public int Id
        {
            get { return id; }
        }

        public Prüfungsart(string Art, int Id)
        {
            art = Art;
            id = Id;
        }
    }


Fehlen natürlich noch ein paar Properties. Ich bin mir nicht sicher, ob C# diese Inline-Initialisierung wie Delphi kennt, aber gesehen habe ich sie noch nicht.

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
hanspeter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 44

Windows

BeitragVerfasst: Sa 14.10.06 22:02 
user profile iconChristian S. hat folgendes geschrieben:


Fehlen natürlich noch ein paar Properties. Ich bin mir nicht sicher, ob C# diese Inline-Initialisierung wie Delphi kennt, aber gesehen habe ich sie noch nicht.

Grüße
Christian


Ja so habe ich es jetzt programmiert. Da erfolgt aber die Initialisierung zur Laufzeit.
Aber danke Du hast mich auf den Kern der Frage gebracht.

Also was ich eigentlich wissen will:

Kann man in C# zur Entwurfszeit Felder und Felder von Strukturen Inline initialisieren?

Gruß Peter
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 15.10.06 12:41 
Wie stellst du dir das vor ;) ? Wenn die Structure auch nur halbwegs den OOP-Regeln entspricht, hast du keinen Zugriff auf die Felder. Und Properties lassen sich zur Compiletime eher schlecht ansprechen.