2

I am trying to get information from an external class back to a textbox in the main form. However, monodevelop won't let me change the automatic form creation code so a textbox can be public and easily accessible. When I debug, any changes I make are overwritten.

So, I attempted to pass the text box by reference parameter to a subclass then change the value there. Didn't work.

Finally, I tried to use a listener to trigger a function in the form to change the value, still no luck. Reference: Stack Overflow Page

All routes lead to an infinite loop or failure because of the way I have this structured. Can someone please suggest a solution, and explain why the heck this keeps becoming an infinite loop? Thank you

public class MainClass
{
    //Get Information to here
    //textbox
    public static void Main()
    {
        ParentClass Parent = new ParentClass ();
    }
    public void print1 (string Test)
    {
        Console.WriteLine(Test);
    }
}
public class ParentClass : MainClass
{
    public ParentClass()
    {
        ChildClass child = new ChildClass ();
    }

    public void print2(string Test)
    {
        base.print1 (Test);
    }
}

public class ChildClass : ParentClass
{
    public ChildClass()
    {
        // Some code
    }

    public void print3(string Test)
    {
        base.print2(Test);
    }

    class Socket : ChildClass
    {
        //Asynchronous Socket
        public Socket()
        {
            //Start Listening
        }

        //Listener running, target function:
        void ListenerTargetFunction()
        {
            base.print3 ("Test");
        }
    }
}
Community
  • 1
  • 1
user2419860
  • 101
  • 2
  • 11
  • `ParentClass` inherits from `MainClass`. So whenever you instantiate an object of `ParentClass`, you're implicitly also call the constructor of `MainClass`. So you're basically doing: `new ParentClass() => new MainClass() => new ParentClass() => new MainClass() => ...` so there you have your endless loop. And I'm not quite sure, *why* you instanciate a `ParentClass` inside the `MainClass` (or a `ChildClass` inside a `ParentClass` for that matter). It seems like you're not using those instantiated objects. – Corak May 27 '13 at 07:57
  • Because `ChildClass` inherits from `ParentClass`, and `ParentClass`creates a `ChildClass`, so every time a `ChildClass` is created a `ParentClass` is created, which creates a `ChildClass` again and so on and so on. Same problem with `MainClass` and `ParentClass`. This will lead to an `StackOverflowException`, which is mostly indicating an endless loop. – jAC May 27 '13 at 08:03
  • Ah, well that explains the StackOverflowException. Thank you. – user2419860 May 27 '13 at 22:59

2 Answers2

0

Try modifying manually the Form.Designer.cs

In the :

  private void InitializeComponent()
  {

  }

Just change the control declaration from:

private System.Windows.Forms.Textbox YourTextbox;

to:

public System.Windows.Forms.Textbox YourTextbox;
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • `protected` might suffice. Or have a `protected property TextBox MyTextbox { get { return myPrivateTextBox; } }` – Corak May 27 '13 at 08:03
  • Unfortunately the Monodevelop IDE automatically rewrites the code any time you change it, as it is generated by the form builder, and this still doesn't quite solve the problem. Also, the property idea didn't work either, as I could not access the form object to access the property. In this example, the "Parent Class" was the form class, which was created inside the Application class: "Main Class." The trail of objects kind of restricts me from accessing anything from the top of the hierarchy. – user2419860 May 27 '13 at 23:04
0

Fortunately I had a moment of thinking outside the box and tried passing each object into its child object, conversely to my original screw up of building the hierarchy upside down from intended with subclasses. Off the wall or intuitive, you be the judge:

class Program
{
    public static void Main()
    {
        Console.WriteLine("Main");
        Console.ReadLine();
        Form oForm = new Form();
    }
}

public class Form
{
    public Parent oParent;
    public Form()
    {
        Console.WriteLine("Form");
        Console.ReadLine();
        Parent oParent = new Parent(this);
    }

    public void TargetFunction()
    {
        Console.WriteLine("Target Hit");
        Console.ReadLine();
    }
}

public class Parent
{
    Form oForm;
    Child oChild;
    public int hi = 1;
    public Parent(Form Form)
    {
        oForm = Form;
        Console.WriteLine("Parent");
        Console.ReadLine();
        oChild = new Child(oForm, this);
    }
}

public class Child
{
    Form oForm;
    Parent oParent;
    public Child(Form Form, Parent Parent)
    {
        Console.WriteLine("Child");
        Console.ReadLine();
        oForm = Form;
        oParent = Parent;

        oForm.TargetFunction();
    }
}
user2419860
  • 101
  • 2
  • 11