469,352 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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 29747
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rick Austin | last post: by
3 posts views Thread by Martin Montgomery | last post: by
7 posts views Thread by Donald Grove | last post: by
reply views Thread by =?Utf-8?B?UmljayBHbG9z?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.