Home arrow ASP.NET arrow Page 4 - Using Objects in ASP.NET: Part 1/2

Using Objects in ASP.NET: Part 1/2

Obects are a big part of .NET. In this 2 part article series, Craig shows us how to use objects with ASP.NET, focusing mainly on providing clear working examples.

Author Info:
By: Wrox Team
Rating: 5 stars5 stars5 stars5 stars5 stars / 139
February 25, 2003
  1. · Using Objects in ASP.NET: Part 1/2
  2. · Some of the Grander OOP Features
  3. · Class Members
  4. · Class Accessibility
  5. · The Constructor
  6. · Conclusion

print this article

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
If _power = True Then
Return "On"
Return "Off"
End If

End Get
Set(ByVal Value As String)
If Value = "On" Then
_power = True
_power = False
End If
End Set
End Property

'When the appliance is first created, the power is off.
Public Sub New()
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
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

End Class

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.

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.
blog comments powered by Disqus

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials