2015-11-09 244 views
0

因此,我正在編寫一個應用程序,其中有一個登錄屏幕,然後是菜單屏幕。類實例化後,類變量重置

菜單爲每個級別的用戶啓用不同的按鈕。要在用戶登錄時執行此操作,將分配一個int值,讓應用程序知道用戶登錄的級別以及要啓用的按鈕/報告。

我遇到的問題是當報告關閉並且菜單再次被調用時,該變量被重置回默認的管理員級別。

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個不同的報告,當我在報告上點擊後退鍵時,這就是我如何返回菜單。

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(); 
     } 
    } 

這是菜單類,我一直跟蹤到,但AccessLevel變量更改爲默認值。

感謝

+0

什麼是「類變量」?你能顯示變量的定義嗎? –

+0

就像一個常規變量,但是它是在類級別聲明的,以便類的所有方法都可以使用它。 – Demonic217

+1

@DamonTPelser請發佈它的代碼。所提供的代碼應該是一個小而完整的例子,證明了這個問題。 –

回答

0

另一種選擇是使用保存你所需要的所有信息的靜態類,例如:

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 
     } 
    } 
} 

作爲一個方面說明,以避免重複代碼,並提高安全性,您應該創建你的初始化函數LockKeys這樣的:

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; 
     } 

    }