Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Install AJAX On Machines Running Visual Studio 2005

› Creating Pretty Popups Using AJAX

› Simple ASP Image Resize Function

› SQL Server Performance Counters

› ASP.Net Interview Questions And Their Answers

› Encode Url using ASP

› Difference Between DataGrid and GridView in Asp.Net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
 
Home> ASP.NET
 
An introduction to classes and properties - Asp.net
 
A beginners guide to creating classes and properties

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:
  1. Before we set the value: 0  
  2. After we set the value: 10  
  3. After a call to the function: 1000  
 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› ASP.NET Simple Connect To Database

› UpdatePanel control in Asp.net Ajax

› How to Send an Email in Asp.Net

› Ajax - The Basics

› How My Page Rank Went From 0 To 5 In One Update

› Encode Url using ASP

› Cache Data within your Application - Asp.Net

› What Kind of DBA Are You?

› Clear all Cached objects in Asp.Net

› Displaying Data using ASP.NET 2.0 DataList

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.