There are lots of ways you do this; here's two examples:
First example: using a 'static variable:
using System.Windows.Forms;
namespace Demo1
{
public class Sample1
{
public static string x = "initial value of 'x";
public void sampleFn1() {x = "value of 'x set in function";}
}
}
namespace Demo2
{
public class Sample2
{
public void sampleFn2(){MessageBox.Show(Demo1.Sample1.x);}
}
}
If we then executed this code in some other NameSpace (not Demo1, or Demo2):
Demo2.Sample2 newSample2 = new Demo2.Sample2();
newSample2.sampleFn2();
You'd get a MessageBox pop-up showing the value of 'x it was initialized to.
If you changed the access-modifier of 'x so it was not 'static: you'd get an error message because there is no instance of Sample1 available, and Sample2
has no access to an instance of Sample1 even if one was created.
Second example: where 'x is not 'static, and access to 'x in an instance of Sample1 is provided by passing the instance of Sample1 into the method 'sampleFn2:
namespace Demo1
{
public class Sample1
{
public string x = "initial value of 'x";
public void sampleFn1(){x = "value of 'x set in class constructor";}
}
}
namespace Demo2
{
public class Sample2
{
public void sampleFn2(Demo1.Sample1 instance){MessageBox.Show(instance.x);
}
}
Demo1.Sample1 newSample1 = new Demo1.Sample1();
Demo2.Sample2 newSample2 = new Demo2.Sample2();
newSample2.sampleFn2(newSample1);
newSample1.sampleFn1();
newSample2.sampleFn2(newSample1);