C# 类变量在类实例化后重置
所以我正在编写一个应用程序,它有一个登录屏幕和一个菜单屏幕 菜单为每个级别的用户启用了不同的按钮。要在用户登录时执行此操作,将分配一个int,让应用程序知道用户登录的级别以及要启用的按钮/报告 我遇到的问题是,当报表关闭并且再次调用菜单时,变量被重置回默认的管理级别C# 类变量在类实例化后重置,c#,class,variables,constructor,C#,Class,Variables,Constructor,所以我正在编写一个应用程序,它有一个登录屏幕和一个菜单屏幕 菜单为每个级别的用户启用了不同的按钮。要在用户登录时执行此操作,将分配一个int,让应用程序知道用户登录的级别以及要启用的按钮/报告 我遇到的问题是,当报表关闭并且再次调用菜单时,变量被重置回默认的管理级别 Menu M = new Menu(Permission); M.Show(); this.Hide(); 此代码用于从登录中拉出菜单,int被传递给构造函数 public Menu(string Permission)
Menu M = new Menu(Permission);
M.Show();
this.Hide();
此代码用于从登录中拉出菜单,int被传递给构造函数
public Menu(string Permission)
{
InitializeComponent();
AccessLevel = int.Parse(Permission);
LockKeys();
}
public Menu()
{
InitializeComponent();
LockKeys();
}
这是将int赋值给类变量的菜单代码
我有10个不同的报告,当我在报告上按back键时,这就是我调用菜单的方式
Menu shwMenu = new Menu();
shwMenu.Show();
this.Hide();
现在我明白了问题是它调用的构造函数没有分配变量。现在,我所能想到的就是将变量反弹到每个报表中,然后返回,只需调用正确的构造函数即可
我的问题是,有没有更简单的方法?某种保存类变量的方法,这样我就不必每次调用类时都重新实例化它
public partial class Menu : Form
{
private int AccessLevel;
public Menu(string Permission)
{
InitializeComponent();
AccessLevel = int.Parse(Permission);
LockKeys();
}
public Menu()
{
InitializeComponent();
LockKeys();
}
private void LockKeys()
{
if(AccessLevel == 1)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = false;
btnCompJob.Enabled = false;
btnCompServ.Enabled = false;
btnVS.Enabled = true;
}
else if (AccessLevel == 2)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = true;
btnCompJob.Enabled = true;
btnCompServ.Enabled = false;
btnVS.Enabled = false;
}
else if (AccessLevel == 3)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = true;
btnCompJob.Enabled = false;
btnCompServ.Enabled = true;
btnVS.Enabled = false;
}
else if (AccessLevel == 4)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = true;
btnBookJob.Enabled = false;
btnCompJob.Enabled = false;
btnCompServ.Enabled = false;
btnVS.Enabled = false;
}
}
private void btnVS_Click(object sender, EventArgs e)
{
VehicleStatR1 VS = new VehicleStatR1();
VS.Show();
this.Hide();
}
private void btnSR_Click(object sender, EventArgs e)
{
ServRequireR3 SR = new ServRequireR3();
SR.Show();
this.Hide();
}
private void btnVSC_Click(object sender, EventArgs e)
{
VehServCompleteR4 VSC = new VehServCompleteR4();
VSC.Show();
this.Hide();
}
private void btnPT_Click(object sender, EventArgs e)
{
PlannedTripR6 PT = new PlannedTripR6();
PT.Show();
this.Hide();
}
private void btnCT_Click(object sender, EventArgs e)
{
ActualTripR7 CT = new ActualTripR7();
CT.Show();
this.Hide();
}
private void btnTS_Click(object sender, EventArgs e)
{
HoursWorkedR8 TS = new HoursWorkedR8();
TS.Show();
this.Hide();
}
private void btnBookJob_Click(object sender, EventArgs e)
{
BookJobServ BJ = new BookJobServ();
BJ.Show();
this.Hide();
}
private void btnCompJob_Click(object sender, EventArgs e)
{
CompJob CJ = new CompJob();
CJ.Show();
this.Hide();
}
private void btnCompServ_Click(object sender, EventArgs e)
{
CompServ CS = new CompServ();
CS.Show();
this.Hide();
}
private void btnLogOut_Click(object sender, EventArgs e)
{
LogIn LI = new LogIn();
this.Hide();
LI.Show();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
这是menu类,我一直回溯到该类,但AccessLevel变量更改为默认值
谢谢我想你要找的是一个“静态”变量,它是一个类级变量
private static int AccessLevel;
静态变量是在类级别定义的,类的所有实例都可以访问和修改它们。换句话说,静态变量在类实例之间共享。更多信息:另一个选项是使用一个静态类来保存您需要的所有信息,例如:
public static class CurrentUser
{
public static string UserName { get; set; }
public static int AccessLevel { get; set; }
}
用户登录时使用的选项:
public void LogUserIn(string userName, string password)
{
//Make sure to reset, so an invalid login doesn't retain data
LogUserOut();
//Do the user validation here
CurrentUser.UserName = userName;
CurrentUser.AccessLevel = 0; //whatever was read from your user database
}
public void LogUserOut()
{
CurrentUser.UserName = "";
CurrentUser.AccessLevel = int.MinValue;
}
然后,当您需要使用访问级别时,您不需要担心传递值,例如,您的菜单表单如下所示:
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
LockKeys();
}
private void LockKeys()
{
if(CurrentUser.AccessLevel == 1)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 2)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 3)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 4)
{
//Enable/Disable Controls
}
}
}
private void LockKeys()
{
//Set them all default false, and then only enable.
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = false;
btnCompJob.Enabled = false;
btnCompServ.Enabled = false;
btnVS.Enabled = false;
if(AccessLevel == 1)
{
btnVS.Enabled = true;
}
else if (AccessLevel == 2)
{
btnBookJob.Enabled = true;
btnCompJob.Enabled = true;
}
else if (AccessLevel == 3)
{
btnBookJob.Enabled = true;
btnCompServ.Enabled = true;
}
else if (AccessLevel == 4)
{
btnTS.Enabled = true;
}
}
作为旁注,为了避免代码重复并提高安全性,您应该创建如下初始化函数锁键
:
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
LockKeys();
}
private void LockKeys()
{
if(CurrentUser.AccessLevel == 1)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 2)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 3)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 4)
{
//Enable/Disable Controls
}
}
}
private void LockKeys()
{
//Set them all default false, and then only enable.
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = false;
btnCompJob.Enabled = false;
btnCompServ.Enabled = false;
btnVS.Enabled = false;
if(AccessLevel == 1)
{
btnVS.Enabled = true;
}
else if (AccessLevel == 2)
{
btnBookJob.Enabled = true;
btnCompJob.Enabled = true;
}
else if (AccessLevel == 3)
{
btnBookJob.Enabled = true;
btnCompServ.Enabled = true;
}
else if (AccessLevel == 4)
{
btnTS.Enabled = true;
}
}
什么是“类变量”?您能显示变量的定义吗?就像一个常规变量一样,但它是在类级别声明的,这样类的所有方法都可以使用它。@DamonTPelser请发布它的代码。提供的代码应该是一个小而完整的示例,演示了问题。@DamonTPelser在显示报告时是否关闭主窗体?@DamonTPelser会话状态和Cookie不适用于winforms项目。