1

I am creating a windows form application using c#. My form has labels, list-boxes and buttons, all of which need to be edited. They are edited from a different class, and further will need the values in the labels to perform other logic.

My problem is accessing these controls from other classes. I tried making an instance of the form and using it like this:

Form frm = new Form(); myVar = frm.lblMylabel.Text;

However, using a breakpoint, I followed the code, and it loops between the above instance being created, and the start of the Form code, which calls the class again where the instance was created.

I have tried to find the answer online, however they don't seem applicable to what I am trying to do, and whilst I am unsure how to do it, they appear to be overly complex. So, my question it: is there a relatively simple way to be able to access values and edit values from controls in a form from another class?

Thanks in advance.

EDIT - Breakpoint starts at Point 3, '=new Form();'.

Loops to Point 1.

Point 2 calls GamePlay().

Reaches Point 3 and loop restarts.

public partial class frmGame : Form     //Point 1
{
    public frmGame()
    {
        InitializeComponent();


        Game.GamePlay();    //Point 2

    }

class Game{

    public static void GamePlay()
    {
        frmGame form = new frmGame();  //Point 3

        form.lstPrevious1.Items.Add("Item Number");

    }
}
10aples
  • 103
  • 7
  • 1
    Welcome to stackoverflow. Could you post a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) of your problem here please. – Caltor Apr 22 '17 at 22:36
  • Thank you, I have edited the question to include one. – 10aples Apr 22 '17 at 22:58

1 Answers1

2

Your problem has nothing to do with accessing properties of a different class, you are just producing a recursive loop:

At Point 3, within the method GamePlay you create a new instance of frmGame. When creating an instance (an object), its constructor is called. In this case, your constructor is the method public frmGame().

Within this constructor, you now call GamePlay (Point 2), which, as we saw from before, again creates a new instance of frmGame (Point 3 again), also again calling its constructor (Point 1).

At this point, you have already two forms created (although not shown yet).

Very soon you will receive a stack overflow.

One possible solution: Move the line creating your instance into a different method that is guaranteed to be called only once at program start:

public partial class frmGame : Form     //Point 1
{
    public frmGame()
    {
        InitializeComponent();


        Game.GamePlay();    //Point 2

    }
}

class Game{

    public static void GameStart(){
        frmGame form = new frmGame();  //Point 3
    }

    public static void GamePlay()
    {
        form.lstPrevious1.Items.Add("Item Number");    
    }
}
Psi
  • 6,387
  • 3
  • 16
  • 26
  • Sorry if i am slow on the uptake, but if I create the instance in another method, how to I ensure it is only called once? Since each time the instance is created it calls the class `frmGame`, this starts executing the code from the top of that class. However, since this class is that start of the code, the method used to create the instance will have had to be called from somewhere in `frmGame` or another method called from `frmGame`? – 10aples Apr 22 '17 at 23:47
  • It can't _call_ a _class_, it only calls a _method_. In this case `Game.GamePlay`. But after your form creation is done in `Game.GameStart`, it won't get recursed again. – Psi Apr 22 '17 at 23:49
  • How would I not call the `Game.GameStart` method in the constructor? – 10aples Apr 23 '17 at 15:33
  • You can do this, and by moving the object creation out of that method you don't create a loop here – Psi Apr 23 '17 at 17:42