Reputation: 2523
working on a login method, using Mdi, and MVC. The Class structure is very elaborate, so constant references between classes is required. However, when I try to change the Main Menu view, I get the "object reference is required for non-static field, method or property".
I am still only an amateur, started 2 months ago and self-taught, so I am still fuzzy on what the error is actually referring to and what it means.
Here is the Menu View Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DCIM_With_Test.User;
namespace DCIM_With_Test.Menu
{
public partial class Menu_View : Form
{
public static int btnUser;
public Menu_View()
{
InitializeComponent(); //runs the Main Menu form
User_Controller CreateLoginView = new User_Controller(); //opens the Show_Login method in the User_Controller
CreateLoginView.Show_Login(this); //sets the Mdi Parent container
}
public void SetMenuView()
{
switch (Db_Facade.ACCESS)
{
case 1:
plantAreaCodeToolStripMenuItem.Visible = true;
cableIDToolStripMenuItem.Visible = true;
logOutToolStripMenuItem.Visible = true;
createNewUserToolStripMenuItem.Visible = true;
editUserToolStripMenuItem.Visible = true;
break;
default:
plantAreaCodeToolStripMenuItem.Visible = false;
cableIDToolStripMenuItem.Visible = false;
logOutToolStripMenuItem.Visible = false;
createNewUserToolStripMenuItem.Visible = false;
editUserToolStripMenuItem.Visible = false;
break;
}
}
The User Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCIM_With_Test.Menu;
namespace DCIM_With_Test.User
{
class User_Controller
{
public void Show_Login(Menu_View Main_Menu)
{
User_Login_View LoginView = new User_Login_View(); // Creates an object of the User_Login_View.
LoginView.MdiParent = Main_Menu; // Set the Parent Form of the Child window.
LoginView.Show(); // Display the new form.
}
public void Show_User(Menu_View Main_Menu)
{
User_Edit_View EditUserView = new User_Edit_View(); // Creates an objet of the User_View.
EditUserView.MdiParent = Main_Menu; // Set the Parent Form of the Child window.
EditUserView.Show(); // Display the new form.
}
public static void Compare_Login(User_Login_View Login_View)
{
User_Model getACCESS = new User_Model();
getACCESS.uName = Login_View.txtUsername.Text;
getACCESS.uPwd = Login_View.txtPassword.Text;
Db_Facade.ACCESS = 1;
if (Db_Facade.ACCESS > 0)
{
Login_View.Close();
}
else
{
Login_View.lblError.Visible = true;
}
Menu_View.SetMenuView(); //here is where the error occurs
}
}
}
The Db_Facade Class is currently just a collection of Variables, hence why ACCESS is set to 1 in the User_Controller
Upvotes: 0
Views: 2754
Reputation: 22323
Your problem occurs because you don't have a reference to the Menu_View
object inside your function, so it tries to reference the Menu_View
class, which doesn't have any static members assigned. It looks like what you want to do is call Login_View.MdiParent.SetMenuView()
EDIT
You probably need to cast your call, as you are saving Main_Menu
into LoginView.MdiParent
which stores it as it's base class Form
. Try: (Main_Menu)Login_View.MdiParent.SetMenuView()
If casting the object isn't possible, then what you can do is create a Property to access the object directly.
In your User_Login_View
, create a new property public Menu_View Menu {get;set;}
. Then, in your Show_Login
function, add a line to set the Menu
object LoginView Menu = Main_Menu;
. Now, you can reference LoginView.Menu.SetMenuView();
without needing a cast.
Upvotes: 1