What & Why : Properties :: Part 2


In the part 1 of this series, http://www.c-sharpcorner.com/UploadFile/rsharma/Properties111142005014237AM/Properties1.aspx, I discussed about the get method of the properties, with the help of which you can make your variable so that nobody can modify the value of the variable. This was really a great use of properties and win-win situation over normal variables. 

At the end of the part 1, I asked you about some another method to do the same thing (By which no body can change the value of your variable) and the solution is read only variables.

Read only variables are the variables, you can set the value of the variable once only and can not change it later and that is actually exactly what we want.

Example C :

/////////// ExC.cs ////////////
namespace Rahul
{
using System ;
class Colors
{
public static void Main()
{
UrChoice UrColor =
new UrChoice() ;
//Overwriting your favorite color to yellow
UrColor.Color = "Yellow" ; // This line causes an error
Console.WriteLine("Your favorite color is " + UrColor.Color) ;
}
}
public class UrChoice
{
// Want to provide the access to everybody.
public readonly string Color ;
// Default Constructor setting up your favorite color
public UrChoice()
{
Color = "White" ;
}
}
}

So when we try to compile this program as: csc ExC.cs

We get this error:

A readonly field can not be assigned to (except in a constructor or a variable initializer)

And the result is, that nobody can change the value of a readonly variable once this is set in the constructor (As we did in Example 'C') or when initializing at the time of declaration. Now once you have set the value of your readonly variable, nobody can change it later in the program and if he does so he gets an error like we got in our Example 'C' because we tried to change the value of readonly variable 'Color' to "Yellow" in Main method.

Now remove the line

UrColor.Color = "Yellow" ; // This line causes an error  

From your C# code and again try to compile the program as: csc ExC.cs

Program has been compiled successfully.

Now run your program as: ExC (Press Enter Key)

Output is: Your favorite color is White.

Yes! Of course we got the right output.

Now we'll discuss about the set method of the properties.

What if I want a variable to allow everybody to modify it by his own value but can not read it.

We are having a powerful tool to provide such strength to our normal variable and again the answer is property but this time we'll forget about the get method of the properties rather we'll use set method of the properties.

As name suggests that get method of a property provides you the access to the value of the variable (you can read the value of the variable or can get the value) while set method provides you to modify the value of the variable (you can modify or set the value of variable).

Example D:

////////// ExD.cs //////////////
namespace Rahul
{
using System ;
class Colors
{
public static void Main()
{
UrChoice UrColor =
new UrChoice() ;
//Overwriting your favorite color to yellow
UrColor.Color = "Yellow" ;
Console.WriteLine("Your favorite color is " + UrColor.Color) ;
}
}
public class UrChoice
{
// your private variable to store your favorite color
private string MyColor ;
// Defining property, want to provide the access to everybody.
public string Color
{
set
{
MyColor =
value ;
}
}
// Default Constructor setting up your favorite color
public UrChoice()
{
MyColor = "White" ;
}
}
}
 
As you can see in the set method of the Color property, we have assigned the value to the MyColor variable.

What is this value?

Actually, this value you can consider as hidden variable to which the value is assigned when we modify the value of the property (In our example we modified the value as UrColor.Color = "Yellow", so here the value "Yellow" is assigned to the hidden variable named as 'value' that is associated to the concerned property means this hidden variable named 'value' is available to every property and when you access this in the set method you get the value of that particular property only.
But if you have not defined the get method of the property, you can not read the value of the variable (property), you can only set (modify) the value of the variable.

This time when we compile this program as: csc ExD.cs

We get an error:

The property or indexer 'Rahul.UrChoice.Color' can not be used in this context because it lacks the get accessor.

In this case we get this error because we can not read the value of the property rather we can only modify the value. Therefore when we try to read the value of the Color property in Conole.WriteLine we get this error.

Example E:

////////// ExE.cs //////////////
namespace Rahul
{
using System ;
class Colors
{
public static void Main()
{
UrChoice UrColor =
new UrChoice() ;
//Overwriting your favorite color to yellow
UrColor.Color = "Yellow" ;
}
}
public class UrChoice
{
// Your private variable to store your favorite color
private string MyColor ;
// Defining property, want to provide the access to everybody.
public string Color
{
set
{
MyColor =
value ;
}
}
// Default Constructor setting up your favorite color
public UrChoice()
{
MyColor = "White" ;
}
}
}

This time when we compile the program we do not get any error. So we got the success because now we have a variable that can be modified but can not be read. This looks a great feature but do we really need it somewhere in real life (programs)? Yes! Of course we do!!

I'll discuss the real program implementation of the benefit of this technique in the next part of this series.  

Is this all about properties?

We can define a variable that can be read only and we can define a variable that can be modifies only.

Can we define a property, which can be read, and write both?

Yes! This is very easy, just define both get and set method, as we are going to do in the next example.

Example F:

////////// ExF.cs //////////////
namespace Rahul
{
using System ;
class Colors
{
public static void Main()
{
UrChoice UrColor =
new UrChoice() ;
//Overwriting your favorite color to yellow
UrColor.Color = "Yellow" ;
Console.WriteLine("Your favorite color is " + UrColor.Color) ;
}
}
public class UrChoice
{
// Your private variable to store your favorite color
private string MyColor ;
// Defining property, want to provide the access to everybody.
public string Color
{
set
{
MyColor =
value ;
}
get
{
return MyColor ;
}
}
// Default Constructor setting up your favorite color
public UrChoice()
{
MyColor = "White" ;
}
}
}

Now in this example we have defined both get and set method of the Color property and when we compile the program as: csc ExF.cs

Program successfully compiles and when we run the program as: ExF (Press Enter Key)

Output is: Your favorite color is Yellow. 

But this is like a normal variable, so what else special we can do with the combination of get and set method. 

I'll discuss the prominent feature of get and set methods of the properties in the next part, that will prove that when we use both get and set method on our property we can do many more things than normal variables.  

Part 1: http://www.c-sharpcorner.com/UploadFile/rsharma/Properties111142005014237AM/Properties1.aspx

Up Next
    Ebook Download
    View all
    Learn
    View all