c#工程中全局变量的定义方法

1.建立一单独的公共类,在类中定义全局常量或变量,如下

using System;

namespace Globalspace
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
///

public class global
{

#region "常量定义区"
//数据库连接字符串
public const string conString = "User ID=sa;Password=mypass;Initial Catalog=master;Data Source=127.0.0.1;Min Pool Size=10;";
//帐套数据库名称
//public const string DBName = "myCompany001";
public const string DBName = "ERPClient";
#endregion


#region "变量定义区"
public static bool pqLogined; //用户是否已经登录
public static string userName="";
#endregion

private global()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
}

2.在其他模块中引用类的命名空间,如

using Globalspace;

然后全局变量使用方式如下:

if(global.pqLogined == false)
Application.Exit();

posted @ 2012-02-02 14:06  大佛张  阅读(21539)  评论(0编辑  收藏  举报