use of properties - get - set

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

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
 
Hi,
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
 
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
 
Laurent Bugnion [MVP] napisał(a):
Hi,


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top