好了,所以我有是导致我的悲伤一行代码,但不幸的是,没有它,我的程序中断。 这是我能想到的(除了完全重组我的程序),做我想做的事情的唯一途径。
此行的目的是为了选择下一个类来运行(在菜单中),这是它自己的范围之外。 麻烦的是类(登录)菜单的范围存在。
下面是一行:
LoginView.Menu.SetMenuView();
任何人都可以提出写这更好的办法?
从技术上讲,没有什么错行了,程序运行顺利。 但是,当我运行一个测试,测试失败是由于空引用异常。
下面是它出现在类:
控制器:
public void Show_Login(Menu_View Main_Menu)
{
// Creates an object of the User_LoginView.
// Set the Parent Form of the Child window
// Display the new form.
User_LoginView LoginView = new User_LoginView();
LoginView.MdiParent = Main_Menu;
// This line changes the reference to the "Menu" variable for later use.
LoginView.Menu = Main_Menu;
LoginView.Show();
}
public static void Compare_Login(User_LoginView LoginView)
{
// Creates a new object of User_Model and populates it from the User_Controller.
User_Model AccessModel = new User_Model();
AccessModel.Name = screenName;
AccessModel.Pwd = screenPwd;
// Runs the Login Comparsion in the Database_Facade, and passes in the Model.
Database_Facade Database = new Database_Facade();
Database.GetLoginAccess(AccessModel);
// screenAccess used for testing.
screenAccess = AccessModel.AccessLevel;
Menu_View.accessLevelSet = AccessModel.AccessLevel;
// If the return is placed here, the assert passes, but the rest of the code
// becomes unreachable.
// return;
// Compares the returned AccessLevel.
// if it is corect; closes the Login and runs the SetMenuView method,
// if it is incorrect; shows an error.
if (AccessModel.AccessLevel > 0)
{
Console.WriteLine("Access Level " + AccessModel.AccessLevel);
LoginView.Menu.SetMenuView();
LoginView.Close();
Menu_View.accessLevelSet = AccessModel.AccessLevel;
}
else
{
ErrorCodes_Controller LoginError = new ErrorCodes_Controller();
LoginError.WrongLoginError();
}
}
视图:
public partial class User_LoginView : Form
{
// This is where the new reference is set, for when it gets called
// at the end of the Login Comparison.
public Menu_View Menu { get; set; }
public User_LoginView()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
User_Controller.Check_Login(this);
}
}
下面是测试:
[TestMethod()]
public void Compare_LoginTest()
{
User_LoginView LoginView = new User_LoginView();
User_Controller.screenName = "ben";
User_Controller.screenPwd = "password";
User_Controller.Compare_Login(LoginView);
int actual = User_Controller.screenAccess;
int expected = 1;
Assert.AreEqual(expected, actual);
}