C# 非静态字段、方法或属性需要对象引用

C# 非静态字段、方法或属性需要对象引用,c#,non-static,object-reference,C#,Non Static,Object Reference,使用Mdi和MVC处理登录方法。类结构非常复杂,因此类之间需要不断引用。然而,当我试图更改主菜单视图时,我得到了“非静态字段、方法或属性需要对象引用” 我仍然只是一个业余爱好者,两个月前开始自学,所以我仍然不清楚错误到底指的是什么,它意味着什么 以下是菜单视图类: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;

使用Mdi和MVC处理登录方法。类结构非常复杂,因此类之间需要不断引用。然而,当我试图更改主菜单视图时,我得到了“非静态字段、方法或属性需要对象引用”

我仍然只是一个业余爱好者,两个月前开始自学,所以我仍然不清楚错误到底指的是什么,它意味着什么

以下是菜单视图类:

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出现问题,因为函数中没有对
菜单视图
对象的引用,所以它尝试引用
菜单视图
类,它没有分配任何静态成员。看起来您要做的是调用
Login\u View.mdipparent.SetMenuView()

编辑

在将
主菜单
保存到
LoginView.MdiParent
中时,您可能需要将调用转换为基类
表单
。尝试:
(主菜单)登录视图.mdipradent.SetMenuView()

如果无法强制转换对象,则可以创建一个属性来直接访问该对象


User\u Login\u视图
中,创建一个新属性
public Menu\u View Menu{get;set;}
。然后,在
Show_Login
功能中,添加一行设置
菜单
object
LoginView Menu=Main_菜单。现在,您可以引用
LoginView.Menu.SetMenuView()不需要强制转换。

这很好地解决了这个问题,但现在我有了另一个问题:“System.Windows.Forms.Form”不包含“SetMenuView”的定义,并且找不到接受“System.Windows.Forms.Form”类型的第一个参数的扩展方法“SetMenuView”(是否缺少using指令或程序集引用?)其实我有个想法。我是否可以将“SetMenuView”调用放在另一个方法中,该方法包含对主菜单的引用,并以这种方式访问它?或者对菜单视图的引用仍然会妨碍您吗?主要问题是存储变量以备将来使用的方式。创建变量时,您可以设置期望存储在其中的类型<例如,code>MdiParent
被定义为
表单
类型。如果对象在其继承树的某个地方定义了该签名,则可以将该对象存储在该变量中。当您检索它时,它所存储的类型告诉您该对象可以直接访问什么。您需要强制转换对象以访问链下游的属性或方法。或者,您可以创建一个属性,专门将对象存储为
主菜单
类型。我对访问器的理解仍然很基本,但我确实了解它的工作原理:)