0

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).

i.e. 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:

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();
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
Ben
  • 2,433
  • 5
  • 39
  • 69
  • `Menu.SetMenuView()` is defined as `public void SetMenuItem() { .. }` or some such. That is, it returns *no* value. The compiler error has nothing to do with `new` or scoping. Simply, the fact that a [`void`](http://msdn.microsoft.com/en-us/library/yah0tteb.aspx) expression, which has *no value*, cannot be used as an expression that *requires* a value. – user2864740 Jan 29 '14 at 01:35
  • What are you actually trying to do? I don't get it by the pieces of code you posted. But I feel you are wildly mixing design and app logic... – mvo Jan 29 '14 at 01:35
  • @user2864740 Are you saying that if I make the `accessLevelSet` a return value and make `public static SetMenuView()`, this will help my issue? – Ben Jan 29 '14 at 01:38
  • @Ben The immediate issue (aka the compiler error) is because the code is attempting use the *result* of a `void` function which just doesn't make sense as [a `void` function has *no* return value](http://msdn.microsoft.com/en-us/library/yah0tteb.aspx). (So any form of not doing such, which is clearly invalid, will "help" the overall goal - or rather, move onto other problems.) – user2864740 Jan 29 '14 at 01:39
  • @user2864740 would you be able to explain that in the answers? – Ben Jan 29 '14 at 02:02

1 Answers1

1

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.

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

// This line gives me an error.
LoginView.Menu_View.SetMenuView();
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • This was the first method I implemented (use `Menu_View Menu{ get; set; }` in the LoginView), but when I ran a test, it threw an exception, because the test believed the Login no longer existed before the test finished. – Ben Jan 29 '14 at 01:31
  • @ben that sounds like a slightly different problem though. Most likely you tried to do something that's illegal after the value is disposed. – JaredPar Jan 29 '14 at 01:32
  • I posted the Test issue using the instance here: http://stackoverflow.com/questions/21224727/test-assert-fail-null-reference-exception-object-reference-not-set-to-an-insta – Ben Jan 29 '14 at 01:35
  • @ben the answer by Brendan seems to address why the test is failing. – JaredPar Jan 29 '14 at 01:38