我在我的应用程序中使用ASP.NET Web Forms和C#。 我有一个名为Global.cs
的类文件,在其中使用set
和get
属性定义变量。 我通过实例化该类对象在任何页面上使用这些变量。
这是我的Global.cs
文件:
using System;
using System.Data;
using System.Linq;
using System.Web;
/// <summary>
/// Contains my site's global variables.
/// </summary>
public static class Global
{
/// <summary>
/// Global variable storing important stuff.
/// </summary>
public static string gDate;
public static string gMobLength;
public static string gDateFormat;
public static string gApplicationNo;
public static string gBranchNo;
public static string gMemId;
public static string gIsEditable="false";
public static string gLoggedInUserName;
public static string ImportantData
{
get
{
return gDate;
}
set
{
gDate = value;
}
}
public static string MobileLength
{
get
{
return gMobLength;
}
set
{
gMobLength = value;
}
}
public static string DateFormat
{
get
{
return gDateFormat;
}
set
{
gDateFormat = value;
}
}
public static string ApplicationNo
{
get
{
return gApplicationNo;
}
set
{
gApplicationNo = value;
}
}
public static string BranchNo
{
get
{
return gBranchNo;
}
set
{
gBranchNo = value;
}
}
}
这是在整个项目中使用变量的正确方式吗?这种方法可能存在哪些优缺点?对于使用全局变量,你们会采取什么方法?
Settings.Current = new Settings{ ImportantData = "blah blah blah"};
会导致编译错误,因为"set"是不可访问的。 - Daniel Marín