Autor Beitrag
ots_sharp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Mo 07.05.12 15:48 
Hallo,
aus einem Ereignis möchte ich ein Array zusammenbasteln. Irgendwie verliert das Array bei der Übergabe seine Inhalte. Bestimmt ist die Lösung wieder ganz einfach. Vielleicht seht ihr den Fehler?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
private void btnSaveSettings_Click(object sender, EventArgs e)
        {
            SWF.Control[] controls = new SWF.ComboBox[]{};
            GetArrayOfBoxes(controls);
        }
private GetArrayOfBoxes(SWF.Control[] control)
        {
            control = new SWF.Control[] { this.Box1, this.Box2};

        }


Gruß Stefan
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 07.05.12 16:33 
Hallo,

so legst du innerhalb deiner Methode nur eine lokale Instanz an, sieh dir mal die Schlüsselwörter ref und out an: Modifizierer für out-Parameter (Titel ist zwar falsch übersetzt [Original: "out parameter modifier"], aber ich denke mal, daß du lieber deutsch als englisch liest.)

Oder gib einfach das Control-Array als Rückgabewert deiner Methode an...
ots_sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Di 08.05.12 07:23 
Die Lösung: Übergabe mit out-Parameter.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
private void btnSaveSettings_Click(object sender, EventArgs e)
        {
            SWF.Control[] controls = new SWF.ComboBox[]{};

            GetArrayOfBoxes(out controls);
        }

private GetArrayOfBoxes(out SWF.Control[] control)
        {
            control = new SWF.Control[] { this.Box1, this.Box2};

}
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 08.05.12 09:52 
Du brauchst das Array in deiner btnSaveSettings_Click()-Methode aber nicht initialisieren (new SWF.ComboBox[]{}), da es ja von der GetArrayOfBoxes wieder überschrieben wird (bei einem out-Parameter ist es zwingend notwendig, daß dieser Parameter in der Methode initialisiert wird).
ujr
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 102
Erhaltene Danke: 12



BeitragVerfasst: Di 08.05.12 15:11 
Hallo,

es ist hier allerdings ungewöhnlich, mit einem out-Parameter zu arbeiten, wenn man die Möglichkeit hat, den Rückgabewert zu verwenden. Gerade bei "Get..." wäre das Array als Rückgabe sinnvoller.