473,511 Members | 15,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use of properties - get - set

Hi

I have been told about the following code:

private string boxLabel;

public string BoxLabel

{

get { return boxLabel;}

set {boxLabel = value;}

}

does this routine set the value of the private string to be the same as the
value input by the public string or is it the other way? Could you please
tell me in simple words what this is used for?

Thanks

doug
Jan 3 '07 #1
5 29881
Hi,

Doug wrote:
Hi

I have been told about the following code:

private string boxLabel;

public string BoxLabel

{

get { return boxLabel;}

set {boxLabel = value;}

}

does this routine set the value of the private string to be the same as the
value input by the public string or is it the other way? Could you please
tell me in simple words what this is used for?

Thanks

doug
Properties are a way to control access to your private members. The
example here above is very trivial, as it simply (set) copies the given
value to the private member and (get) returns the private member without
modifying it.

Often, the set part is used to check the given value in order to avoid
later errors, or to simplify code. For example, you can do something like:

private string myString = "";

public string MyString
{
set
{
if ( value == null )
{
value = "";
}
myString = value;
}
}

This code ensures that the private member "myString" can never be set to
null. This way, the code using myString doesn't need to check everytime
to avoid null reference exception.

There are many other ways to use properties, but generally, you can say
that they are used to control user's access to private members.

It is good practice to never return attributes, but always properties.
The reason is that it allows later changes to your code without
modifying the interface to your class. The code you posted, though
trivial, follows that rule.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 3 '07 #2
Further to Laurent's post... just to clarify for you...

there *is* no public string (as an instance). The property definition
(with "set" and "get") actually just defines access; it doesn't have
anywere to put the data; hence the need for a backing field (the
private string) which we use to store the value. As already mentioned,
this allows you a lot more control, for instance we can validate the
value first, or trigger actions (generally just events to trigger UI
updates) when the value changes, etc. It also allows various tricks
with the property not mapping directly to any single field - e.g. it
could be forwarding the request to existing properties on a related
class, or it could be aggregating data, or perhaps talking to a
database for lazy loading, but the caller just sees the simple
property.

Marc
Jan 3 '07 #3
Laurent Bugnion [MVP] napisał(a):
Hi,

Doug wrote:
>Hi

I have been told about the following code:

private string boxLabel;

public string BoxLabel

{

get { return boxLabel;}

set {boxLabel = value;}

}

does this routine set the value of the private string to be the same
as the value input by the public string or is it the other way? Could
you please tell me in simple words what this is used for?

Thanks

doug

Properties are a way to control access to your private members. The
example here above is very trivial, as it simply (set) copies the given
value to the private member and (get) returns the private member without
modifying it.

Often, the set part is used to check the given value in order to avoid
later errors, or to simplify code. For example, you can do something like:

private string myString = "";

public string MyString
{
set
{
if ( value == null )
{
value = "";
}
myString = value;
}
}

This code ensures that the private member "myString" can never be set to
null. This way, the code using myString doesn't need to check everytime
to avoid null reference exception.

There are many other ways to use properties, but generally, you can say
that they are used to control user's access to private members.

It is good practice to never return attributes, but always properties.
The reason is that it allows later changes to your code without
modifying the interface to your class. The code you posted, though
trivial, follows that rule.

HTH,
Laurent
You can use Properties too if you need one way direction "communication
with variables". For example

private string s;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public string Name
{
get {return s;}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
code above allows you to only read value of s, not to write(!).

Another idea is to combine return result depending on many than one
variable.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private bool var1, var2, var3;

.... // some code to modify values (i.e. test with aswer true or false)

public string TestResult
{
get
{
if((var1 && var2) || (var1 && var3) || (var2 && var3))
return "Test passed successfully";
else
return "You need to try again, good luck!";
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Generally what for you use properties ... it is up to you. It could only
return or set value from variable, raise event (before or after settings
a variable), get/set values to group of variables (like methods), test
something and many more.

best,
SÅ‚awomir
Jan 3 '07 #4
Properties are also useful for extensibility. You can override them in
derived classes.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Where there's a Will, there's a William.

"Slawek" <sp****************@o2.plwrote in message
news:en**********@node4.news.atman.pl...
Laurent Bugnion [MVP] napisal(a):
>Hi,

Doug wrote:
>>Hi

I have been told about the following code:

private string boxLabel;

public string BoxLabel

{

get { return boxLabel;}

set {boxLabel = value;}

}

does this routine set the value of the private string to be the same as
the value input by the public string or is it the other way? Could you
please tell me in simple words what this is used for?

Thanks

doug

Properties are a way to control access to your private members. The
example here above is very trivial, as it simply (set) copies the given
value to the private member and (get) returns the private member without
modifying it.

Often, the set part is used to check the given value in order to avoid
later errors, or to simplify code. For example, you can do something
like:

private string myString = "";

public string MyString
{
set
{
if ( value == null )
{
value = "";
}
myString = value;
}
}

This code ensures that the private member "myString" can never be set to
null. This way, the code using myString doesn't need to check everytime
to avoid null reference exception.

There are many other ways to use properties, but generally, you can say
that they are used to control user's access to private members.

It is good practice to never return attributes, but always properties.
The reason is that it allows later changes to your code without modifying
the interface to your class. The code you posted, though trivial, follows
that rule.

HTH,
Laurent

You can use Properties too if you need one way direction "communication
with variables". For example

private string s;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public string Name
{
get {return s;}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
code above allows you to only read value of s, not to write(!).

Another idea is to combine return result depending on many than one
variable.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private bool var1, var2, var3;

... // some code to modify values (i.e. test with aswer true or false)

public string TestResult
{
get
{
if((var1 && var2) || (var1 && var3) || (var2 && var3))
return "Test passed successfully";
else
return "You need to try again, good luck!";
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Generally what for you use properties ... it is up to you. It could only
return or set value from variable, raise event (before or after settings a
variable), get/set values to group of variables (like methods), test
something and many more.

best,
Slawomir

Jan 3 '07 #5
you said it yourself : set {boxLabel = value;}
"Doug" <go**********@optusnet.com.auwrote in message
news:45***********************@news.optusnet.com.a u...
Hi

I have been told about the following code:

private string boxLabel;

public string BoxLabel

{

get { return boxLabel;}

set {boxLabel = value;}

}

does this routine set the value of the private string to be the same as
the value input by the public string or is it the other way? Could you
please tell me in simple words what this is used for?

Thanks

doug


Jan 3 '07 #6

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

Similar topics

2
2991
by: Rick Austin | last post by:
I recently had to perform a reinstalltion of Windows XP (my registry seems to have become corrupt). After this completed I had to reinstall all applications since most use the registry for settings,...
4
7488
by: Lyn | last post by:
Hi, This question may seem a bit academic... To learn more about Access VBA, I have been enumerating the properties of various form controls. This was mostly successful and I have learned a lot...
10
7341
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
6
5168
by: JerryP | last post by:
Hello, is there a way to launch the property dialogue for a directory from my c# app ? I would also like to launch the User Account Properties from Active Directory Users and Computers, and the...
3
5072
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank ...
7
8506
by: Donald Grove | last post by:
Is it possible to retrieve field properties from a table in access2000 using code? I have tried: " dim dbs as dao.database dim tbl as dao.tabledef dim fld as dao.field dim prop as...
1
1648
by: Christophe Peillet | last post by:
I have a CompositeControl with two types of properties: 1.) Mapped Properties that map directly to a child control's properties (ex.: this.TextboxText = m_txt.Text). These properties are handled...
7
6051
by: Anderskj | last post by:
Hi! I am developing a c# application. I have a interface (which can change therefore my problem) If i do like this: List<PropertyInfoproperties = new List<PropertyInfo>();...
0
2811
by: =?Utf-8?B?UmljayBHbG9z?= | last post by:
For some unknown reason (user error?), I cannot get a NameValueCollection to persist in the app.config file. Unlike other settings, I cannot get the String Collection Editor GUI to allow my to...
4
9829
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
0
7252
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
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
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
7517
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...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.