I’ve got my windows form Main.cs and and my windows form Options.cs. Inside my Main.cs I’ve defined a class called “Player” and I’ve instantiated it there.
From my Options form I’d like to be able to access the methods and properties of the Player object I created in the Main.cs form. Visual Studio 2010 tells me “The name “Player1” does not exist in the current context.
What I think is weird and what is adding to my confusion is the fact that I can “see” static methods for my Player class from the Options.cs form.
Thank you in advance!
EDIT:
Inside Main.cs:
Player Player1 = new Player("Player 1", "X");
Player Player2 = new Player("Player 2", "O");
…
public class Player
{
public static bool Turn { get; set; }
public static int TotalGames { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public static void ToggleTurn()
{
Turn = !Turn;
}
public Player(string name, string icon)
{
Name = name;
Icon = icon;
}
}
Inside Options.cs:
private void Options_Load(object sender, EventArgs e)
{
SetCheckboxesToPlayerTurn();
txtPlayer1Name.Text = Player1.Name; //HERE IS WHAT I WANT :(
}
You need to somehow get a reference to your Player object from the Options form.
Change this line:
to
and move it outside of the constructor (if that’s where it is).
Then create a constructor in Options which accepts an instance of the Main, like this:
Now, instead of creating Options with it’s default constructor from Main, use(still in Main):
You should now be able to reference your Players from Options using: