c#定义全局对象

shark84 2009-03-05 09:29:17
有一个类,很多地方都有调用.想把这个类做成全局的,请教该怎样写.请看清楚是全局对象 不是全局变量
...全文
1191 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinjianjing 2009-03-05
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090305/08/a4cae23a-b7c9-496c-b540-42882683be7a.html?seed=584068651
高手们帮小弟看看, 我急啊。。。
PandaIT 2009-03-05
  • 打赏
  • 举报
回复
public static
liumj2001 2009-03-05
  • 打赏
  • 举报
回复
clsglo.clientSocket

这样就可以全局访问了。
liumj2001 2009-03-05
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Xml;
using System.Data;


namespace oaelf
{
public static class clsglo
{

public static Socket clientSocket; //客户机SOCKET对像
public static string loginFlag; //登陆标志
public static TcpClient tcpcl = new TcpClient(); //客户TCP联接
public static string strname; //用户姓名
public static string strID; //用户ID
public static string strPws; //用户密码
public static string strinoaURL; //内网OA地址
public static string stroutoaURL; //外网OA地址
public static string strinftpURL; //内网FTP地址
public static string stroutftpURL; //外网FTP地址
public static string strftpuser; //FTP用户名
public static string strftppws; //FTP密码

public static IList<FormMSG> m_List = new List<FormMSG>(); //窗体列表



/// <summary>
/// 将指定datatable转换为其等效的xml流形式。
/// </summary>
/// <param name="table">datatable对象</param>
/// <returns></returns>
public static string toxmlstring(DataTable table)
{
if (table == null)
{
return null;
}


StringBuilder content = new StringBuilder();
StringWriter writer = new StringWriter(content);
XmlTextWriter xmlwriter = new XmlTextWriter(writer);

try
{

DataSet ds = new DataSet();
ds.Tables.Add(table.Copy());
ds.WriteXml(xmlwriter);

return content.ToString();
}
finally
{
writer.Close();
xmlwriter.Close();
}
}

/// <summary>
/// 将指定的xml流转换为其等效的dataset对象。
/// </summary>
/// <param name="xmlstream">xml流</param>
/// <returns></returns>
public static DataSet todataset(string xmlstream)
{
if (xmlstream == null || xmlstream.Trim().Length == 0)
{
return null;
}

StringReader reader = new StringReader(xmlstream);
XmlTextReader xmlreader = new XmlTextReader(reader);
try
{
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
return ds;
}
finally
{
reader.Close();
xmlreader.Close();
}
}
}
}
C_sdnElf 2009-03-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hbxtlhx 的回复:]
最好把类设计成静态的,这样在整个应用程序都可以通过类名来访问其成员,相当于全局的了。
[/Quote]

不错
ghychina 2009-03-05
  • 打赏
  • 举报
回复
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
北京的雾霾天 2009-03-05
  • 打赏
  • 举报
回复
最好把类设计成静态的,这样在整个应用程序都可以通过类名来访问其成员,相当于全局的了。

pztx1992 2009-03-05
  • 打赏
  • 举报
回复
用Application吧!
zgke 2009-03-05
  • 打赏
  • 举报
回复
你可以做一个静态的变量

public static Test ApplicationConfig =new Test();

public class Test
{
public int ConfigInt{get;set;}
public string Config1{get;set;}
}

不行吗?
ximi82878 2009-03-05
  • 打赏
  • 举报
回复
创建一个 globalVariable的.CS 的类文件 把你要存的对象放进去就行了

public class globalVariable
{

//******************************************************************
//*Hashtable用于存放全局变量,由key和value成对实现。
//******************************************************************
private static Hashtable table = new Hashtable();

//******************************************************************
//*由于是私有构造函数,不能由new产生实例,所以只有一个实例,
//*保证了该类在程序中是唯一的。
//******************************************************************
private globalVariable()
{}

public static object GetValue(object akey)
{
return table[akey];
}
public static void SetValue(object akey, object avalue)
{
table[akey] = avalue;
}
public static void Remove(object akey)
{
table.Remove(akey);
}
}
ximi82878 2009-03-05
  • 打赏
  • 举报
回复
public class globalVariable
{

//******************************************************************
//*Hashtable用于存放全局变量,由key和value成对实现。
//******************************************************************
private static Hashtable table = new Hashtable();

//******************************************************************
//*由于是私有构造函数,不能由new产生实例,所以只有一个实例,
//*保证了该类在程序中是唯一的。
//******************************************************************
创建一个 globalVariable的.CS 的类文件
private globalVariable()
{}

public static object GetValue(object akey)
{
return table[akey];
}
public static void SetValue(object akey, object avalue)
{
table[akey] = avalue;
}
public static void Remove(object akey)
{
table.Remove(akey);
}
}

把你要存的对象放进去就行了
HUOWEI 2009-03-05
  • 打赏
  • 举报
回复
思路是:你可以把这个经常用到的类进行封装,然后用的时候进行引用,就行了.
楼主没有办法提供代码给你的.
yinjianjing 2009-03-05
  • 打赏
  • 举报
回复
不知道哦,我也在考虑这个问题,不好意思,我到是想用SESSION之类的东西,不过这对服务器消耗太大,没敢用,所以我也不知道

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧