Forms Inheritance

  • Thread starter Thread starter Mojtaba Faridzad
  • Start date Start date
M

Mojtaba Faridzad

Hi,

I have some forms that they need to have access to some public variables. to
solve this problem I decided to have a RootForm and all other forms are
drived from this form. I can set my public variables on this RootForm and
use them in the drived forms. sounds good? but there is a problem here. I
don't have access to the public variables in RootForm!!! the RootForm is
like this:

namespace myprog
public class RootForm : System.Windows.Forms.Form
{
public string myvar = "test";
.....
}

now the other form I defined like this:

namespace myprog
public class OtherForm : myprog.RootForm
{
....
MessageBox.Show(this, RootForm.myvar);
....
}

but I don't have access to RootForm.myvar. what's the problem and how can I
use public variable in my program?

thanks
 
Mojtaba,

When you say:

RootForm.myvar

You are looking for a static instance of the string on the RootForm
type. However, you don't want this, you want to access the instance
variable for your instance, which you would do by just using "myvar".

Your definition would look like this:

MessageBox.Show(this, myvar);

Hope this helps.
 
Back
Top