• Advertisement
Sign in to follow this  

c# object referrence

This topic is 2902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Quote:

InfoScreen.SetInfo("s","S","s");

InfoScreen is a class. The compiler error is trying to say "which instance of InfoScreen should I call SetInfo() on"?

The answer is to name the otherwise anonymous Infoscreen instance you sent to StateManager.AddScreen:

void ShaneMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
InfoScreen screen = new InfoScreen();
screen.SetInfo("s","S","s");
StateManager.AddScreen(screen, e.PlayerIndex);
}


Note that I call SetInfo before the call to AddScreen, otherwise the AddScreen method might try to access the values before they are initialised.

Finally, have you considered moving the SetInfo() logic to a constructor for InfoScreen? That would eliminate a lot of this work, and is generally a safer and superior method to having manual initialisation functions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement