[英]Object reference is required for non-static field, method or property
使用Mdi和MVC研究登錄方法。 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;
}
}
用戶控制器:
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
}
}
}
Db_Facade類當前僅是變量的集合,因此為什么在User_Controller中將ACCESS設置為1
發生問題是因為您在函數內沒有對Menu_View
對象的引用,因此它嘗試引用Menu_View
類 , 該類沒有分配任何靜態成員。 看起來您想要執行的操作是調用Login_View.MdiParent.SetMenuView()
編輯
將Main_Menu
保存到LoginView.MdiParent
並將其存儲為基類Form
,您可能需要Main_Menu
調用。 試試: (Main_Menu)Login_View.MdiParent.SetMenuView()
如果無法投射對象,那么您可以創建一個Property以直接訪問該對象。
在您的User_Login_View
,創建一個新屬性public Menu_View Menu {get;set;}
。 然后,在Show_Login
函數中,添加一行以設置Menu
對象LoginView Menu = Main_Menu;
。 現在,您可以引用LoginView.Menu.SetMenuView();
不需要演員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.