在哪里声明公共变量 [英] Where to declare Public variable

查看:95
本文介绍了在哪里声明公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的项目中有一个Public变量。在VB中,它可以在

中声明为标准模块。我在哪里可以用C#做​​什么?

我试着在默认的Program.cs中做这个,我尝试在

me class中添加它。

没有成功


那么,我如何以及在哪里声明项目中任何模块可见的变量?


Esha

I need to have a Public variable in my project. In VB it can be declared in
a standard module. Where can I do it in C# ?
I tried to do it in default class Program.cs and I tried it in an added by
me class.
No success

So, how and where I declare a variable visible by any module in the project?

Esha

推荐答案

esha写道:
esha wrote:

我需要在我的项目中有一个Public变量。在VB中,它可以在

中声明为标准模块。我在哪里可以用C#做​​什么?

我试着在默认的Program.cs中做这个,我尝试在

me class中添加它。

没有成功


那么,我如何以及在哪里声明项目中任何模块可见的变量?
I need to have a Public variable in my project. In VB it can be declared in
a standard module. Where can I do it in C# ?
I tried to do it in default class Program.cs and I tried it in an added by
me class.
No success

So, how and where I declare a variable visible by any module in the project?



快速解决方案:


公共类Globals

{

public SomeClass Varname;

}


可用作Globals.Varname


更好的解决方案:


看单身模式或其他重组方式,

因为第一个不好OOP


Arne

Quick solution:

public class Globals
{
public SomeClass Varname;
}

can be used as Globals.Varname

Better solution:

look at singleton pattern or another way of restructuring,
because the first one is not good OOP

Arne


" Arne Vajh?j" < ar ** @ vajhoej.dkwrote in message

news:UQ%Ng.37831
"Arne Vajh?j" <ar**@vajhoej.dkwrote in message
news:UQ%Ng.37831


_q4.15928@dukeread09 ...


你忘记了静态
_q4.15928@dukeread09...

You forgot the static

public class Globals

{

public static SomeClass Varname;

}
public class Globals
{
public static SomeClass Varname;
}



Michael

Michael


这篇关于在哪里声明公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆