Huh? Sorry, I always try for clever wordplay :-) The usage of variables as seen is direct and to the point. But what if we had someone trying to build a house with color's that simply won't do? Well aside from our field assignment, I'll now demonstrate fields kicked up a notch - these being properties that allow you to perform conditional testing to prevent any faulty or unwanted values from being passed in.
Within our properties you deal with two accessors - Get
and Set
, that work in a read-write fashion. Get retrieves and references any calls from within, for which Set assigns our value when called.
In addition, our summary method now employs our properties as an alternative to our simple variables.
[C#]
public class House {
//Exposed variables
private string ShowSize;
private string HseBuilder;
private string HseColor;
//Private properties
public string ShowBuilder {
get {return HseBuilder;}
set {HseBuilder = value;}
}
public string ShowColor{
get {
if (HseColor == "red"){
return HseColor + ". But I'll change it to white.";
}else{
return HseColor;
}
set {HseColor = value;}
}
public string Summary() {
return ShowBuilder + " is building a new house that's " + ShowSize + " and is painting it " + ShowColor;
}
}
[VB]
Public Class House
Private ShowSize As string
Private HseBuilder As string
Private HseColor As String
Public Property ShowBuilder As String
Get
return HseBuilder
End Get
Set
HseBuilder = value
End Set
End Property
Public Property ShowColor As String
Get
If (HseColor = "red") Then
Return HseColor & ". But I'll change it to white."
Else
Return HseColor
End If
End Get
Set
HseColor = value
End Set
End Property
Public Function Summary() As String
Return ShowBuilder & " is building a new house that's " & ShowSize & " and is painting it " & ShowColor
End Function
End Class
And in our .aspx
page we can add, and by now you should've gotten the VB conversion idea as well, the code below:
House MyOtherHouse = new House();
MyOtherHouse.HseBuilder = "Dude";
MyOtherHouse.ShowSize = "looks ok";
MyOtherHouse.HseColor = "red";
Response.Write (MyOtherHouse.Summary());
So after all's said and done our results will look like this:
The first and last values of our summary method are utilized from our private properties ShowBuilder()
, and ShowColor()
that incorporates our conditional statement checking that if someone tries to paint our house red, we'll change that to white. The second one is simply from our plain variable - ShowSize
. Therefore, we can clearly observe how properties are like super sized variables. Both are accessed the same way, but properties have a little more power over what you permit to take place.
Notice how we use words like public
or private
in front of our methods, properties or variables? These are what are generally known as access modifiers . In our object, recall how our HseBuilder
, HseSize
, and HseColor
variables are public. With access modifiers we implicitly determine for our object's user what members they will be allowed to be modify or gain access to, their scope in other words.
In our example, the properties that retrieve these assigned public variables and analyze their content are private , and accessible to the class itself and cannot be accessed externally. The true nature of OOP insists on such, that the whole intent is defined by their proper usage, determining their external access allowances.
There are five allowed modifiers or accessibility levels in C# and they consist of:
-
public
- Full access is allowed to this -
private
- Accessible from within the calls that contains it -
internal
- Can only be accessed from the current DLL or assembly.Friend
in VB. -
protected
- Like private above, and could be access in a class derived from the one it's in -
protected internal
- Can only be accessed from any derived class, itself and like internal - the current DLL it resides in. In VB this would beProtected Friend
.
Note: Any time you don't specify any modifiers to a variable, it will default to private . Likewise, on any properties or methods , its modifiers default to public .
Moreover, as far are modifiers are concerned, others exist, for both variable fields and methods, that won't be looked into here, like extern
, volatile
, sealed
, NotInheritable
or NotOverridable
, etc. In light of the article's objective, .NET's exhaustive documentation would suffice better in clarifying any terms not delved into here. Still, upon reading this guide, any other OOP terms and their supposed uses won't be nearly as challenging to figure out as they may have once been.
Comments