Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Visibility of objects, properties & methods
Message
From
04/11/2010 13:35:01
 
 
To
04/11/2010 12:11:43
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01488251
Message ID:
01488268
Views:
63
>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
        }
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform