Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Object referrence

Options
  • 14-03-2010 11:36pm
    #1
    Closed Accounts Posts: 25


    void ShaneMenuEntrySelected(object sender, PlayerIndexEventArgs e)
            {
                StateManager.AddScreen(new InfoScreen(), e.PlayerIndex);
                InfoScreen.SetInfo("s","S","s");
            }//End UngulateMenuEntrySelected
    
     public void SetInfo(string x, string y, string z)
            {
                name.Text = x;
                role.Text = "role: " + y;
                email.Text = "contact: " + z;
    
            }//End SetMenuEntryText
    
    
    

    Simple menu entry when I click someones name I want to add a new screen with the info passed via the Parameters, of the SetInfo which is declared in the InfoScreen class. At the minute I get

    Error 1 An object reference is required for the non-static field, method, or property

    if I make the SetInfo method static I get 3 errors

    Error 1 An object reference is required for the non-static field, method, or property 'InfoScreen.name'

    etc etc


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Not sure exactly what language that is but as near as I can tell:

    [PHP]InfoScreen.SetInfo("s","S","s");[/PHP]

    With the above you're trying to call the method SetInfo in a static context; i.e. without any reference to an object.

    In order to be able to call the SetInfo method in that way; you would have to declare it as a static method. If however you do so you will not be able to update any instance variables (which leads to the error message which lists three reference errors).

    What you'll need to do is something like the following:

    [PHP]
    InfoScreen infoScreen = new InfoScreen();
    StateManager.AddScreen(infoScreen, e.PlayerIndex);
    infoScreen.SetInfo("s","S","s"); //Note you're now calling an instance method as opposed to a static method and can therefore update that instances variables.
    [/PHP]


Advertisement