Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Object referrence

  • 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, Registered Users 2 Posts: 1,919 ✭✭✭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