简体   繁体   English

无法将类型“ void”隐式转换为“ x”

[英]Cannot implicity convert type “void” to “x”

I'm having a hard time understanding referencing and calls. 我很难理解引用和调用。 If a form already exists , how can I call a method from it without using re-creating the form? 如果表单已经存在 ,如何在不使用重新创建表单的情况下从中调用方法? (Using the new operator). (使用new运算符)。

ie Menu_View Menu = Menu.SetMenuView(); Menu_View Menu = Menu.SetMenuView();

Currently my scope flows a bit like this: 目前,我的范围有点像这样:

In the Menu: 在菜单中:

    public Menu_View()
    {
        // Initialises the Menu form.
        // Runs the method in the controller to open the Login form.
        InitializeComponent();  
        User_Controller UserController = new User_Controller();              
        UserController.Show_Login(this);
    }

In the Controller: 在控制器中:

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

In the Login Form: 在登录表单中:

public partial class User_LoginView : Form
{
    // Opens the form.
    // When the Login Button is clicked, runs checks and comparisons.
    public User_LoginView()
    {
        InitializeComponent();
    }

    public void btnLogin_Click(object sender, EventArgs e)
    {
        User_Controller.Check_Login(this);
    }    

Then back in the Controller: 然后返回Controller:

public static void Compare_Login(User_LoginView LoginView)
    {
        // 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.Close();
            Menu_View.accessLevelSet = AccessModel.AccessLevel;
        }
        else
        {
            ErrorCodes_Controller LoginError = new ErrorCodes_Controller();
            LoginError.WrongLoginError();
        }
        // This line gives me an error.
        Menu_View Menu = Menu.SetMenuView();
    }

In order to do this you need to pass around the created instance as parameters or fields to the methods and types that need access to the value. 为此,您需要将创建的实例作为参数或字段传递给需要访问该值的方法和类型。 For instance here you could just add a field to User_LoginView of type Menu_View . 例如,在这里您可以只向User_LoginView类型的Menu_View添加一个字段。

class User_LoginView : Form
{
  public Menu_View Menu_View;

  ...
}

This could be set when creating the instance 这可以在创建实例时设置

User_LoginView LoginView = new User_LoginView();
LoginView.Menu_View = Main_Menu;

And then accessed in Compare_Login 然后在Compare_Login访问

// This line gives me an error.
LoginView.Menu_View.SetMenuView();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM