简体   繁体   中英

C# class variable initialization

I want to declare and initialize a string variable that is local to a class but can be accessed by all functions of the class. Fyi, this is an app for a gui which will be making use of several text files in folders. I am trying to set a string variable containing the project directory path so it can be accessed by all the functions within this class.

I have provided an portion of my code including the function that sets the path along with a function that uses the string variable when set.

public class Program
{   
    private string DirectoryPath;

    public static void Main()
    {
        setPaths();
        SetGroundTempArray();
    }

    public static void setPaths()
    {
        DirectoryPath = Directory.GetCurrentDirectory();
    }

    public static void SetGroundTempArray()
    {
        string groundtempfile = "\\groundtemp.txt";
        string groundtempdir = "\\Text Files";
        string groundtempFP = DirectoryPath + groundtempdir + groundtempfile;
    }
}

Your code won't compile.
You should declare the DirectoryPath class field as static:

private static string DirectoryPath;

So you are currently on the right track. In C# we call them Fields

Fields typically store the data that must be accessible to more than one class method and must be stored for longer than the lifetime of any single method

In your case private string DirectoryPath; is a field. And you are following the good practice of making it private .

Also as noted you have all methods as static so you need to make the Field variable static as well to access it

private static string DirectoryPath;

A field can optionally be declared static. This makes the field available to callers at any time, even if no instance of the class exists.

As given in your example, you have done it right as the functionality you want. But you may need to learn more about the usage of static keywords in C#. You can learn more about it at MSDN

Here's a intercept about your code, that might clear your concept.
As DirectoryPath is used by a static method in your progream, you must need to declare this variable as static also because of, setPaths method is used in static Main, and Main is the topmost level static class that don't requires the instance to be created of the Program Class. and that is why, Main method would require all the methods or variable or fields is being used inside the method must be declared as static.

public class Program
{   
    private static string DirectoryPath;

    public static void Main()
    {
        setPaths();
        SetGroundTempArray();
    }

    public static void setPaths()
    {
        DirectoryPath = Directory.GetCurrentDirectory();
    }

    public static void SetGroundTempArray()
    {
        string groundtempfile = "\\groundtemp.txt";
        string groundtempdir = "\\Text Files";
        string groundtempFP = DirectoryPath + groundtempdir + groundtempfile;
    }
}

Add static in front of string.

 class Program
{
    //add static in front of string
    static String a = "Hello";


    static void Main(string[] args)
    {
        h();
        Console.ReadKey();
    }

    public static void h()
    {
        Console.WriteLine(a);
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM