ASP.NET: An introduction to classes and properties
From Wiki
Summary: A beginners guide to creating classes and properties
Need help with ASP.NET? Come and ask a question in our ASP.NET Forum
In the forums I frequent, I often see a lot of beginners who want to use ASP.NET but haven't quite understood how implement an Object Orientated approach. I've tried to come up with the simplest of methods to demonstrate how a class can be used in this approach, and how properties of said class can be set and retrieved.
Let's start by creating an example class which has one property and one function. Create a new class, call it "ExampleClass" and place it inside your App_Code folder (which will allow us to access it from anywhere in our project). Add the following code to this class:
- Public Class ExampleClass
- Public Property ExampleNumber() As Integer
- Get
- ' Return the value of the private variable
- Return _ExampleNumber
- End Get
- Set(ByVal value As Integer)
- ' Set the value of the private variable
- _ExampleNumber = value
- End Set
- End Property
- ' A private variable that we'll set from out public property
- Private _ExampleNumber As Integer = 0
- ' An example function that will multiply our private variable by the number specified
- Public Function MultiplyBy(ByVal value As Integer)
- Return _ExampleNumber * value
- End Function
- End Class
You'll notice the property at the top (named "ExampleNumber") has a type of Integer and has two methods (Get and Set). The Get method simply returns the value of a private variable, whereas the Set simply sets the private variable. The reason we've created this extra private variable is so that user's can't directly set it and, if needed, we could perform some validation inside the Set method before we set the value of the private variable. We don't need to in this example, but it's good practice to get into so there's no harm done by including it here.
Our "MultiplyBy" function simply multiplies the private variable by whatever number is passed into the function.
Now, we need to create a new page with 3 labels on it to show how the class can be used:
- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="ExamplePage.aspx.vb" Inherits="ExamplePage" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- </div>
- <div>
- <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
- </div>
- <div>
- <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
- </div>
- </form>
- </body>
- </html>
In the code-behind for this page, we'll create a Page Load event and this is where we'll access our class from. First, we create a new instance of the class, then we can read and write to our property and also call the function we created. Here's the code-behind for the page we just created that will demonstrate this:
- Partial Class ExamplePage
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- ' Declarations
- Dim ex As New ExampleClass
- ' See what the property currently holds
- Label1.Text = "Before we set the value: " & ex.ExampleNumber
- ' Set the property to equal something
- ex.ExampleNumber = 10
- ' See what the property currently holds
- Label2.Text = "After we set the value: " & ex.ExampleNumber
- ' Call the example MultiplyBy function
- Label3.Text = "After a call to the function: " & ex.MultiplyBy(100)
- End Sub
- End Class
When you run this code, you should see the following output:
- Before we set the value: 0
- After we set the value: 10
- After a call to the function: 1000
This Hack is part of the ASP.NET Hacks collection


