且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在.NET应用程序中定义“全局”变量?

更新时间:2023-11-25 07:54:58

亲爱的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" &LT的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/