473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Protected or Public ?

I have 30 textBox in my UserControl...they are with PROTECTED modifier.
But i need to access these TextBox throught my WebForm...
What is the best way to access them ? leaving PROTECTED and creating publics
properties or Leaving de objects publics ?

--
Please, check my theSpoke:
http://www.thespoke.net/MyBlog/dgroh/MyBlog.aspx
Nov 19 '05 #1
6 1923
Paperback Writer

If you want them exposed from the control, then you want to expose them
as public, or internal (if they are hosted in the same assembly), that is
the only way you will be able to access them directly from the page the user
control is on.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paperback Writer" <ne*********@gmail.com> wrote in message
news:O2*************@TK2MSFTNGP15.phx.gbl...
I have 30 textBox in my UserControl...they are with PROTECTED modifier.
But i need to access these TextBox throught my WebForm...
What is the best way to access them ? leaving PROTECTED and creating
publics properties or Leaving de objects publics ?

--
Please, check my theSpoke:
http://www.thespoke.net/MyBlog/dgroh/MyBlog.aspx

Nov 19 '05 #2
You can expose them via public properties, this method is much better
because when you change the textBox from protected to public, visual
studio designer wil change them back,

If you are not using VS then its not a problem,

by the way this is fixed in VS 2005

Nov 19 '05 #3
You should avoid exposing controls within your user control as public
members. I almost wrote "You should never expose..." but I'm not
feeling that cocky.

The way to handle this problem (and it's a common one), is to think
about the user control as a whole. Obviously, it has 30 text boxes for
a reason. What is the sum functionality of the entire user control? Is
it, for example, an application form?

Assuming that it's an application form (for the sake of argument), then
you don't _really_ want to be able to get at the text boxes. What you
_really_ want is to get at the information on the form. So, you could
do that by exposing one public property for each item on the form:

public string LastName
{
get { return this.txtLastName.Text; }
set { this.txtLastName.Text = value; }
}

Or, you could create a class that would hold all of the application
information, and expose a single property:

public ApplicationInformation ApplicationInformation
{
get { return new ApplicationInformation(this.txtLastName.Text, ...
); }
set
{
this.txtLastName.Text = value.LastName;
...
}
}

Either way, what you've done is expose properties that "talk in the
language of" your user control as a whole. This does two very good
things: 1) it makes the calling code more readable, because callers can
now write code in terms of what the user control does, not in terms of
how it's put together, and 2) it makes the calling code ignorant of the
structure of your user control (loose coupling), so that you can
change, for example, a radio button group to a combo box if that makes
more sense from an interface point of view... all without changing any
calling code. The less your calling code knows about the guts of your
user control, the better.

You can do this with events, too (which is also common). WIthin your
user control you subcribe to events from the text boxes, and then in
the event handler you raise an event particular to the user control.
For example:

this.userControl.ApplicationInformationChanged += new
System.EventHandler(...);

and then inside your user control:

this.txtLastName.TextChanged += new
System.EventHandler(this.txtLastName_TextChanged);
.... etc ...
private void txtLastName_TextChanged(System.EventArgs ea)
{
OnApplicationInformationChanged();
}
.... etc ...
protected void OnApplicationInformationChanged()
{
if (ApplicationInformationChanged != null)
{
ApplicationInformationChanged(System.EventArgs.Emp ty);
}
}

Again, this isolates your caller from knowing about the controls within
your user control, and creates events that are meaningful at the level
of the user control rather than events that depend upon how the user
control is constructed.

Nov 19 '05 #4
Bruce, that was extreemly good advice, but the question was "how do I
expose", not "should I expose".

I do agree with everything you have said but sometimes it can be an
overkill to do it the way you explain.

It would be great if everything could be programmed the correct way all
of the time, but sometimes its just not feasable.

I believe that the whole point of control (aka component) architecture
of ASP.net is often not understood, but sometimes this is just to much
to take in for new ASP.net developers.

I hope I have not flamed you here, as I said before I agree with
everything you said

Nov 19 '05 #5
Was your question resolved? It looks like Bruce gave you a C# example of how
to expose properties in a user control. Here is the VB code to expose a
textbox which would allow you, for example, to change its css class,
visibility, text, etc. If you only need to work with the text, then you
could expose it as a string and return me.fname.text instead.

(Sorry if you already have your answer and this isn't relevant anymore.
Hopefully it will at least help someone else.)

Public Property FirstName() As TextBox
Get
Return Me.fname
End Get
Set(ByVal Value As TextBox)
Me.fname= Value
End Set
End Property
<ga**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Bruce, that was extreemly good advice, but the question was "how do I
expose", not "should I expose".

I do agree with everything you have said but sometimes it can be an
overkill to do it the way you explain.

It would be great if everything could be programmed the correct way all
of the time, but sometimes its just not feasable.

I believe that the whole point of control (aka component) architecture
of ASP.net is often not understood, but sometimes this is just to much
to take in for new ASP.net developers.

I hope I have not flamed you here, as I said before I agree with
everything you said

Nov 19 '05 #6
Hi there, I found your post really helpful..but i wondered if, once I have
exposed a public property containing the value of a textbox in a user
control..how do I grab this from the calling page? I cant think of the
syntax, since my page doesnt know the contents of the class (and therefore,
the public properties) of the user control??

Sorry if its blatently obvious!

I am currently getting a value using
Page.FindControl(usercontrol);usercontrol.FindCont rol...etc etc and am told
this is much slower than the public property route.

Many Thanks, Louise.

"Bruce Wood" wrote:
You should avoid exposing controls within your user control as public
members. I almost wrote "You should never expose..." but I'm not
feeling that cocky.

The way to handle this problem (and it's a common one), is to think
about the user control as a whole. Obviously, it has 30 text boxes for
a reason. What is the sum functionality of the entire user control? Is
it, for example, an application form?

Assuming that it's an application form (for the sake of argument), then
you don't _really_ want to be able to get at the text boxes. What you
_really_ want is to get at the information on the form. So, you could
do that by exposing one public property for each item on the form:

public string LastName
{
get { return this.txtLastName.Text; }
set { this.txtLastName.Text = value; }
}

Or, you could create a class that would hold all of the application
information, and expose a single property:

public ApplicationInformation ApplicationInformation
{
get { return new ApplicationInformation(this.txtLastName.Text, ...
); }
set
{
this.txtLastName.Text = value.LastName;
...
}
}

Either way, what you've done is expose properties that "talk in the
language of" your user control as a whole. This does two very good
things: 1) it makes the calling code more readable, because callers can
now write code in terms of what the user control does, not in terms of
how it's put together, and 2) it makes the calling code ignorant of the
structure of your user control (loose coupling), so that you can
change, for example, a radio button group to a combo box if that makes
more sense from an interface point of view... all without changing any
calling code. The less your calling code knows about the guts of your
user control, the better.

You can do this with events, too (which is also common). WIthin your
user control you subcribe to events from the text boxes, and then in
the event handler you raise an event particular to the user control.
For example:

this.userControl.ApplicationInformationChanged += new
System.EventHandler(...);

and then inside your user control:

this.txtLastName.TextChanged += new
System.EventHandler(this.txtLastName_TextChanged);
.... etc ...
private void txtLastName_TextChanged(System.EventArgs ea)
{
OnApplicationInformationChanged();
}
.... etc ...
protected void OnApplicationInformationChanged()
{
if (ApplicationInformationChanged != null)
{
ApplicationInformationChanged(System.EventArgs.Emp ty);
}
}

Again, this isolates your caller from knowing about the controls within
your user control, and creates events that are meaningful at the level
of the user control rather than events that depend upon how the user
control is constructed.

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.