如何在.NET应用程序中定义“全局”变量? [英] How to define 'Global' variables in a .NET app?

查看:112
本文介绍了如何在.NET应用程序中定义“全局”变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows应用中的多个表单之间共享变量。在哪里

我声明变量?


我提出了一种方法,即将其声明为MainForm的成员,

然后使用

((MainForm)Application.OpenForms [0])访问它。< MyVariableName> ;.


是否有更优雅还是更好的方式?顺便说一下,我想分享的变量

是一个开放的SqlConnection。


谢谢!

解决方案

亲爱的John,

在多个

表单之间分享变量的方法很少,但我最喜欢的是我制作了一个Singleton

作为应用程序会话的类。


公共类ApplicationSession:Hashtable

{

private static ApplicationSession instance = new

ApplicationSession();


private ApplicationSession()

{

}


public static ApplicationSession getInstance()

{

返回实例;

}


public override void添加(对象键,对象值)

{

if(this.Contains(key)== true )

{

this.Remove(key);

}

base.Add(key,value) ;

}

}


它适用于m y项目。


问候,

Naveed Ahmad Bajwa
http://bajoo.blogspot.com/


一个不错的解决方案,我的建议是这个具体的问题是不要将b $ b保持与Sql服务器的开放连接。它将可怕地扩展。打开

conn,做东西,关闭conn,重复。 ;)


-

Robert Jeppesen

Durius
http://www.durius.com/

" Bajoo" <的Na ********** @ gmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...

亲爱的John,
在多种
表单之间分享变量的方法很少,但我最喜欢的是我制作了一个Singleton类,它可以作为一个应用程序会话。公共类ApplicationSession:Hashtable
{private static ApplicationSession instance = new
ApplicationSession();

私有ApplicationSession()
{
}

public static ApplicationSession getInstance()
{
返回实例;
}

公共覆盖void添加(对象键,对象值)
{
if(this.Contains(key)== true)
{
this.Remove(key);
}
base.Add(key,value);
}


它适用于我的项目。

问候, Naveed Ahmad Bajwa
http:// ba joo.blogspot.com/



是的,罗伯特你是绝对正确的。连接应该关闭。我是

提到如何在表单之间共享变量/对象。 John我会

建议你使用某种块/框架,比如

Microsoft.ApplicationBlock.Data。非常好,它为你处理了很多。


问候,

Naveed Ahmad Bajwa
http://bajoo.blogspot.com/


I want to share a variable between multiple forms in a Windows app. Where do
I declare the variable ?

I came up with one way that is to declare it as a member of the MainForm,
and then access it using
((MainForm)Application.OpenForms[0]).<MyVariableName>.

Is there a more elegant or better way? Btw, the variable I''d like to share
is an open SqlConnection.

Thanks!

解决方案

Dear John,
There are few ways to share variables between multiple
forms but what i liked the most is that i made a Singleton class which
works as a Application session.

public class ApplicationSession : Hashtable
{
private static ApplicationSession instance = new
ApplicationSession();

private ApplicationSession()
{
}

public static ApplicationSession getInstance()
{
return instance;
}

public override void Add(object key, object value)
{
if (this.Contains(key) == true)
{
this.Remove(key);
}
base.Add(key, value);
}
}

It worked fine for my Project.

Regards,
Naveed Ahmad Bajwa
http://bajoo.blogspot.com/


A nice solution, byt my advice for this specific problem would be to not
keep an open connection to the Sql server. It will scale horribly. Open
conn, do stuff, Close conn, repeat. ;)

--
Robert Jeppesen
Durius
http://www.durius.com/

"Bajoo" <Na**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Dear John,
There are few ways to share variables between multiple
forms but what i liked the most is that i made a Singleton class which
works as a Application session.

public class ApplicationSession : Hashtable
{
private static ApplicationSession instance = new
ApplicationSession();

private ApplicationSession()
{
}

public static ApplicationSession getInstance()
{
return instance;
}

public override void Add(object key, object value)
{
if (this.Contains(key) == true)
{
this.Remove(key);
}
base.Add(key, value);
}
}

It worked fine for my Project.

Regards,
Naveed Ahmad Bajwa
http://bajoo.blogspot.com/



Yes robert you are Absolutly right. Connection should be closed. I was
mentioning how to share variables/Objects between forms. John I''ll
advice you to use some kind of Block/Framework like
Microsoft.ApplicationBlock.Data. Its really good ,It handles a lot for
you.

Regards,
Naveed Ahmad Bajwa
http://bajoo.blogspot.com/


这篇关于如何在.NET应用程序中定义“全局”变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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