如何在另一个文件中使用一个类的全局常量 [英] How do I use global constants from one class in another file
本文介绍了如何在另一个文件中使用一个类的全局常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在c#中编写了一个类,其中我声明了一些常量,现在我想在另一个类中使用这些常量。这些类位于同一个项目文件夹中。但是当我尝试访问常量时,它报告错误
解决方案
尝试这样
public class ClassA
{
< span class =code-keyword> public const string DataA = 某个值; // 确保它是公共修饰符,以便可以从其他类访问它
}
public classB
{
public < span class =code-keyword> void SomeMethod()
{
string data = ClassA.DataA; // 某个值
}
}
参考 const(C#参考) [ ^ ]
I wrote a class in c# in which I declared some constants and now I want to use these constants in another class . These classes are in same project folder. But when I try to access the constants , it reports error
解决方案
try like this
public class ClassA { public const string DataA = "some value"; // make sure that it is public modifier so that it can be accessed from other class } public classB { public void SomeMethod() { string data = ClassA.DataA; // some value } }
refer const (C# Reference)[^]
这篇关于如何在另一个文件中使用一个类的全局常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文