>I'm struggling to understand the visibility of objects and their properties & methods in .Net, can anyone assist?
>
>For Example;
>
>I have a simple project with two forms "MainForm" and "SubForm", there is a button on the MainForm that calls the SubForm.
>
>If I was in VFP I would expect the subform to be able to see the properties and methods of the MainForm, EG. MainForm.MyProperty etc. but in .Net although I can see MainForm and all base properties I can't see any of my custom ones.
>
>Code looks like this
>
>namespace WindowsTestApplication
>{
> public partial class MainForm : Form
> {
> public int publicInt = 0;
> private string privateString = "Hello";
>
> public MainForm()
> {
> InitializeComponent();
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
> Form oSubForm = new Form();
> oSubForm.Show();
> }
> }
>}
>
>
>
>namespace WindowsTestApplication
>{
> public partial class SubForm : Form
> {
> public SubForm()
> {
> InitializeComponent();
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
> // Want to reference MainForm here.
> }
> }
>}
>
>
>Thought in the above I would be able to see 'publicInt' but not see 'privateString', but I can't see either.
I think your code is a bit wonky in that you are not actually creating an instance of SubForm(). IAC if you want SubForm to access properties and methods of MainForm you will probably want to pass a reference to it the constructor:
SubForm oSubForm = new SubForm(this);
oSubForm.Show();
and in the 'child' form:
public SubForm(MainForm mainform)
{
InitializeComponent();
int i = mainform.publicInt;
string s = mainform.privateString; // Inaccessible due to protection level
}