Link to home
Start Free TrialLog in
Avatar of volking
volking

asked on

Public forced to Protected ... How to stop?

I have a C# webform in Visual Studio 2003 with the following ...

protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Label lblInfo;
protected System.Web.UI.WebControls.Label lblName;

I manually change it to ...

public System.Web.UI.WebControls.HyperLink HyperLink1;
public System.Web.UI.WebControls.Label lblInfo;
public System.Web.UI.WebControls.Label lblName;

works fine for awhile ... then KAPUT! VS IDE forces it back to ...

protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Label lblInfo;
protected System.Web.UI.WebControls.Label lblName;

WTF?
How to stop VS IDE from changing PUBLIC back to PROTECTED?
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

You can't stop the IDE from doing that (that I'm aware of).

The only solution would be to maybe encapsulate your controls as properties if you really want them to be public...

Wint.
Avatar of Carl Tawn
Can't remember if this is the same for 2003 or not but highlight the control in the designer and open its properties window and see if there is a property called "Modifier".
This happened to a colleague of mine before.  After a good discussion we agreed that it was bad practice to put public fields/controls in general and that VS actions was actually justified.  Can you think of a time when you MUST have a public field/control if it is yes then perhaps your objects are not designed correctly.  Don't take this as a criticism please.  It is my experience but then I am not that experienced
Avatar of volking
volking

ASKER

@carl_tawn - no "modifier" just "EnableViewState" and "Visible"
I agree with gbzhhu, public modifiers on labels etc should be avoided if possible, the advantage of using properties is that you can provide just a getter which is at least a little bit safer than a full blown public field.

Wint.
Avatar of volking

ASKER

Maybe I'm doing it ALL WRONG ...

I change declares to public so that, from the form hosting the control, I can set values within the control. Like this ...

The control is:  MyMenu.ascx
The control contains a label: lblInfo

The control has been dropped onto:  MyForm.aspx

In MyForm.aspx codebehind I can simply say:   this.MyMenu1.MyLabel.Text = "Whatever";

Works fine if lblInfo has been declared public ...

Am I doing it wrong? How should I set control values from the host form?

SOLUTION
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of volking

ASKER

Excellent!

I changed this ....

public System.Web.UI.WebControls.Label lblSchoolName;
public System.Web.UI.WebControls.HyperLink HyperLink1;

back to this ....

protected System.Web.UI.WebControls.Label lblSchoolName;
protected System.Web.UI.WebControls.HyperLink HyperLink1;

and added this ...

public System.Web.UI.WebControls.Label LblSchoolName { get { return lblSchoolName; } }
public System.Web.UI.WebControls.HyperLink hyperLink1 { get { return HyperLink1; }

Works GREAT! Thanks all!