![]() |
Introduction to Classes |
Programmer-Defined Types
Introduction
A class is a technique of using one variable or a group of variables in one unit to describe an object.
Creating a Class
The primiary formula to create a class is:
Class class-name End Class
If you are creating the class in a webpage, include it in a script section. The primiary formula to follow is:
<script Language="VB" runat="server"> Class class-name End Class </script>
As an alternative, you can create the class in its own file. In that case, it must not be included in a script. Just create the class directly in a file and save the file with a .vb extension.
The section between the Class class-name and the End Class lines is the body of the class.
A class is a combination of values or variables. To create those values or variables, declare each variable inside of the body of the class. Here is an example:
<script runat="server">
Class Employee
Dim EmployeeeNumber As Integer
Dim FirstName As String
Dim LastName As String
Dim HourlySalary As Double
End Class
</script>
The items that compose a class are called members of the class.
Controlling the Access to a Class
A Private Class
A class is referred to as private if it can be accessed only within the file where it is created. To create a private class, precede it with the Private keyword. Here is an example:
<script Language="VB" runat="server">
Private Class Something
End Class
</script>
A Friendly Class
A class is friendly if it can be accessed by code within the same project, which includes code from other files. To create a friendly class, precede it with the Friend keyword. Here is an example:
<script Language="VB" runat="server">
Friend Class Somewhere
End Class
</script>
A Public Class
A class is a public one if it can be accessed throughout the project and by code of another project. To create a public class, precede the Class keyword with Public when creating it. Here is an example:
<script Language="VB" runat="server">
Public Class Again
End Class
</script>
Creating an Object
Introduction
After creating a class, to use it in a project, you must declare a variable for it. This is also referred to as creating an object or creating an instance of the class. To create an object, use the Dim keyword followed by a name for the object, the As keyword, and the name of the class. Here is an example:
<%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <script Language="VB" runat="server"> Public Class Employee End Class </script> <title>Department Store - Employee Record</title> </head> <body> <h4>Department Store - Employee Record</h4> <% Dim staff As Employee %> </body> </html>
A variable declared of a class is also called an object.
Allocating Computer Memory for an Object
To let the computer hold the values of an object, you must allocate some computer memory for it. To do this, use the New operator followed by the name of the class and assign this expression to the variable. Here is an example:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script Language="VB" runat="server">
Class Employee
End Class
</script>
<title>Department Store - Employee Record</title>
</head>
<body>
<h4>Department Store - Employee Record</h4>
<%
Dim staff As Employee
staff = New Employee
%>
</body>
</html>
Instead of first declaring the variable before allocating memory for it, you can take care of this directly when declaring the variable by inserting the New operator between the As keyword and the name of the class. This can be done as follows:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script Language="VB" runat="server">
Class Employee
End Class
</script>
<title>Department Store - Employee Record</title>
</head>
<body>
<h4>Department Store - Employee Record</h4>
<%
Dim staff As New Employee
%>
</body>
</html>
Accessing a Member of a Class
The Public Members of a Class
If you use the Dim keyword to create a member of a class, the member is said to be hidden. Such a member cannot be accessed outside the class (but it can be accessed by all other members of the class only in the body of the class). For a member to be accessible outside the class, the member must be made public. To make this happen, use the Public keyword in place of Dim to create the member. Here are examples:
<script runat="server"> Class Employee Public EmployeeeNumber As Integer Public FirstName As String Public LastName As String Public HourlySalary As Double End Class </script>
In our example, we marked all members as Public. This is not a requirement. You will (know to) decide when a member must be public and when a member must be created with theDim keyword.
The Period Operator: .
To access a member of a class outside the class, type the name of the variable, followed by a period, followed by the name of the member you want. Once you have accessed a member, you can use it. For example, you can assign a value to it. Here are examples:
<%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <script Language="VB" runat="server"> Class Employee Public EmployeeeNumber As Integer Public FirstName As String Public LastName As String Public HourlySalary As Double End Class </script> <title>Department Store - Employee Record</title> </head> <body> <h4>Department Store</h4> <p><b>Employee Record</b></p> <% Dim staff As Employee staff = New Employee staff.EmployeeeNumber = 29739 staff.FirstName = "Gregory" staff.LastName = "Plane" staff.HourlySalary = 18.85 Response.Write("Employee #: " & staff.EmployeeeNumber) Response.Write("<br>First Name: " & staff.FirstName) Response.Write("<br>Last Name: " & staff.LastName) Response.Write("<br>Hourly Salary: " & staff.HourlySalary) %> </body> </html>
This would produce:
You can also involve an accessed member in an expression. Here is an example:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script Language="VB" runat="server">
Class Payroll
Public PayrollID As Long
Public EmployeeeNumber As Integer
Public Monday As Double
Public Tuesday As Double
Public Wednesday As Double
Public Thursday As Double
Public Friday As Double
Public Saturday As Double
Public Sunday As Double
End Class
</script>
<title>Department Store - Employee Payroll</title>
</head>
<body>
<h4>Department Store</h4>
<p><b>Employee Payroll</b></p>
<%
Dim pay As Payroll
Dim timeWorked As Double
pay = New Payroll
pay.PayrollID = 100001
pay.EmployeeeNumber = 29739
pay.Monday = 8.00
pay.Tuesday = 9.00
pay.Wednesday = 8.50
pay.Thursday = 8.00
pay.Friday = 10.00
pay.Saturday = 0.00
pay.Sunday = 0.00
timeWorked = pay.Monday + pay.Tuesday + pay.Wednesday +
pay.Thursday + pay.Friday + pay.Saturday + pay.Sunday
Response.Write("Employee #: " & pay.PayrollID)
Response.Write("<br>Employee #: " & pay.EmployeeeNumber)
Response.Write("<br>Time Worked: " & timeWorked)
%>
</body>
</html>
This would produce:
Remember that, to create more than one statement on the same line, you can use the colon operator. Here is an example:
<%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <script Language="VB" runat="server"> Class Payroll Public PayrollID As Long Public EmployeeeNumber As Integer Public Monday As Double Public Tuesday As Double Public Wednesday As Double Public Thursday As Double Public Friday As Double Public Saturday As Double Public Sunday As Double End Class </script> <title>Department Store - Employee Payroll</title> </head> <body> <h4>Department Store</h4> <p><b>Employee Payroll</b></p> <% Dim pay As Payroll Dim timeWorked As Double pay = New Payroll pay.PayrollID = 100001 : pay.EmployeeeNumber = 29739 pay.Monday = 8.00 : pay.Tuesday = 9.00 : pay.Wednesday = 8.50 pay.Thursday = 8.00 : pay.Friday = 10.00 pay.Saturday = 0.00 : pay.Sunday = 0.00 timeWorked = pay.Monday + pay.Tuesday + pay.Wednesday + pay.Thursday + pay.Friday + pay.Saturday + pay.Sunday Response.Write("Employee #: " & pay.PayrollID) Response.Write("<br>Employee #: " & pay.EmployeeeNumber) Response.Write("<br>Time Worked: " & timeWorked) %> </body> </html>
With the Members of an Object
With a Class Instance
To access the members of a class using an object, you can use the With keyword. Type it followed by the name of the variable. Create a new line and type End With. The section between the With variable-name line and the End With line is the body of the With statement. In that body, to access a member of the class, type the period operator followed by the desired member. Here are examples:
<%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <script Language="VB" runat="server"> Class Depreciation Public Cost As Double Public SalvageValue As Double Public EstimatedLife As Integer End Class </script> <title>Depreciation - Straight-Line Method</title> </head> <body> <h4>Depreciation - Straight-Line Method</h4> <% Dim vehicle As Depreciation vehicle = New Depreciation With vehicle .Cost = 45800 .SalvageValue = 6000 .EstimatedLife = 6 Response.Write("Machine Cost: " & .Cost) Response.Write("<br>Salvage Value: " & .SalvageValue) Response.Write("<br>Estimated Life: " & .EstimatedLife & " Years") End With %> </body> </html>
Object Initialization With . . .
To initialize the member variables of a class when creating an object, after the name of the class, type the With keyword followed by curly brakets. Inside the curly brakets, type the name of a member variable preceded by a period and assign the desired value. Do the same for the other members but separate them with commas. Here is an example:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script Language="VB" runat="server">
Class Depreciation
Public Cost As Double
Public SalvageValue As Double
Public EstimatedLife As Integer
End Class
</script>
<title>Depreciation - Straight-Line Method</title>
</head>
<body>
<h4>Depreciation - Straight-Line Method</h4>
<%
Dim vehicle As New Depreciation With {.Cost = 125670, .SalvageValue = 25000, .EstimatedLife = 10}
Response.Write("Machine Cost: " & vehicle.Cost)
Response.Write("<br>Salvage Value: " & vehicle.SalvageValue)
Response.Write("<br>Estimated Life: " & vehicle.EstimatedLife & " Years")
%>
</body>
</html>
To make your code easy to read, especially if the lisrt of members is long, you can initialize each member on its own line. Here is an example:
<%
Dim vehicle As New Depreciation With
{
.Cost = 38750,
.SalvageValue = 5000,
.EstimatedLife = 5
}
%>
Classes and Functions
A Function that Returns an Object
A function can be made to return an object. When creating such a function, after its parentheses, type the As keyword followed by a space and the name of the class. In the body of the function, do whatever you want. When the function ends, you must return a value that corresponds to the class. Here is an example:
<script runat="server"> Class Student public StudentNumber As String public FirstName As String public LastName As String public Gender As Char End Class Function Register() As Student Dim std As New Student() std.StudentNumber = "77-3946-84" std.FirstName = "Amir" std.LastName = "Shalloub" std.Gender = "M" Return std End Function </script>
To get the value returned by the function, you can assign its call to an object created from the class. You can then use that returned object. Here is an example:
<%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <script runat="server"> Class Student public StudentNumber As String public FirstName As String public LastName As String public Gender As Char End Class Function Register() As Student Dim std As New Student() std.StudentNumber = "77-3946-84" std.FirstName = "Amir" std.LastName = "Shalloub" std.Gender = "M" Return std End Function </script> <title>Student Registration</title> </head> <body> <% Dim pupil As New Student() pupil = Register() Response.Write("<h2>=--= Student Registration =--=</h2>") Response.Write("<table border=2><tr><td>Student #:</td><td>") Response.Write(pupil.StudentNumber) Response.Write("</td></tr><tr><td>First Name:</td><td>") Response.Write(pupil.FirstName) Response.Write("</td></tr><tr><td>Last Name :</td><td>") Response.Write(pupil.LastName) Response.Write("</td></tr><tr><td>Gender:</td><td>") Response.Write(pupil.Gender) Response.Write("</td></tr></table>") %> </body> </html>
This would produce:
In our example, we first declared a variable for the class. This is not necessary since the class is initialized in the function that will produce the object. This means that you can directly assign the function call to the variable. Here is an example:
. . .
<%
Dim pupil = Register()
. . .
%>
. . .
Passing an Object to a Function
A class can be passed to a function. In this case, provide the name of the class for the parameter in the parentheses of the function. As mentioned for the primitive types, you don't have to use the parameter in the body of the function. Otherwise, you can use the parameter in the body of the class. In this case, to access a member of the class, type the name of the parameter followed by a period and the name of the member. When calling the function, you must pass a valid object. Here are examples:
<%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <script Language="VB" runat="server"> Class Cone Public Radius As Double Public Height As Double End Class Public Function CalculateVolume(ByVal iceCream As Cone) As Double Return (iceCream.Radius * iceCream.Radius * iceCream.Height * 3.14159) / 3 End Function </script> <title>Geometry - Cone</title> </head> <body> <h4>Geometry - Cone</h4> <% Dim order As New Cone Dim volume As Double order.Radius = 7.25 order.Height = 4.06 volume = CalculateVolume(order) Response.Write("Radius: " & order.Radius) Response.Write("<br>Height: " & order.Height) Response.Write("<br>Volume: " & volume) %> </body> </html>
This would produce:
Passing Many Objects to a Function
As mentioned for regular types, a function can take more than parameter. One of the parameters can be a class type while the other(s) is(are) (a) regular type(s). In the body of the class, you don't have to use any of the parameters, or you can use one of them or all of them.
You can create a function that takes more than one parameter where all parameters are class types. Once again, in the body of the class, you can ignore one of the parameters. Here is an example:
<script Language="VB" runat="server"> Class Machine Public SerialNumber As String Public Make As String Public Model As String Public Price As String End Class Class Depreciation Public Cost As Double Public SalvageValue As Double Public EstimatedLife As Integer End Class Function Calculate(ByVal item As Machine, ByVal estimate As Depreciation) As Double Dim value As Double = (estimate.Cost - estimate.SalvageValue) / estimate.EstimatedLife Return value End Function </script>
When calling a function that has a class-based parameter, if you don't need to pass an argument for that parameter, pass the argument as Nothing. Here is an example:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script Language="VB" runat="server">
Class Machine
Public SerialNumber As String
Public Make As String
Public Model As String
Public Price As String
End Class
Class Depreciation
Public Cost As Double
Public SalvageValue As Double
Public EstimatedLife As Integer
End Class
Function Calculate(ByVal item As Machine, ByVal estimate As Depreciation) As Double
Dim value As Double = (estimate.Cost - estimate.SalvageValue) / estimate.EstimatedLife
Return value
End Function
</script>
<title>Depreciation: Straight-Line Method</title>
</head>
<body>
<div align="center">
<%
' Dim videoEngine As New
Dim dep As Depreciation = New Depreciation
dep.Cost = 5000
dep.SalvageValue = 500
dep.EstimatedLife = 5
Dim value As Double = Calculate(Nothing, dep)
Response.Write("<h2>Depreciation: Straight-Line Method</h2>")
Response.Write("<table border=2><tr><td>Machine Cost:</td><td>")
Response.Write(dep.Cost)
Response.Write("</td></tr><tr><td>Salvage Value:</td><td>")
Response.Write(dep.SalvageValue)
Response.Write("</td></tr><tr><td>EstimatedLife:</td><td>")
Response.Write(dep.EstimatedLife)
Response.Write(" Years</td></tr><tr><td>Depreciation:</td><td>")
Response.Write(value)
Response.Write("/Year</td></tr></table>")
%>
</div>
</body>
</html>
This would produce:
In the same way, you can create a function that takes many paremeters but you don't have to use those parameters in the body of the function.
Enumerations
Introduction
An enumeration is a series of constant integers that each has a specific position in the list and can be recognized by a meaningful name.
Creating an Enumeration
To create an enumeration, type the Enum keyword followed by a name. On another line, end the enumeration with End Enum. Here is an example:
Enum HouseTypes End Enum
If you are working on a webpage, the enumeration(s) must be created in the script section in the head part.
The section between the Enum enumeration-name and the End Enum lines is the body of the enumeration. In that body, create a list of the members of the enumeration, each on its own line. Here is an example:
<script Language="VB" runat="server"> Enum HouseType Unknown SingleFamily TownHouse Condominium End Enum </script>
A Variable of an Enumeration Type
After creating an enumeration, it becomes a type and can be used like a data type. For example, you can declare a variable of its type. You use the same formula of any variable. Here is an example:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script Language="VB" runat="server">
Enum HouseType
Unknown
SingleFamily
TownHouse
Condominium
End Enum
</script>
<title>Altair Realtors</title>
</head>
<body>
<div align="center">
<%
Dim ht As HouseType
%>
</div>
</body>
</html>
After declaring the variable, you can use it as you see fit. To initialize a variable of an enumeration type, you must specify which member of the enumeration would be assigned to the variable. You can only assign a known member of the enumeration. To do this, on the right side of the assignment operator, type the name of the enumeration, followed by the period operator, and followed by the member whose value you want to assign. Here is an example:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script Language="VB" runat="server">
Enum HouseType
Unknown
SingleFamily
TownHouse
Condominium
End Enum
</script>
<title>Altair Realtors</title>
</head>
<body>
<div align="center">
<%
Dim ht As HouseType
ht = HouseType.TownHouse
%>
</div>
</body>
</html>
The Values of Members of an Enumeration
Each member of an enumeration holds a value of a natural number, such as 0, 1, 2, 3, etc. The number 0 is given to the first member. The number 1 is given to the second member, and so on. If you don't like those values, you can change them. To specify the value of a member, assign to the member using the assignment operator "=". Here is an example:
<script Language="VB" runat="server">
Enum HouseType
Unknown = 5
SingleFamily
TownHouse
Condominium
End Enum
</script>
After doing this, the member that follows it would receive a value + 1. In our example, Unknown now would have a value of 5, SingleFamily would have a value of 6 (from 5 + 1 = 6). Townhouse would have a value of 7, and Condominium would have a value of 8.
You can also assign a value to more than one member of an enumeration. To do this, simply assign the desired value to any member. Here are examples:
<script Language="VB" runat="server"> Enum HouseType Unknown = 5 SingleFamily = 14 TownHouse Condominium = 8 End Enum </script>
In this case, Townhouse would have a value of 15 because it follows SingleFamily that has a value of 14.
Options on Enumerations
Enumerations Visibility
You can control an enumeration's accessibility outside of its project by hiding or making it visible. To do this, you can precede it with the Private, the Friend, or the Public keyword.
Passing an Enumeration as Argument
Once an enumeration has been created, it becomes a type. It can be passed as argument and it can be returned from a function. You pass an enumeration as argument using the same approach of a normal data type. In the same way, you can pass as many enumeration types as parameters. In the body of the procedure, you can use the enumeration as you see fit. You can also create the parameter as optional.
Returning an Enumeration From a Function
To create a function that returns an enumeration, specify its As factor with the name of the enumeration. You can call a function that returns an enumeration type by using just its name. Otherwise, you can use its returned value.