Autor Beitrag
erfahrener Neuling
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 16.03.16 14:34 
Hallo,

wurde hier und da warscheinlich schon öfters gefragt, aber hab grad nichts spezifisches gefunden (oder war zu doof dafür). Also:
Ich habe 2 Forms, Form1 soll manchmal (nicht immer!) eine List<string>-Variable übergeben.

Wie deklariere ich jetzt in Form2 die List-Variable, an die es übergeben werden soll?
Und wie mache ich das mit get/set, wenn nicht klar ist, ob etwas übergeben wurde?

hier der code:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
//form1
public List<string> databaseTextboxData = new List<string>();

private void irgendeineMethode()
{
    datbaseTextboxData = ...      //wird mit anderer Liste gefüllt

    Form2 form2 = new Form2();
    form2.oldTextboxText = databaseTextboxData;         //die Liste databaseTextboxData wird an die oldTextboxText-Liste übergeben
    if (form2.ShowDialog(this) == DialogResult.OK) 
        //doAnything
}

//Form2
public List<string> oldTextboxText ???        //was kommt dann
???

private void nochIrgendeineMethode()
{
    if(oldTextBoxText != null)
        //doAnything
}


mir geht es vorallem um das get/set von oldTextboxText.

Ich denke, ihr könnt mir da schnell und einfach helfen, deswegen danke schonmal im vorraus!


Moderiert von user profile iconChristian S.: Topic aus WinForms verschoben am Mi 16.03.2016 um 13:44
erfahrener Neuling Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 16.03.16 14:46 
ok hat sich erledigt..

ich habe davor immer nur eine exception gekriegt, weil ich den Liste-Übergeben-Befehl eine Zeile zu spät gesetzt hatte

Lösung für die Deklarierung war:
ausblenden C#-Quelltext
1:
2:
//form 2
public List<string> oldTextboxText;

mehr nicht (initialisierung passiert ja dann schon in Form1)
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4701
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 16.03.16 16:06 
Öffentliche Felder zu benutzen ist etwas verpönt. Man nimmt eigentlich immer Properties. Also eher public List<string> OldTextboxText { get; set; }.
erfahrener Neuling Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 16.03.16 16:49 
@Ralf Jansen
Zitat:
Also eher public List<string> OldTextboxText { get; set; }.

Wieso, ist doch dann auch eine öffentliche Liste oder wie meinst du das?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4701
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 16.03.16 21:25 
Es funktioniert ja auch so wie du es gemacht hast. Was ich ansprach ist eine Frage von Stil/Best Practises.
Der relevante Teil aus den Design Guidelines Field Design.