.NET でのグローバル変数の使用に関する "すべき" 部分は省略し、いくつかの "グローバル" 変数にGlobal.asaxを使用する現在作業中のコードを示します。そのファイルからの情報は次のとおりです。
public class Global : System.Web.HttpApplication
{
public enum InvestigationRole
{
Complainent,
Respondent,
Witness,
}
public static string Dog = "Boston Terrier";
}
したがって、ASPX ページから、次のように静的グローバル クラスを開くことで、これらのメンバーにアクセスできます。
protected void Page_Load(object sender, EventArgs e)
{
string theDog = Global.Dog;
// copies "Boston Terrier" into the new string.
Global.InvestigationRole thisRole = Global.InvestigationRole.Witness;
// new instance of this enum.
}
バイヤーは注意してください。.NET の世界では "グローバル変数" の概念を扱うより良い方法がありますが、上記の方法を使用すると、少なくとも、すべての ASPX ページで同じ文字列を繰り返す上に、抽象化のレイヤーが得られます。