ASP.NET
  Home arrow ASP.NET arrow Page 4 - Using Objects in ASP.NET: Part 1/2
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Using Objects in ASP.NET: Part 1/2
By: Wrox Team
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 39
    2003-02-25

    Table of Contents:
  • Using Objects in ASP.NET: Part 1/2
  • Some of the Grander OOP Features
  • Class Members
  • Class Accessibility
  • The Constructor
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    Appliance.vb

    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

    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:

    ObjUser.SetName(username);

    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.

    More ASP.NET Articles
    More By Wrox Team


     

    ASP.NET ARTICLES

    - 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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT