2

I'm struggling to get a class from a different form without making it static, here's what I want to do:

//First form
public partial class SetupScreen : Form
{
    Control myObject;
    public Battleship myBattleship;

    public SetupScreen()
    {
        InitializeComponent();
        //Create Class Object
        myBattleship = new Battleship();
    }
}

//Launch second form 
public partial class GameScreen : Form
{
    Control myObject;
    Battleship myBattleship;
    Battleship fredBattleship;

    public GameScreen()
    {
        InitializeComponent();
        //Get the class
        myBattleship = SetupScreen.myBattleship;
    }
}

I keep getting the error "an object reference is required for the non-static field, method or property"

I want the class to be accessible by the whole form, not just a single method therefore I don't want to pass it through each time because this is a hassle

I don't want to make the class static since it cannot be erased, how would I go about doing this?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kehza
  • 363
  • 1
  • 5
  • 11
  • If you write `SetupScreen.myBattleship` then you're trying to access a static member called `myBattleship` within class `SetupScreen`. I guess want you want to do is instantiate a new `Battleship` object: `myBattleship = new Battleship()`. If you want to use the same `Battleship` object throughout the program, then you are going to have some static object. Probably you're looking for the *Singleton* pattern. – Julián Urbano Mar 29 '13 at 01:47
  • Possible duplicate of [Access form component from another class](http://stackoverflow.com/questions/6803970/access-form-component-from-another-class) – Ash Jul 21 '16 at 02:54

3 Answers3

1

You are getting this error because you are trying to access a non-static field in a static manner.

Where do you instantiate SetupScreen and GameScreen?

Why not something like this:

public partial class SetupScreen : Form
{
    private Control myObject;
    public Battleship myBattleship;
    private GameScreen gameScreen;

    public SetupScreen()
    {
        InitializeComponent();
        //Create Class Object
        myBattleship = new Battleship();
        gameScreen = new GameScreen(this);
    }
}

public partial class GameScreen : Form
{
    private Control myObject;
    private Battleship myBattleship;
    private Battleship fredBattleship;
    private SetupScreen setupScreen;

    public GameScreen(SetupScreen setupScreen)
    {
        InitializeComponent();

        this.setupScreen = setupScreen;
        myBattleship = this.setupScreen.myBattleship;

    }
}

Of course, this will only work if you can instantiate GameScreen in SetupScreen. I could give you a better answer if you tell me where/how you are "launching" these forms.

Tom
  • 2,360
  • 21
  • 15
1

You could pass a reference of your first form to your second form, or (what I would do), create a public Battleship property on your second form and pass your object that way.

//First form
public partial class SetupScreen : Form
{
    Control myObject;
    public Battleship myBattleship;

    public SetupScreen()
    {
        InitializeComponent();
        //Create Class Object
        myBattleship = new Battleship();

        Form gameForm = new GameScreen(); // New form object
        gameForm.MyBattleship = myBattleship; // Set property
        gameForm.Show(); // Show form
    }
}

//Second form 
public partial class GameScreen : Form
{
    Control myObject;
    Battleship fredBattleship;

    public BattleShip MyBattleship { set; get; }

    public GameScreen()
    {
        InitializeComponent();
    }
}
Ryan Frame
  • 285
  • 1
  • 16
-1

you must use singleton pattern. so your code must be like this:

//First form
public partial class SetupScreen : Form
{
 public static SetupScreen setupScreenFrm;
 Control myObject;
 public Battleship myBattleship;

 public SetupScreen()
 {
    setupScreenFrm=this;
    InitializeComponent();
    //Create Class Object
    myBattleship = new Battleship();
 }
}

//Launch second form 
public partial class GameScreen : Form
{
 Control myObject;
 Battleship myBattleship;
 Battleship fredBattleship;

 public GameScreen()
 {
    InitializeComponent();
    //Get the class
    SetupScreen ssFrm=SetupScreen.setupScreenFrm;
    myBattleship = ssFrm.myBattleship;
 }
}

and first of all, in the start of your app, create an instant of SetupScreen form.

now you can access to SetupScreen in anywhere.

user2852297
  • 109
  • 1
  • 3