Home

Class Construction and Destruction

 

Techniques of Initializing a Member Variable

 

Introduction

We saw that when you declare a variable, it receives a default value. In the same way, the member variables of a class are initialized with default values that depend on the type of the variable. For example, a numeric member variable is initialized with 0 while a string-based variable is initialized with an empty string.

After adding a member variable to a class, instead of relying on the default value assigned by the compiler, you can initialized it with a value of your choice, depending on the type of the variable. You have various alternatives.

Initialization of a Member Variable

One way you can initialize a member variable is to assign it the desired value when declaring it. Here is an example:

<script language="vb" type="text/vb" runat="server">

Private Class Square
        Private Side As Double = 48.25

        Function CalculatePerimeter() As Double
            Return Side * 4
        End Function

        Function CalculateArea() As Double
            Return Side * Side
        End Function

End Class

</script>

Instead of initializing a member variable when declaring it, you can create a method that would be used to do this. Here is an example:

<script language="vb" type="text/vb" runat="server">

Public Class Square
    Private side As Double

    Public Sub SetSide()
        side = 48.25
    End Sub
End Class

</script>

Such a method can also be used to initialize more than one value. When you create a method used to initialize one or more member variables of a class, if you want the initialization to apply, you must make sure that you call that method first before calling other methods of the class.

Just as you can create a method to initialize the member(s) of a class, you can overload that method with different versions to perform different initializations. Here are examples:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">

Private Class Square
        Private side As Double

        Public Sub SetSide()
            side = 48.25
        End Sub

        Public Sub SetSide(ByVal sd As Double)
            side = sd
        End Sub

        Public Function GetSide() As Double
            Return side
        End Function

        Function CalculatePerimeter() As Double
            Return Side * 4
        End Function

        Function CalculateArea() As Double
            Return Side * Side
        End Function
End Class

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim sqr As Square = New Square
        
    sqr.SetSide()

    Response.Write("Square Characteristics" & "<br />" & _
                   "Side: " & sqr.GetSide() & "<br />" & _
                   "Perimeter: " & sqr.CalculatePerimeter() & "<br />" & _
                   "Area: " & sqr.CalculateArea())
 
    Response.Write("<br />----------------------------------<br />")

    Dim sd As Double

    sd = 44.28
    sqr.SetSide(sd)

    Response.Write("Square Characteristics" & "<br />" & _
                   "Side: " & sqr.GetSide() & "<br />" & _
                   "Perimeter: " & sqr.CalculatePerimeter() & "<br />" & _
                   "Area: " & sqr.CalculateArea())
%>

</body>
</html>

This would produce:

Initial

 
 
 
 

Class Construction

 

Introduction

When you declare a variable of a class, a special method must be called to initialize the members of that class. This method is automatically provided for every class and it is called a constructor. 

The Default Constructor

Whenever you create a new class, a constructor is automatically provided to it. This  particular constructor is called the default constructor. You have the option of creating it or not. Although a constructor is created for your class, you can customize its behavior or change it as you see fit.

The constructor of a class is called New and it is created as a sub procedure. Here is an example:

<script language="vb" type="text/vb" runat="server">

Public Class Square
        Public Sub New()
            
        End Sub
End Class

</script>

Like every method, a constructor is equipped with a body. In this body, you can access any of the member variables (or method(s)) of the same class. Consider the following program:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">

Public Class Square
        Public Sub New()
            
        End Sub
End Class

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim sq As Square = New Square
%>

</body>
</html>

When a class has been instantiated, its constructor is the first method to be called. For this reason, you can use a constructor to initialize a class; that is, to assign default values to its member variables. Based on this, instead of initializing the member variable(s) of a class when initializing it or them, or instead of creating a special method used to initialize the member variable(s) of a class, you can use a constructor to do this. The advantage of a constructor is that it doesn't need to be called: it is automatically available whenever the class is instantiated.

A Constructor With Argument(s)

In the previous section, we saw that there was always a default constructor for a new class that you create. You just have the option of explicitly creating one or not. The default constructor as we saw it doesn't take arguments. Instead of a default constructor, you may want to create a constructor that takes an argument. Here is an example:

<script language="vb" type="text/vb" runat="server">

Private Class Square
    Public Sub New(ByVal sd As Double)

    End Sub
End Class

</script>

With this type of constructor, when you declare an instance of the class, you can use this new constructor to initialize the class.

If you create one constructor for your class and pass at least one argument to that constructor, the automatic default constructor disappears. This implies that if you declare an instance of the class and use the default constructor to initialize it, you would receive an error when the web page displays. If you still want to use the default constructor in a class after creating a constructor that takes at least one argument, you must explicitly create that default constructor.

Constructor Overloading

A constructor is the primary method of a class. It allows the programmer to initialize a variable of a class when the class is instantiated. A constructor that plays this role of initializing an instance of a class is also called an instance constructor. Most of the time, you don't need to create a constructor, since one is automatically provided to any class you create. Sometimes though, as we have seen in some classes, you need to create your own constructor as you judge it necessary and sometimes, a single constructor may not be sufficient. For example, when creating a class, you may decide, or find out, that there must be more than one way for a user to initialize a variable.

Like any other method, a constructor can be overloaded. In other words, you can create a class and give it more than one constructor. The same rules used on overloading regular methods also apply to constructors: the different constructors must have different number of arguments or different types of arguments.

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc. Home