0

I am new to C# and OOP and have researched many of the similar topics but end up more confused than when I started. I need to be able to call a method in a parent class from a child in another namespace.

Below is a (over) simplified example of what I have now and seems to work, but is this the right/best way?

File Form1.cs:

namespace Test1
{
    public partial class Form1 : Form
    {
        NotTest1.Class1 myClass1 = null;

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Start the child class and pass this parent
            myClass1 = new NotTest1.Class1(this);
        }

        public void Form1Function(String text) 
        {
            textBox1.AppendText(text + Environment.NewLine);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Do some stuff then call Function1 in myClass1
            myClass1.Function1();
        }

    }
}

File Class1.cs:

namespace NotTest1
{
    class Class1 {
        Test1.Form1 _parent;
        public Class1(Test1.Form1 parent) {
            _parent = parent;
        }

        public void Function1()
        {
           // Do lots of "stuff"
            _parent.Form1Function("Got Here");
        }
    }
}

Examples appreciated, as I am still trying to learn all of the correct terminology.

Thanks

frog_jr
  • 181
  • 1
  • 1
  • 7
  • why does the parent and child class have different namespace ? – Miller Nov 25 '13 at 21:09
  • 2
    This question appears to be off-topic because it is about working code, which might belong at [Code Review](http://codereview.stackexchange.com/) – Tim S. Nov 25 '13 at 21:09
  • I think `NotTest1.Class1.Function1()` should return a `string`, then you can do `textBox1.AppendText(myClass1.Function1())`. `Class1` should not have to know about `Form1`, otherwise you can never reuse it. – CodeCaster Nov 25 '13 at 21:09
  • @MillerKoijam -- as stated, this is a simple example, in actual code, I am interfacing between several namespaces using established APIs and trying to create an interface to those APIs. – frog_jr Nov 25 '13 at 21:13
  • @CodeCaster -- Function1() is actually a rather long and involved function that has many points at which it may need to update Form1 in a variety of ways. If it were as simple as a single string, your approach would certainly be appropriate. – frog_jr Nov 25 '13 at 21:17
  • Sounds like you could expose an event or two, and subscribe to those events from the form. :) Otherwise you cannot use or test `Class1` without a `Form1` instance. – CodeCaster Nov 25 '13 at 21:23
  • @TimS. -- Although my code "appears" to be working, I am so new as to not understand if I may be heading for trouble using the code this way. Also I was unfamiliar with Code Review... – frog_jr Nov 25 '13 at 21:27

3 Answers3

0

The best way in Class1.cs use using.

using Test1;
namespace NotTest1
{
class Class1 {
    Form1 _parent;
    public Class1(Form1 parent) {
        _parent = parent;
    }

    public void Function1()
    {
       // Do lots of "stuff"
        _parent.Form1Function("Got Here");
    }
}

}

vborutenko
  • 4,140
  • 3
  • 25
  • 44
0

If Class1 needs to call methods on a Form1 instance, then what you have done is perfectly fine, though I don't see how you have parent and child classes here. What you really a Class1 class that happens to accept a Form1 instance in order to call methods on it.

Jason Evans
  • 28,042
  • 13
  • 88
  • 145
0

What you did works fine.

Nonetheless, you may want to explore a bit on Events - this way your Parent class may instantiate a child class and subscribe to its events, and your child class may be completely decoupled from your Parent class.

A very nice, clear example is shown on this thread:

Super-simple example of C# observer/observable with delegates

Community
  • 1
  • 1
OnoSendai
  • 3,870
  • 2
  • 22
  • 43
  • I will certainly explore events, thanks for the link to this example. So many of the Q/As are not targeted at novices, so I get lost in the what is actually being said. – frog_jr Nov 25 '13 at 21:23
  • The paradigm shift can be overwhelming sometimes, but don't worry - you'll get there. Everybody here was a nooblet once, and we survived. ;) On another note, here's a post that's very similar to what you're trying to accomplish: http://stackoverflow.com/questions/1940165/how-to-access-to-the-parent-object-in-c-sharp?rq=1 – OnoSendai Nov 25 '13 at 21:27