Sign in to follow this  

c# object referrence

This topic is 2829 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
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

This topic is 2829 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this