C#类变量初始化
我想声明并初始化一个字符串变量,该变量是类的局部变量,但可以被类的所有函数访问。仅供参考,这是一个gui应用程序,将使用文件夹中的多个文本文件。我试图设置一个包含项目目录路径的字符串变量,以便该类中的所有函数都可以访问它 我提供了部分代码,包括设置路径的函数以及设置时使用字符串变量的函数C#类变量初始化,c#,private,declaration,public,C#,Private,Declaration,Public,我想声明并初始化一个字符串变量,该变量是类的局部变量,但可以被类的所有函数访问。仅供参考,这是一个gui应用程序,将使用文件夹中的多个文本文件。我试图设置一个包含项目目录路径的字符串变量,以便该类中的所有函数都可以访问它 我提供了部分代码,包括设置路径的函数以及设置时使用字符串变量的函数 public class Program { private string DirectoryPath; public static void Main() {
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;
}
}
所以你现在走在正确的轨道上。在C#中,我们称他们为 字段通常存储多个用户必须访问的数据 一个类方法,并且必须存储超过 任何单一方法 在您的例子中,
私有字符串DirectoryPath代码>是一个字段。您正在遵循将其设置为私有的良好实践
另外,如前所述,所有方法都是static
,因此需要将字段变量设置为static
,并访问它
private static string DirectoryPath;
可以选择将字段声明为静态。这使得这个领域
随时可供调用方使用,即使没有该类的实例
存在
所以你现在走在正确的轨道上。在C#中,我们称他们为
字段通常存储多个用户必须访问的数据
一个类方法,并且必须存储超过
任何单一方法
在您的例子中,私有字符串DirectoryPath代码>是一个字段。您正在遵循将其设置为私有的良好实践
另外,如前所述,所有方法都是static
,因此需要将字段变量设置为static
,并访问它
private static string DirectoryPath;
可以选择将字段声明为静态。这使得这个领域
随时可供调用方使用,即使没有该类的实例
存在
您的代码无法编译。
您应该将DirectoryPath
类字段声明为静态:
private static string DirectoryPath;
您的代码无法编译。
您应该将DirectoryPath
类字段声明为静态:
private static string DirectoryPath;
正如在您的示例中所给出的,您已经按照您想要的功能正确地完成了它。但是您可能需要了解更多关于C#中静态关键字的用法。您可以在以下网站了解更多信息:
这里有一个关于您的代码的截取,这可能会澄清您的概念。
由于程序流中的静态方法使用DirectoryPath
,因此必须将此变量声明为static
,这也是因为,setpath
方法在static Main中使用,Main是最顶层的静态类,不需要创建程序
类的实例。这就是为什么,Main
method要求方法中使用的所有方法、变量或字段都必须声明为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;
}
}
正如在您的示例中所给出的,您已经按照您想要的功能正确地完成了它。但是您可能需要了解更多关于C#中静态关键字的用法。您可以在以下网站了解更多信息:
这里有一个关于您的代码的截取,这可能会澄清您的概念。
由于程序流中的静态方法使用DirectoryPath
,因此必须将此变量声明为static
,这也是因为,setpath
方法在static Main中使用,Main是最顶层的静态类,不需要创建程序
类的实例。这就是为什么,Main
method要求方法中使用的所有方法、变量或字段都必须声明为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;
}
}
在字符串前面添加static
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);
}
}
在字符串前面添加static
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);
}
}
你的问题是什么?你的问题是什么?