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