RoboKozo
RoboKozo

Reputation: 5062

How do I access an object in another windows form using c#?

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 :(
}

Upvotes: 1

Views: 5667

Answers (3)

JDPeckham
JDPeckham

Reputation: 2524

use repository pattern for getting/creating players, identity map pattern for storing living references to playres and service locator pattern perhaps in a singleton instance for global access to your identity mapper(s).

for games global static is fine i think but...

Upvotes: 0

Greg Sansom
Greg Sansom

Reputation: 20870

You need to somehow get a reference to your Player object from the Options form.

Change this line:

Player Player1 = new Player("Player 1", "X");

to

public Player Player1 = new Player("Player 1", "X");

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:

private Main main;
public Options(Main main)
{
  this.main=main;
}

Now, instead of creating Options with it's default constructor from Main, use(still in Main):

Options o = new Options(this);

You should now be able to reference your Players from Options using:

string name = this.main.Player1.Name

Upvotes: 3

Cine
Cine

Reputation: 4402

public static bool Turn { get; set; }

Means that this variable is SHARED between ALL instances of the class. So your turn logic wont work, as they share this variable.

Visual Studio 2010 tells me "The name "Player1" does not exist in the current context.

Because you declare that variable in your main.cs, something that Options.cs has no access too. You would need to make the static variables, and then access those. IE

public static Player Player1;
public static Player Player2;
public static main(){
    Player1 = new Player("Player 1", "X");
    Player2 = new Player("Player 2", "O");
    ...
}

Upvotes: 3

Related Questions