From: marcusy3k on
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


From: Peter Duniho on
marcusy3k wrote:
> 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
From: marcusy3k on
Thanks a lot! Pete, it really works! ^_^


 | 
Pages: 1
Prev: winform and flash
Next: Hyperaccess using C#