哦,没有全局变量 [英] Oh No a Global Variable

查看:60
本文介绍了哦,没有全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是一个Win表单。


目前我有几个相同的连接字符串

my应用。全局变量是这种情况的最佳选择吗?如果

那么,制作全局字符串变量的代码是什么,以及我把它放在哪里,

(在命名空间之前,就在它之下)??? ??


This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If
so, what is the code to make a global string variable, and where do I put it,
(before the namespace, of just under it)?????

推荐答案

你不能在类之外有变量。尝试一下,你会得到一个编译

错误(我刚试过)。我建议有一个配置文件存储你的

连接字符串,也许是一个单独的设置类负责检索所有设置的
。这样你就可以确定整个应用程序

使用相同的值,而你只能在一个地方检索它们。


-

谢谢

Wayne Sepega

Jacksonville,Fl

企业库配置控制台模块生成器
http://workspaces.gotdotnet.com/elccmg


当一个男人和一个漂亮的女孩坐在一起一小时,这似乎是一分钟。但是

让他坐在热炉上一分钟,它比任何一小时都要长。

那是'相对论'。 - 阿尔伯特爱因斯坦


" Mike L" <钙*** @ nospam.nospam>在留言中写道

news:13 ********************************** @ microsof t.com ...
You can''t have variables outside of a class. Try it and you''ll get a compile
error (I just tried). I would suggest having a config file that stores your
connection string, and maybe a singleton settings class that is responsible
for retrieving all of your settings. This way you are sure your entire app
is using the same values, and you only retrieve them in one place.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it''s longer than any hour.
That''s relativity." - Albert Einstein

"Mike L" <Ca***@nospam.nospam> wrote in message
news:13**********************************@microsof t.com...

这是一个Win表单。

目前我有几个相同的连接字符串
我的应用程序。全局变量是这种情况的最佳选择吗?
如果
那么,创建全局字符串变量的代码是什么,我在哪里放置它,
(之前)在它下面的命名空间)?????

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation?
If
so, what is the code to make a global string variable, and where do I put
it,
(before the namespace, of just under it)?????



" Mike L" <钙*** @ nospam.nospam> écritdansle message de news:
13 ********************************** @ microsoft.com ...
"Mike L" <Ca***@nospam.nospam> a écrit dans le message de news:
13**********************************@microsoft.com...
目前,我的应用程序中有几个相同的连接字符串。全局变量是这种情况的最佳选择吗?
如果是这样,制作全局字符串变量的代码是什么,我在哪里放置
呢(在命名空间之前,在其下面)?????
Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If so, what is the code to make a global string variable, and where do I put it, (before the namespace, of just under it)?????




通常的解决方法是使用静态类静态方法或

属性返回必要的全局属性。值。


静态类设置

{

公共静态字符串MyValue

{

get

{

return" MyValue";

}

}

}


那么你就可以这样使用它:


{

string user = Settings.MyValue;

...

}


Joanna


- -

Joanna Carter

顾问软件工程师



The usual solultion for this is to have a static class of static methods or
properties that return the necessary "global" values.

static class Settings
{
public static string MyValue
{
get
{
return "MyValue";
}
}
}

Then you can use it like this :

{
string user = Settings.MyValue;
...
}

Joanna

--
Joanna Carter
Consultant Software Engineer


我添加了一个配置文件,但它看起来像xml代码。这对我的胜利是否有效?

表格申请?


" Wayne"写道:
I added a config file but it looks like xml code. Will this work for my win
form application?

"Wayne" wrote:
你不能在课外有变量。尝试一下,你会得到一个编译
错误(我刚试过)。我建议有一个存储你的
连接字符串的配置文件,也许是一个负责检索所有设置的单例设置类。通过这种方式,您可以确保整个应用程序使用相同的值,并且只能在一个地方检索它们。

-
谢谢
Wayne Sepega
Jacksonville,Fl
企业库配置控制台模块生成器
http://workspaces.gotdotnet.com/elccmg

当一个男人和一个漂亮的女孩坐在一起一小时,这似乎是一分钟。但是让他坐在炉子上一分钟,这比任何一小时都要长。
这就是相对论。 - 阿尔伯特爱因斯坦

迈克L <钙*** @ nospam.nospam>在消息中写道
新闻:13 ********************************** @ microsof t.com。 ..
You can''t have variables outside of a class. Try it and you''ll get a compile
error (I just tried). I would suggest having a config file that stores your
connection string, and maybe a singleton settings class that is responsible
for retrieving all of your settings. This way you are sure your entire app
is using the same values, and you only retrieve them in one place.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it''s longer than any hour.
That''s relativity." - Albert Einstein

"Mike L" <Ca***@nospam.nospam> wrote in message
news:13**********************************@microsof t.com...

这是一个Win表单。

目前我的几个连接字符串在我的应用程序中完全相同。全局变量是这种情况的最佳选择吗?
如果
那么,创建全局字符串变量的代码是什么,我在哪里放置它,
(之前)在它下面的命名空间)?????

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation?
If
so, what is the code to make a global string variable, and where do I put
it,
(before the namespace, of just under it)?????




这篇关于哦,没有全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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