Autor Beitrag
GerhardS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Mo 04.05.15 13:22 
Hallo,
bisher habe ich nur mit ObjectPascal gearbeitet, jetzt will ich mich in C# einarbeiten. Auf den ersten Blick sieht alles ziemlich vertraut aus, auf den zweiten wird's schon schwieriger.
Hier mein Code:
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:
38:
39:
40:
41:
42:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Einstellung 
        { 
            public string Wert; 
            public string Schluessel; 
            public Einstellung (string Schluessel, string Wert)
            {
                this.Schluessel = Schluessel;
                this.Wert = Wert;
            }
        }
        public void CreateList()
        { 
           List<Einstellung> einstellungen = new List<Einstellung>();
           einstellungen.Add(new Einstellung("Erzeugung""04.05.2015"));
           einstellungen.Add(new Einstellung("User""Max"));
           einstellungen.Add(new Einstellung("PW""pwMax"));
        }
        public void FillListBox()
        {
            foreach (Einstellung einst in einstellungen)  //hier beschwert sich der Compiler:"Der Name 'einstellungen' ist im aktuellen Kontext nicht vorhanden."             
            {
               listBox1.Items.Add(einst.Schluessel, einst.Wert);               
            }
        } 

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("Erster Eintrag");
            CreateList();
            FillListBox();
        }
    }
}

Wie bekomme ich es hin, dass die Definition der Liste aus CreateList() auch in FillListBox() sichtbar ist?

Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 04.05.15 13:38 
Hallo,

auch in ObjectPascal (Delphi) gibt es die Sichtbarkeit von Variablen - das solltest du also kennen. ;-)
Du mußt diese als Membervariable (innerhalb der Klasse) erstellen:
ausblenden C#-Quelltext
1:
List<Einstellung> einstellungen; // optional noch intialisieren: = new List<Einstellung>();					
GerhardS Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Mo 04.05.15 15:22 
In Delphi würde ich so vorgehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Create(Sender: TObject);
var MyList: TList;
begin
ListBox1.Items.Add("Erster Eintrag");
MyList:= TList.Create;
FillListBox(MyList);
end;


Meinst du das?
In meinem Code steht ja schon:
ausblenden C#-Quelltext
1:
List<Einstellung> einstellungen = new List<Einstellung>();					


Moderiert von user profile iconTh69: Delphi-Tags hinzugefügt
Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 04.05.15 15:29 
In deinem Delphi Code übergibst du die Liste auch an die FillListBox Methode wenn du das in C# genauso machen willst dann musst du es auch genauso machen ;)
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 04.05.15 15:30