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: Mo 30.05.16 09:32 
Moin Leute,

ich wollte ausprobieren, eine string-Variable als Referenz eines Feldes zu 'deklarieren'.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
public string DefaultFolderPath { get; set; }

private void buttonSearchSaveFolder_Click(object sender,EventArgs e)
{
    ref string path = this.DefaultFolderPath;   //so geht es nicht
    ...
}

Frage: Welche Alternativen zu dieser ref-Synthax oder zum ref-Schlüsselwort gibt es/wären am besten für diesen Fall?

Gruß Julian
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Mo 30.05.16 09:42 
Gibt's nicht und macht auch keinen Sinn.

Was hast Du denn vor?
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: Mo 30.05.16 10:03 
Hi Palladin,

ich habe 4 Textboxen, Buttons und 4 Felder dieser Art. Bisher hatte ich dafür 4 Button-Click-Event-Methoden, die praktisch alle das gleiche machen, nur das halt verschiedene Controls usw verwendet werden. Also dacht ich mir, das kann man optimieren und das in einer Methode zusammenfassen.
Soweit war ich bis jetzt:
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:
43:
44:
45:
private void buttonSearchSaveFolder_Click(object sender,EventArgs e)
{
    if (sender != null)
    {
        Button button = sender as Button;
        TextBox txtBox = null
        ref string path;

        //Zuweisen der richtigen Textbox und des richtigen Path-Feldes
        if (button.Name == "buttonSearchSaveFolder")
        {
            txtBox = this.textBoxSaveFolderPath;
            path = this.DefaultFolderPath;
        }
        else if (button.Name == "buttonClaimSearchSaveFolder")
        {
            txtBox = this.textBoxClaimPath;
            path = this.ClaimFolderPath;
        }
        else if (button.Name == "buttonMasterSearchSaveFolder")
        {
            txtBox = this.textBoxMasterPath;
            path = this.MasterFolderPath;
        }
        else if (button.Name == "buttonSchittAGSearchSaveFolder")
        {
            txtBox = this.textBoxSchnittAGPath;
            path = this.SchnittAGPath;
        }
        else 
        {
            txtBox = new TextBox();
            path = null;
        }

        this.folderBrowserDialog1.SelectedPath = txtBox.Text;
        if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            txtBox.Text = this.folderBrowserDialog1.SelectedPath;
            
            if (path != null
                path = this.folderBrowserDialog1.SelectedPath;
        }
    }
}

Also ich wollt mir einfach durch das Referenzieren von path eine 2. If-Abfrage sparen, um dann über den Button das richtige Feld herauszufinden.
Gibt es da keine Möglichkeit, wie das ungefähr funktionieren könnte?
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 30.05.16 10:39 
Es ist immer guter Stil EventCode von eigentlichem funktionalen Code zu trennen. Hier schreit das gerade geradezu danach ;)
Lagere den allgemeinen Code in eine Methode aus

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
private static string ShowFolderBrowserDialog(string path) // ist jetzt statisch und kann man in irgendeine Helper Klasse auslagern 

    using (var dlg = new FolderBrowserDialog())
    { 
        dlg.SelectedPath = path;  
        if (dlg.ShowDialog() == DialogResult.OK)
            return this.folderBrowserDialog1.SelectedPath;
        else
            return path;
    }
}

// einer der 4 Events
private void buttonSearchSaveFolder_Click(object sender,EventArgs e)
{
    DefaultFolderPath = ShowFolderBrowserDialog(this.textBoxSaveFolderPath);
}
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: Mo 30.05.16 10:44 
Ok ja sowas hatte ich auch im Kopf, hatte mich nur zulang an dieser ref-Sache aufgehalten. Trotzdem danke, wieder was gelernt!