1

Hopefully I can explain this OK. I have a countdown Timer - A user enters the time, etc and picks if they want to open min size or max size from Radio buttons. Depending on which they choose it will load either Min form or Max form where the time will value entered in the UserControl form will be passed across and start to countdown. Now there were buttons for pause/stop,reset etc on the User Control form. I want these to be instead on the Min/Max forms. I was hoping the easiest way for me to do this would be hide the button on the user control form and then try and wire it up to a button on min/max form so if they were pressed it preform like the button on user control was pressed. However I am getting the error in title - it highlights the statement below in yellow (next statement that will be executed)....

(note - this line of code is in the Min form - I need to declare a new instance of it so i can call the function PauseMinClick (the Pause button on MinForm) _ I am wanting it to call the btnPauseClick function which is in CountdownUserControl).

    private CountdownUserControl CU = new CountdownUserControl();

    private void PauseMin_Click(object sender, EventArgs e)
    {
        CU.btnPause_Click(sender, e);
    }

and highlights the one in green below (next statement to execute when this thread returns from current function)...

    private Min _Min = new Min();

(this is in my CountdownUserControl class - note i need an instance of it here to pass across the values which have to countdown. Does anyone know what i should be doing to resolve this easily? Ideally I dont want to have to re-write lots of code - I would just like to get it working with the buttons on the new forms Max/Min but wired up as if they were being pressed on the UserControl form (where they all work fine).

Many Thanks - Colly

2 Answers 2

4

It sounds like you have this:

class CountdownUserControl
{
    private Min _Min = new Min();

    // Other stuff...
}

class Min
{
    private CountdownUserControl CU = new CountdownUserControl();

    // Other stuff...
}

In other words, to create an instance of Min, you need to create an instance of CountdownUserControl... which in turn needs to create an instance of Min... which in turn needs to create an instance of CountdownUserControl... Do you see why you have a problem?

It's not really clear to me what you're trying to achieve, but this is the cause of the problem. Perhaps one of the classes should take a parameter in its constructor to allow it to refer to an instance of the other?

Sign up to request clarification or add additional context in comments.

8 Comments

I was thinking that CU.btnPause_Click would actually call PauseMin_Click :-)
@John - yes that is the type of scenario I have - I see now how it is causing a problem. I am not quit sure of your solution. I am trying to use a fucntion which is in CountdownuserControl within Min. So within min I would like to call CU.btnPauseClick
@CollyMcK: You need to think about what should really be creating instances of other classes. Presumably you want to call btnPauseClick on a specific instance of CountdownUserControl, not just any arbitrary one.
There is a button on CountdownUser Control called Pause. I want to wire up my Pause Button on Min form so that when pressed it is as if the pause button on countdown user control was pressed.
@CollyMcK: So who creates both of these controls? It sounds like actually you should be doing the wiring outside, in code which merely knows about both objects. It feels wrong to hard-code either to know about each other.
|
1

You say this is in your CountdownUserControl class? If so, this is the problem:

 private CountdownUserControl CU = new CountdownUserControl(); 

It creates a new CountdownUserControl, which creates a CountdownUserControl, which.....etc until the stack overflows

1 Comment

No, that one's in Min as far as I can see... the problem is that the two classes each have a field referring to the other and creating a new instance. See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.