全局变量访问问题C# [英] Global Variable Access Problem c#
本文介绍了全局变量访问问题C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友..
我正在用C#开发一个项目,并且在dll中声明了所有常量,错误消息等(使用classlibrary).
现在我想将所有这些都访问到我的项目中,为此我声明了名称空间,然后创建了一个类的对象,但是我无法访问变量.
我不知道该怎么办?还有其他方法可以访问这些变量.
谢谢
斯瓦普尼尔
删除了TextSpeak并改进了语法[/编辑]
hi friends..
I am working on a project in c#, and I declared all constant, error msgs etc in a dll.(using classlibrary).
Now I want to access all of them into my project.For that I declared the namespace and then I created an object of the class, but I am not able to access the variables.
What can I do as I don''t have any idea? Is there any other way to access these variables.
Thanks
Swapnil
Removed TextSpeak and improved grammar[/Edit]
推荐答案
您是否已在解决方案资源管理器中引用了dll-添加了引用?如果是这样,您是否已在dll中将变量声明为公共变量?
Have you referenced the dll in Solution Explorer - Add Reference ? If so, have you declared the variables public in the dll ?
也许一个解决方案将使用静态变量(甚至在静态类中),
或使用只读"关键字.比较一下:
Maybe a soltion would be using static variables (maybe even in a static class),
or use "readonly" keyword. Compare this:
MyConstants mc = new MyConstants();
string strDefaultInstance = mc.strDEFAULT_INSTANCE;
string strDefault = MyConstants.strDEFAULT;
public class MyConstants
{
public const string strDEFAULT = "Default";
public readonly string strDEFAULT_INSTANCE = "Default";
}
.NET中没有全局变量的概念(谢天谢地!最后!).
您可能需要了解单人模式的工作方式:
http://en.wikipedia.org/wiki/Singleton_pattern [ http://csharpindepth.com/Articles/General/Singleton.aspx [
There is no a notion of global variable in .NET (thank goodness! finally!).
You may need to know how the singleton pattern works:
http://en.wikipedia.org/wiki/Singleton_pattern[^].
Good .NET implementation of it is explained here:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].
—SA
这篇关于全局变量访问问题C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文