Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Public forced to Protected ... How to stop?

Posted on 2006-07-03
9
Medium Priority
?
247 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:volking
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17031592
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.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17031641
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".
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 17031708
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Author Comment

by:volking
ID: 17031715
@carl_tawn - no "modifier" just "EnableViewState" and "Visible"
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17031733
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.
0
 
LVL 5

Author Comment

by:volking
ID: 17031769
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?

0
 
LVL 12

Assisted Solution

by:gbzhhu
gbzhhu earned 800 total points
ID: 17031852
You should declare lblInfo as private (or protected if you need access to it project wide) then in MyMenu.ascx add a public property that will return lblInfo  or even just string (for get) and sets the lblInfo.text (for set)

I believe this is the OO way.  
0
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 1200 total points
ID: 17031879
Yes, the lbl would be declared as:

 protected Label lblInfo...

and a property like:

  public Label LblInfo { get { return lblInfo; } }

you'd then use it as:

   LblInfo.Text = "TEST";

What the property will stop you doing is:

  LblInfo = new Label(); //!! ERROR !!

Wint.
0
 
LVL 5

Author Comment

by:volking
ID: 17032149
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!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question