2016-10-18 1 views
-1

Ich möchte diese Methode https://stackoverflow.com/a/11967357/5425608 verwenden, um eine Datei zu erstellen. Var darf jedoch nur innerhalb einer Methode https://stackoverflow.com/a/5337470/5425608 verwendet werden. Wie konvertiere ich die Namen der Pfade in Klassenvariablen, auf die andere Methoden innerhalb der Klasse zugreifen sollen.Einen Pfad zu Dateien festlegen

Zum Beispiel befindet sich "labelPath" im folgenden Code nicht im Bereich von var labelPath der setPaths() -Methode. Ich habe auch andere Methoden, die "Pfad" und "Objektpfad" verwenden und diese Variablen sind nicht im Bereich dieser Methoden.

Jede Hilfe würde sehr geschätzt werden.

public Form2() 
    { 
     InitializeComponent(); 
     setPaths(); 
     current = this; 
     instantiateNumUpDown(); 
     System.IO.File.WriteAllText(labelPath, " "); 

    } 

    public static void setPaths() 
    { 
     var systemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); 

     var path = Path.Combine(systemPath, "TextDynamic.txt"); 
     var labelPath = Path.Combine(systemPath, "currentLabel.txt"); 
     var objectPath = Path.Combine(systemPath, "object.txt"); 

    } 
+0

machen Sie einfach 'var' zu' string' –

Antwort

0

Sie können dies nicht tun, da var keinen Typ selbst ist, wird es die Art des Wertes auf der rechten Seite der Zuweisung.

Stattdessen können Sie den Variablentyp als String definieren und erklären unter Klasse

public class A 
{ 
     private static string systemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); 
     private string path = Path.Combine(systemPath, "TextDynamic.txt"); 
     private string labelPath = Path.Combine(systemPath, "currentLabel.txt"); 
     private string objectPath = Path.Combine(systemPath, "object.txt"); 
} 
0

Try this:

public class Form2 
{ 
    private static string labelPath; 
    private static string objectPath; 
    private static string path; 
    public Form2() 
    { 
     InitializeComponent(); 
     setPaths(); 
     current = this; 
     instantiateNumUpDown(); 
     System.IO.File.WriteAllText(Form2.labelPath, " "); 

    } 

    public static void setPaths() 
    { 
     var systemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); 

     path = Path.Combine(systemPath, "TextDynamic.txt"); 
     labelPath = Path.Combine(systemPath, "currentLabel.txt"); 
     objectPath = Path.Combine(systemPath, "object.txt"); 

    }  
} 
0

Weil Ihre Variable "path" "labelPath" "objectPath" alle aus From2's Construction-Methode, um es zu beheben, sollten Sie wie folgt vorgehen:

public Form2() 
{ 
    InitializeComponent(); 

    string path = string.empty; 
    string labelPath = string.empty; 
    string objectPath = string.empty; 

    setPaths(ref path, ref labelPath, ref objectPath); 

    current = this; 
    instantiateNumUpDown(); 
    System.IO.File.WriteAllText(labelPath, " "); 

} 

public static void setPaths(ref string path, ref string labelPath, ref string objectPath) 
{ 
    var systemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); 

    path = Path.Combine(systemPath, "TextDynamic.txt"); 
    labelPath = Path.Combine(systemPath, "currentLabel.txt"); 
    objectPath = Path.Combine(systemPath, "object.txt"); 

} 
Verwandte Themen