Using Objects in ASP.NET: Part 1/2 - The Constructor
(Page 5 of 6 )
Next, we define our constructor with the following code:
Public Sub New()
TurnOff()
End Sub Remember that the constructor will run when we first initialize our class or use it in another program. We’ll do this shortly. Our constructor here simply calls the TurnOff() function. This makes sense, as most appliances are not automatically running as soon as you buy them.
The TurnOn() and TurnOff() methods are pretty simple and admittedly, exist in our code more for educational purposes. We already have the CurrentPower property to programmatically set the power on or off. However, these two methods are handy as a quick way to do the same thing. They also return a string value by calling our GetStatus() method which is useful for display purposes. After the methods turn the power on or off using the CurrentPower property, they call the get status to return a string. Let’s look at the GetStatus() method:
Private Function GetStatus() As String
Return "The " & Me.GetType().ToString() & "'s Power is " & Me.CurrentPower
End Function The function is returning a string that starts with the Return statement. This means that the string that we build is going to immediately return to the code that executed the GetStatus() method. We then append Me.GetType().ToString(). This looks complicated but is actually pretty simple. Remember that all our classes inherit from the .NET base class Object. We use Me.GetType() which returns the Type of class we’re using. Type, by the way, is a generic reference in .NET to data types such as integers, strings, arrays and objects. You can use the GetType() method on any object to ask it what class it belongs to. In this case, it is the Appliance class. We then use the ToString() method on that type which returns the name of the class as a string. Again, ToString() is another method built into .NET. You’ll see this later. Finally we append more text to the string to display whether the power is on or off.
If you are lost at this point I’ll summarize. We have built sort of a chain reaction here. The TurnOn() and TurnOff() methods grab a string from the GetStatus() method. This method builds a string displaying the name of the class and whether it is on or off. It then returns the string to the TurnOn() or TurnOff() method. These methods then return that same string to whatever is calling them. You’ll see how this works when we use this class in a web page below.
DisplayPage.aspx Next we’ll create a page called DisplayPage.aspx so we can actually use our new appliance class in a web page. As I mentioned earlier, I use the code-behind method in Visual Studio.NET so I’ll break up my code into two files: The main DisplayPage.aspx which will contain only display code and HTML, and the DisplayPage.aspx.vb file which will hold all the programmatic functionality that our page is going to use.
DisplayPage.aspx <%@ Page Language="vb" AutoEventWireup="false" Codebehind="DisplayPage.aspx.vb"
Inherits="ASPObjectsVB.DisplayPage"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ASP.NET Objects</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="lblDisplay" runat="server"></asp:Label>
</form>
</body>
</HTML> Not much is going on here. In fact there are only two places of note in this code. One is our page directive:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DisplayPage.aspx.vb" Inherits="ASPObjectsVB.DisplayPage"%> The codebehind attribute instructs Visual Studio .NET which file acts as the code-behind for this page so it knows which file to open up when we want to view the code. The Inherits attribute does the real work and tells this page that it inherits from class ASPObjectsVB.DisplayPage. We’ll work on creating our own inheritance relationships in Part II but, for now, if you haven’t used the code-behind method before be aware that our DisplayPage.aspx inherits all the members in DisplayPage.aspx.vb which defines the DisplayPage class.
The only other important piece is our <ASP:Label> tag that creates an ASP Label Server control on this page. We’ll programmatically access this label for display purposes.
Now let’s look at our code-behind file, which contains the "guts" of our web page:
DisplayPage.aspx.vb Imports System.Threading
Public Class DisplayPage
Inherits System.Web.UI.Page
Protected WithEvents lblDisplay As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim appl As New Appliance()
Me.lblDisplay.Text = appl.TurnOn()
Me.lblDisplay.Text &= "<BR>" & appl.TurnOff()
'We can just use the TurnOn() function without doing anything
'with the string it returns. It will still turn the apppliance
'on.
appl.TurnOn()
Me.lblDisplay.Text &= "<BR> Our Appliance's CurrentPower property is " &
appl.CurrentPower
'We can write to the CurrentPower property if we wish:
appl.CurrentPower = "Off"
Me.lblDisplay.Text &= "<BR> Using the property, we turned it back off."
Me.lblDisplay.Text &= "<BR> The appliance is " & appl.CurrentPower
End Sub
End Class Much of the code above was automatically written by Visual Studio .NET (which is really handy by the way). For this article, I only want to concentrate on a few places in this code. You’ll notice that the code-behind file is itself a public class that inherits from System.Web.UI.Page. Every .aspx page you write and also every .aspx.vb file you write (if you choose to use the code-behind method) inherits from this class either directly or indirectly. This gives our pages and code-behind files access to all the members we need for web development such as the request and response properties, application management, page level events, etc.
Next, we define the lblDisplay server control. VS.NET writes this into my code-behind automatically, so I can programmatically access the <asp:label> server control defined on my DisplayPage.aspx file. Visual Studio defines this as a protected member so other classes can inherit it.
As you are probably aware, the Page_Load procedure is an event handler that fires when the page first loads. If you are not very familiar with ASP.NET’s event system, you might want to look at this article on Microsoft’s MSDN website. Our method simply accesses the lblDisplay and starts adding new text to its text property so we can see our Appliance class doing something. I create an instance of our Appliance class using the following code:
Dim appl As New Appliance() This is equivalent to the following:
Dim appl
Appl = New Appliance() We’re declaring the appl variable as a local variable to our procedure and instantiating it using the New statement. Back in our appliance class, our constructor gets run when the New statement is used. If you recall our constructor looked like this:
Public Sub New()
TurnOff()
End Sub So our newly created appliance starts out turned off. Next we’ll turn our appliance on:
Me.lblDisplay.Text = appl.TurnOn() Recall that the TurnOn() and TurnOff() methods not only alter whether the appl object is on or off, but they also return a string displaying the class name and its power setting. However we can also just run these methods without doing anything with the string that is returned:
appl.TurnOn() Here we just execute the function. We then test it out using the following code:
Me.lblDisplay.Text += "<BR> Our Appliance's CurrentPower property is " & appl.CurrentPower We’re writing out some text manually and accessing the appl.CurrentPower property. When the CurrentPower is read from, the following code is executed back in our CurrentPower property in our Appliance class:
Get
If _power = True Then
Return "On"
Else
Return "Off"
End If
End Get The Get block here checks our protected _power field for whether it is true or false and returns the more friendly On or Off strings. We can also do the exact opposite with this code:
'We can write to the CurrentPower property if we wish:
appl.CurrentPower = "Off"
Me.lblDisplay.Text &= "<BR> Using the property, we turned it back off."
Me.lblDisplay.Text &= "<BR> The appliance is " & appl.CurrentPower Here we are writing to the CurrentPower property and setting its value to Off just as if it were a standard field or variable. However, this really runs the Set block of code back in our Appliance class:
Set(ByVal Value As String)
If Value = "On" Then
_power = True
Else
_power = False
End If
End Set The set block acts like a method inside our class, accepts the new string Value and determines what to do with the protected _power field.
To see the results at work, you’ll need to compile and run the application. Remember that unlike .aspx pages, our class and code-behind files must be compiled using the SDK compiler or built using VS.NET. The results will look like the following:

Next: Conclusion >>
More ASP.NET Articles
More By Wrox Team