Class Construction and Destruction |
|
In Lesson 2, we saw that when you declare a variable, the compiler initializes its allocated memory with a default value. Consider the following program: Imports System Public Class Square Private Side As Double Function CalculatePerimeter() As Double Return Side * 4 End Function Function CalculateArea() As Double Return Side * Side End Function Public Overrides Function ToString() As String Console.WriteLine("Square Characteristics") Console.WriteLine("Side: {0:F}", Side) Console.WriteLine("Perimeter: {0:F}", CalculatePerimeter()) Console.WriteLine("Area: {0:F}", CalculateArea()) End Function Public Overridable Overloads Function Equals(ByVal sqr As Square) As Boolean ' We will only compare the side of the square ' because the calculations of the perimeter and the area ' directly depend on the side ' If the side of the square passed as argument is equal ' to the side of this object, both objects are equal If sqr.Side = Me.Side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function Public Overloads Shared Function Equals(ByVal first As Square, _ ByVal second As Square) As Boolean ' We will only compare the side of the square ' If the side of the first square is equal ' to the side of the second one, then both squares are equal If first.Side = second.Side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function Shared Sub Main() Dim sqr As Square = New Square Console.WriteLine("{0}", sqr.ToString()) End Sub End Class This would produce: Square Characteristics Side: 0.00 Perimeter: 0.00 Area: 0.00 Notice that this program indicates that the Side member variable of the Square class was initialized with 0. This means that, like the regular variables, the member variables of a class are initialized by the compiler 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.
One way you can initialize a member variable is to assign it the desired value when declaring it. Here is an example: Imports System Public 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 Public Overrides Function ToString() As String Console.WriteLine("Square Characteristics") Console.WriteLine("Side: {0:F}", Side) Console.WriteLine("Perimeter: {0:F}", CalculatePerimeter()) Console.WriteLine("Area: {0:F}", CalculateArea()) End Function Public Overridable Overloads Function Equals(ByVal sqr As Square) As Boolean ' We will only compare the side of the square ' because the calculations of the perimeter and the area ' directly depend on the side ' If the side of the square passed as argument is equal ' to the side of this object, both objects are equal If sqr.Side = Me.Side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function Public Overloads Shared Function Equals(ByVal first As Square, _ ByVal second As Square) As Boolean ' We will only compare the side of the square ' If the side of the first square is equal ' to the side of the second one, then both squares are equal If first.Side = second.Side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function Shared Sub Main() Dim sqr As Square = New Square Console.WriteLine("{0}", sqr.ToString()) End Sub End Class This would produce: Square Characteristics Side: 48.25 Perimeter: 193.00 Area: 2328.06 Notice that, this time, the default value assigned to the member variable applies. 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: Imports System Public Class Square Private side As Double Public Sub SetSide() side = 48.25 End Sub Shared Sub Main() End Sub End Class 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: Imports System Public 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 Function CalculatePerimeter() As Double Return side * 4 End Function Function CalculateArea() As Double Return side * side End Function Public Overrides Function ToString() As String Console.WriteLine("Square Characteristics") Console.WriteLine("Side: {0:F}", side) Console.WriteLine("Perimeter: {0:F}", CalculatePerimeter()) Console.WriteLine("Area: {0:F}", CalculateArea()) End Function Public Overridable Overloads Function Equals(ByVal sqr As Square) As Boolean ' We will only compare the side of the square ' because the calculations of the perimeter and the area ' directly depend on the side ' If the side of the square passed as argument is equal ' to the side of this object, both objects are equal If sqr.side = Me.side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function Public Overloads Shared Function Equals(ByVal first As Square, _ ByVal second As Square) As Boolean ' We will only compare the side of the square ' If the side of the first square is equal ' to the side of the second one, then both squares are equal If first.side = second.side Then Return True ' If the sides are not equal, then the objects are not equal Return False End Function Shared Sub Main() Dim sqr As Square = New Square sqr.SetSide() Console.WriteLine("{0}", sqr.ToString()) Dim sd As Double Console.Write("Enter Square Side: ") sd = CDbl(Console.ReadLine()) sqr.SetSide(sd) Console.WriteLine("{0}", sqr.ToString()) End Sub End Class Here an example of running the program: Square Characteristics Side: 48.25 Perimeter: 193.00 Area: 2328.06 Enter Square Side: 63.97 Square Characteristics Side: 63.97 Perimeter: 255.88 Area: 4092.16 Notice that, although the Square.Side member variable is private, you cab call the SetSide() public method to initialize it before displaying the characteristics of a square.
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. 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: Public Class Square Public Sub New() End Sub End Class 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: Imports System Module Exercise Public Class Square Public Sub New() Console.WriteLine("Square Builder") End Sub End Class Sub Main() Dim sq As Square = New Square End Sub End Module When executed, it would produce: Square Builder This shows that, 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. |
|
Practical Learning: Constructing a Class |
Imports System Public Class Applicant Public FullName As String Public Sex As String Public DateOfBirth As String Public Sub New() FullName = "Unknown" Sex = "N/A" End Sub End Class Class Exercise Public Shared Sub Main() Dim App As Applicant = New Applicant Console.WriteLine(" -=- Motor Vehicle Administration -=-") Console.WriteLine(" --- Driver's License Information ---") Console.WriteLine("Full Name: {0} ", App.FullName) Console.WriteLine("Sex: {0} ", App.Sex) Console.WriteLine() End Sub End Class |
-=- Motor Vehicle Administration -=- --- Driver's License Information --- Full Name: Unknown Sex: N/A |
A Constructor With Argument |
In the previous section, we saw that there was always a default constructor for a new class that you create; you just havce the option of explicitly creating one or not. The default constructor as we saw it doesn't take arguments: this is not a rule, it is simply assumed. Instead of a default constructor, you may want to create a constructor that takes an argument. Here is an example: Public Class Square Public Sub New(ByVal sd As Double) End Sub End Class With this type of constructor, when you declare an instance of the class, you can use this new constructor to initialize the class. Here is an example: Imports System Module Exercise Public Class Square Public Sub New(ByVal sd As Double) Console.WriteLine("Square Builder") End Sub End Class Sub Main() Dim sq As Square = New Square(38.64) End Sub End Module If you create one constructor for your class and pass at least one argument to that constructor, the automatic default constructor created by the compiler 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 you compile the program. Based on this, the following program will produce an error: Module Module1 Public Class Square Public Sub New(ByVal sd As Double) Console.WriteLine("Square Builder") End Sub End Class Sub Main() Dim sq As Square = New Square ' The default constructor is not available End Sub End Module 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. |
Practical Learning: Constructing a Class |
Imports System Public Class Applicant Public FullName As String Public Sex As String Public DateOfBirth As String Public Sub New(ByVal name As String) FullName = name Sex = "N/A" End Sub End Class Class Exercise Public Shared Sub Main() Console.WriteLine(" -=- Motor Vehicle Administration -=-") Console.WriteLine(" --- Driver's License Information ---") Dim name As String Console.WriteLine("Applicant's Registration") Console.Write("Enter Full Name: ") name = Console.ReadLine() Dim App As Applicant = New Applicant(name) Dim sex As Char Dim gender As String Do Console.Write("Sex(F=Female/M=Male): ") sex = CChar(Console.ReadLine()) If (sex <> "f") And (sex <> "F") And (sex <> "m") And (sex <> "M") Then Console.WriteLine("Please enter a valid character") End If Loop While ((sex <> "f") And (sex <> "F") And (sex <> "m") And (sex <> "M")) If (sex = "f") Or (sex = "F") Then gender = "Female" ElseIf (sex = "m") Or (sex = "M") Then gender = "Male" End If Dim person As Applicant = New Applicant(name) person.Sex = gender Console.WriteLine() Console.WriteLine(" -=- Motor Vehicle Administration -=-") Console.WriteLine(" --- Driver's License Information ---") Console.WriteLine("Full Name: {0} ", person.FullName) Console.WriteLine("Sex: {0} ", person.Sex) Console.WriteLine() End Sub End Class |
-=- Motor Vehicle Administration -=- --- Driver's License Information --- Applicant's Registration Enter Full Name: Paul Bertrand Yamaguchi Sex(F=Female/M=Male): m -=- Motor Vehicle Administration -=- --- Driver's License Information --- Full Name: Paul Bertrand Yamaguchi Sex: Male |
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. |
Practical Learning: Overloading a Constructor |
Imports System Public Class Applicant Public FullName As String Public Sex As String Public DateOfBirth As String ' The default constructor, used to initialize an ' Applicant instance without much information Public Sub New() FullName = "Unknown" Sex = "N/A" End Sub ' A constructor that is passed only one argument Public Sub New(ByVal name As String) FullName = name Sex = "N/A" End Sub ' A constructor with more than one argument ' This type is suitable to completely initialize a variable Public Sub New(ByVal name As String, ByVal gdr As String, ByVal dob As String) FullName = name Sex = gdr DateOfBirth = dob End Sub End Class Class Exercise Public Shared Sub Main() Dim person1 As Applicant = New Applicant person1.FullName = "John Doe" person1.Sex = "Unknown" person1.DateOfBirth = #1/1/1960# Dim name As String Dim sex As Char Dim gender As String Dim dob As Date Console.WriteLine(" -=- Motor Vehicle Administration -=-") Console.WriteLine(" --- Driver's License Information ---") Console.WriteLine("First Person") Console.WriteLine("Full Name: {0} ", person1.FullName) Console.WriteLine("Sex: {0} ", person1.Sex) Console.WriteLine("Date of Date: {0} ", person1.DateOfBirth) Console.WriteLine() Console.WriteLine(" -=- Motor Vehicle Administration -=-") Console.WriteLine(" --- Driver's License Information ---") Console.WriteLine("Applicant's Registration") Console.WriteLine("Second Person") Console.Write("Enter Full Name: ") name = Console.ReadLine() Dim person2 As Applicant = New Applicant(name) person2.DateOfBirth = #1/1/1960# person2.Sex = "Unknown" Console.WriteLine("Third Person") Console.Write("Enter Full Name: ") name = Console.ReadLine() Do Console.Write("Enter Sex(F=Female/M=Male): ") sex = CChar(Console.ReadLine()) If (sex <> "f") And (sex <> "F") And (sex <> "m") And (sex <> "M") Then Console.WriteLine("Please enter a valid character") End If Loop While ((sex <> "f") And (sex <> "F") And (sex <> "m") And (sex <> "M")) If (sex = "f") Or (sex = "F") Then gender = "Female" ElseIf (sex = "m") Or (sex = "M") Then gender = "Male" End If Console.Write("Enter Date of Birth(MM/DD/YYYY): ") dob = CDate(Console.ReadLine()) Dim person3 As Applicant = New Applicant(name, gender, dob) Console.WriteLine() Console.WriteLine(" -=- Motor Vehicle Administration -=-") Console.WriteLine(" --- Driver's License Information ---") Console.WriteLine(" - First Person -") Console.WriteLine("Full Name: {0} ", person1.FullName) Console.WriteLine("Date of Birth: {0} ", person1.DateOfBirth) Console.WriteLine("Sex: {0} ", person1.Sex) Console.WriteLine(" - Second Person -") Console.WriteLine("Full Name: {0} ", person2.FullName) Console.WriteLine("Date of Birth: {0} ", person2.DateOfBirth) Console.WriteLine("Sex: {0} ", person2.Sex) Console.WriteLine(" - Third Person -") Console.WriteLine("Full Name: {0} ", person3.FullName) Console.WriteLine("Date of Birth: {0} ", person3.DateOfBirth) Console.WriteLine("Sex: {0} ", person3.Sex) Console.WriteLine() End Sub End Class |
--- Driver's License Information --- Applicant's Registration Second Person Enter Full Name: Sereine Halou Third Person Enter Full Name: Bernadette Gouang Enter Sex(F=Female/M=Male): F Enter Date of Birth(MM/DD/YYYY): 10/08/1952 -=- Motor Vehicle Administration -=- --- Driver's License Information --- - First Person - Full Name: John Doe Date of Birth: 1/1/1960 Sex: Unknown - Second Person - Full Name: Sereine Halou Date of Birth: 1/1/1960 Sex: Unknown - Third Person - Full Name: Bernadette Gouang Date of Birth: 10/8/1952 Sex: Female |
Class Destruction |
Garbage Collection |
When you initialize a variable using the New operator, you are in fact asking the compiler to provide you some memory space in the heap memory. The compiler is said to "allocate" memory for your variable. When that variable is no longer needed, for example when your program closes, it (the variable) must be removed from memory and the space it was using can be made available to other variables or other programs. This is referred to as garbage collection. The .NET Framework solves the problem of garbage collection by letting the compiler "clean" memory after you. This is done automatically when the compiler judges it necessary so that the programmer doesn't need to worry about this issue. |
Class Finalization |
When you declare a variable based on a class, the compiler allocates memory for it. This portion of memory would be available and used while the program is running and as long as the compiler judges this necessary. While the program is running, the instance of the class uses or consumes the computer's resources as necessary. When the object is not needed anymore, for example when the program terminates, the object must be destroyed, the memory it was using must be emptied to be made available to other programs on the computer, and the resources that the object was using should (must) be freed to be restored to the computer so they can be used by other programs. To make this possible, the Object class is equipped with a method called Finalize that is protected and therefore made available to all descendant classes of the .NET Framework. The syntax of the Object.Finalize() method is: Overrides Protected Sub Finalize() The Finalize() method is automatically called when an instance of a class is not needed anymore. In all of the classes we have used so far, this method was transparently called when the compiler judged that the instance of the class was not used anymore. If you don't want this method to be called, call the Public Shared Sub SuppressFinalize(ByVal obj As Object) In most cases, you can let the compiler call the Finalize() method when the time comes. |
Introduction |
A structure is an enhanced version of the primitive data types we have used in previous lessons. Like a class, a structure is created from one primitive type or by combining various primitive types, resulting in an advanced data type that is not inherently built in the VBasic language. To create a structure, you use the same formula as a class but with the Structure keyword. Here is an example of a structure: Public Structure Box End Structure Like a class, a structure can have member variables and they are listed in the body of the structure between the Structure and the End Structure lines. Here is an example: Public Structure Box Dim Length As Double End Structure Like a class, a structure can have methods as its members. The structures are created and implemented using the same techniques. Here is an example: Public Structure Box Dim Length As Double Dim Width As Double Dim height As Double Function Volume() As Double Return Length * Width * height End Function End Structure Like a class, a structure can have a constructor and even various versions of its constructor. |
Structure Declaration |
Like any other data type, to use a structure, you can first declare a variable from it and you must allocate its memory using the New operator. After declaring the variable, to access the members of the structure, you can use the period operator. Here is an example: Imports System Module Exercise Public Structure Box Dim Length As Double Dim Width As Double Dim Height As Double Function Volume() As Double Return Length * Width * Height End Function End Structure Sub Main() Dim ShoeBox As Box = New Box ShoeBox.Length = 22.84 ShoeBox.Width = 18.05 ShoeBox.Height = 12.94 Console.WriteLine("Box Characteristics") Console.WriteLine("Length: {0} ", ShoeBox.Length) Console.WriteLine("Width: {0} ", ShoeBox.Width) Console.WriteLine("Height: {0} ", ShoeBox.Height) End Sub End Class This would produce: Box Characteristics Length: 22.84 Width: 18.05 Height: 12.94 Although there are many similarities in the behaviors of classes and structures, you should use a structure when the object you are creating is meant to represent relatively small values. |
Characteristics of Members of a Class |
Constant Member Variables |
In Lesson 3, we saw that you could create a constant variable in your program. In the same way, you can make a member variable of class to be constant. To do this, follow the same formula we used previously to declare a constant. Here is an example: |
Imports System Module Exercise Public Class Circle Public Radius As Double Public Const Twice As Integer = 2 Public Sub New() End Sub End Class Sub Main() Dim circ As Circle Dim Diameter As Double circ = New Circle circ.Radius = 32.86 Diameter = circ.Radius * circ.Twice Console.WriteLine("Circle Characteristics") Console.WriteLine("Radius: {0} ", circ.Radius) Console.WriteLine("Diameter: {0} ", Diameter) End Sub End Module
This would produce:
Circle Characteristics Radius: 32.86 Diameter: 65.72
Read-Only Member Variables |
In the same way, in Lesson 3, we saw that you could declare a variable as ReadOnly if you wanted its value to be constant. This can also be applied to a member of a class. To do this, follow the same formula we saw for those variables, except that the variable should be made a member of the class. Unlike a constant variable that you must initialize when creating it, you can declare a ReadOnly variable in the class without initializing it. This would be done as follows: Public ReadOnly PI As Double After declaring the variable, you should initialize it. You can do this when declaring it, as done for a constant. Here is an example: Public Class Circle Public Radius As Double Public Const Twice As Integer = 2 Public ReadOnly PI As Double = 3.14159 End Class Alternatively, you can initialize the variable in the(a) constructor of its class. This would be done as follows: Imports System Module Exercise Public Class Circle Public Radius As Double Public Const Twice As Integer = 2 Public ReadOnly PI As Double Public Sub New() PI = 3.14159 End Sub End Class Sub Main() Dim circ As Circle Dim Diameter As Double Dim Circumference As Double circ = New Circle circ.Radius = 32.86 Diameter = circ.Radius * circ.Twice Circumference = Diameter * circ.PI Console.WriteLine("Circle Characteristics") Console.WriteLine("Radius: {0} ", circ.Radius) Console.WriteLine("Diameter: {0} ", Diameter) Console.WriteLine("Circumference: {0} ", Circumference) End Sub End Module This would produce: Circle Characteristics Radius: 32.86 Diameter: 65.72 Circumference: 206.4652948 If the value held by a read-only member variable is gotten from an expression, then the value should be initialized in the(a) construction with the desired expression. If you don't rightly initialize it, the compiler would initialize it with the default value based on the type of that variable. Therefore, you should make sure you initialize your ReadOnly member variables in a constructor, if those variables are based on an expression. Here are a few examples: Imports System Module Exercise Public Class Circle Public Radius As Double Public Const Twice As Integer = 2 Public ReadOnly PI As Double Public ReadOnly Diameter As Double Public ReadOnly Circumference As Double Public ReadOnly Area As Double Public Sub New() PI = 3.14159 Radius = 24.55 Diameter = Radius * Twice Circumference = Diameter * PI Area = Radius * Radius * PI End Sub End Class Sub Main() Dim circ As Circle = New Circle circ.Radius = 32.86 Console.WriteLine("Circle Characteristics") Console.WriteLine("Radius: {0} ", circ.Radius) Console.WriteLine("Diameter: {0} ", circ.Diameter) Console.WriteLine("Circumference: {0} ", circ.Circumference) Console.WriteLine("Area: {0} ", circ.Area) End Sub End Module This would produce: Circle Characteristics Radius: 32.86 Diameter: 49.1 Circumference: 154.252069 Area: 1893.444146975 In the previous section, we saw that a constant variable must be initialized when it is created. Although a read-only variable seems to follow the same rule, it doesn't. Remember that you don't need to initialize a read-only variable when you declare it since you can do this in the(a) constructor of the class. Also, because a constructor can be overloaded, a read-only member variable can hold different values depending on the particular constructor that is accessed at a particular time but the value of a constant variable cannot change: it is initialized once, in the class (or in a method) and it keeps that value throughout the class (or method). |
|
||
Previous | Copyright © 2004-2016, FunctionX, Inc. | Next |
|