Inherit from window form, text box is protected ?

M

marcusy3k

Hi,

I have a base form, it has 2 textboxs (textbox1, textbox2).

When I inherit a new form from the base form, all of the textbox controls
appear correctly, however, when I try to add below code on the new form in
order to assign a value to textbox1, e.g.

textbox1.Text = "123";

the compiler said the "textbox1.Text is inaccessible due to its protection
level"

how can I fix it ?


Thanks,
Marcus
 
P

Peter Duniho

marcusy3k said:
Hi,

I have a base form, it has 2 textboxs (textbox1, textbox2).

When I inherit a new form from the base form, all of the textbox controls
appear correctly, however, when I try to add below code on the new form in
order to assign a value to textbox1, e.g.

textbox1.Text = "123";

the compiler said the "textbox1.Text is inaccessible due to its protection
level"

how can I fix it ?

The best way to fix it is to, for each control of interest, abstract the
control features you need access to via a property in the base class.
For example:

class BaseForm : Form
{
public string Field1
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}

// In Designer-generated code, this field is actually
// declared in the *.Designer.cs file
private TextBox textBox1;
}

A poor way to fix it would be to change the protection modifier for each
control from "private" to "protected". It would work, but it gives the
child form carte blanche access to the control instance and its field,
which can lead to maintenance and code-correctness problems.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top