Using Objects in ASP.NET: Part 1/2 - Class Accessibility (Page 4 of 6 )
I mentioned earlier that classes are usually declared Public. This is so you can use the classes within other classes or .NET projects. However, it is possible to limit class access in a similar manner that you do data members. Classes can be declared as Public, Friend or Private. This can limit the use of the class to only within the same file (Private) or within the same project (Friend.)
Using Objects in ASP.NET While taking a college class on OOP Analysis and Design, I became very enthusiastic because I could see the benefit of modeling and writing software in an object oriented fashion. We used UML (Unified Modeling Language) diagrams to model how software components would be designed and interrelate. Unfortunately, we did not write any software using these techniques. Once I started trying to write more OOP code in various languages, I found it difficult to know how to translate theory into code. Some features, such as encapsulation and polymorphism, are more or less built in. In the case of encapsulation, when you build an object, much of its inner workings are hidden by default, unless you explicitly state them as "Public" to be used by other components. Other features, such as inheritance and aggregation, require a little more thought ahead of time in order to use effectively.
In order to start small, weíll write three simple files. If you are using Visual Studio.NET as I do, you can start a new project called ASPObjectsVB. Weíll create our first class, Appliance.vb. Weíll also create our web page, DisplayPage.aspx and itís code-behind file DisplayPage.aspx.vb. I use the code-behind method to create my web applications in VB.NET because I find it similar to writing standard VB.NET and C# applications. However, you can simply choose to take the code in the DisplayPage.aspx.vb file and put it into server-side script tags in the DisplayPage.aspx file or manually write and compile the code-behind files without VS.NET.
First letís make our Appliance class. This will be a simple class that emulates a generic appliance in the real world. As such it will have features such as TurnOn() and TurnOff(). As a personal note, I like to comment my example code so you have documentation about each block, but I hate typing in comments when Iím doing other peopleís tutorials. So feel free to ignore them if you are typing this in by hand.
Public Class Appliance '_power is used internally as a true/false switch for 'whether the appliance is on. Protected _power As Boolean
'The CurrentPower property gives us a more natural way 'of saying whether the power is on than the "_power" boolean 'field above Public Property CurrentPower() As String Get If _power = True Then Return "On" Else Return "Off" End If
End Get Set(ByVal Value As String) If Value = "On" Then _power = True Else _power = False End If End Set End Property
'When the appliance is first created, the power is off. Public Sub New() TurnOff() End Sub
'While the CurrentPower property does almost the same thing 'As these two functions/methods, they demonstrate the use 'of methods now and help us demonstrate inheritance and polymorphism 'later. Public Overridable Function TurnOn() As String Me.CurrentPower = "On" Return GetStatus() End Function
Public Overridable Function TurnOff() As String Me.CurrentPower = "Off" Return GetStatus() End Function
'Since all classes inherit from the Object class, I can use the 'GetType() method in this helper function to figure out what class 'this object is, programmtically. This is useful when using inheritance, 'especially during development and debugging. Private Function GetStatus() As String Return "The " & Me.GetType().ToString() & "'s Power is " & Me.CurrentPower End Function
The first line of code creates our class Appliance. Next we define a field called _power. Remember fields are variables that are declared right after the class definition (as opposed to being declared inside a function or procedure) and can be accessed by any methods (procedures or functions) inside that class. I declared this as protected so that it is not publicly accessible outside this class but can still be inherited by other classes. The _power field is a Boolean variable, because it will represent whether the power is on or off. Boolean is the closest data type for this type of value as it also can only be in one of two states: true or false.
About Properties The next member to be defined is the CurrentPower property. While it might be handy to emulate "Power" as a Boolean value (true / false) from inside our class, at other times it is more humanly readable to refer to the power as "On" or "Off". For this, weíve constructed a public property called CurrentPower. CurrentPower acts as wrapper for the protected _powe r field. From outside the class, we can set the _power field to True by passing the CurrentPower property a string value called On. This is written in the Set block. Similarly, when we read the CurrentPower field, it will check the _power field and if it is true, it will return the string Off. This is defined in the Get block of code and uses the Return statement to send back the On or Off string. The CurrentPower property keeps our Appliance object in a consistent state, so that if the user of the class puts in something strange instead of On or Off the property will set our _power field to false by default.
This is a good practice and partially what properties are designed for. Most developers do not make their fields public as it makes the class easy to break easily and violates encapsulation. Instead, they use properties or methods to manipulate private or protected fields and ensure that the fields never have an illegal value.
In Java, there are no properties in the sense that Microsoft has provided us with VB and C#. Instead, the developer must make something called "Get" or "Set" methods that modify the internal fields. For example to Set a property in Java, you would actually be making a call to a method similar to the following:
Compare this to VB.NET:
ObjUser.Name = username
As you can see, the latter is a more natural way of setting a property. It "feels" more like we are simply changing a fieldís value, even though when the property value is set, any code can be executed just as if we were using a standard method. This can include validation code to ensure the new value is in a certain range or size or anything else that might help keep your program in a consistent state.
Me In appliance.vb, I am using the prefix Me for many of my variables. In C#, Jscript, Java and C++ this prefix is called this but essentially means the same thing. While itís not always necessary, itís often useful for documentation purposes to prefix your class fields so you know they are fields and not just a variable local to a method. If you declare a variable inside a method with the same name as one of your fields, than you MUST use the Me to scope your variable when you want to reference the field. Otherwise, the compiler believes you are referencing the variable local to that function or procedure.