Inheritance and the .NET Framework |
|
The .NET Framework is the main library used by the Microsoft Visual Basic .NET programming language. When VBasic was developed, inheritance was kept in mind art all levels so that the .NET Framework library provides as much functionality as possible to enhance all types of applications necessary. To provide this functionality, the .NET Framework provides a rich set of classes (and namespaces as we will see). At the highest level, the library provides the Object class that serves as the common ancestor to all classes used in the .NET Framework. In fact, any time you create a class to use in your VBasic application, the class is automatically derived from Object. Consider the following Square class: Imports System Imports System Public Class Square Public Side As Double Function CalculatePerimeter() As Double Return Side * 4 End Function Function CalculateArea() As Double Return Side * Side End Function Shared Sub Main() Dim sqr As Square = New Square Console.Write("Enter Side: ") sqr.Side = Double.Parse(Console.ReadLine()) Console.WriteLine() Console.WriteLine("Square Characteristics") Console.WriteLine("Side: {0:F}", sqr.Side) Console.WriteLine("Perimeter: {0:F}", sqr.CalculatePerimeter()) Console.WriteLine("Area: {0:F}", sqr.CalculateArea()) Console.WriteLine() End Sub End Class Here is an example of running the program: Enter Side: 42.48 Square Characteristics Side: 42.48 Perimeter: 169.92 Area: 1804.55 Although the Square class doesn't indicate that it is inheriting from any class, by virtue of belonging to a VBasic application, it inherits from Object. For this reason, the above code could also have been written as follows: Imports System Public Class Square Inherits Object Public Side As Double Function CalculatePerimeter() As Double Return Side * 4 End Function Function CalculateArea() As Double Return Side * Side End Function Shared Sub Main() Dim sqr As Square = New Square Console.Write("Enter Side: ") sqr.Side = Double.Parse(Console.ReadLine()) Console.WriteLine() Console.WriteLine("Square Characteristics") Console.WriteLine("Side: {0:F}", sqr.Side) Console.WriteLine("Perimeter: {0:F}", sqr.CalculatePerimeter()) Console.WriteLine("Area: {0:F}", sqr.CalculateArea()) Console.WriteLine() End Sub End Class This would produce the same results. Most of the time, if not always, you don't need to derive a class from Object: this inheritance is automatic and it is implied. The obvious question would be, "What does Object offer to its descendant?". By itself, the Object class provides little but useful functionality to its children. In fact it is equipped with only half a dozen methods. Still, this little functionality allows the various child class to interact easily or perform some operations that would require casting. All of the methods of the Object class are public, making them directly available to the descendant classes. Most are "Overridable"s, meaning if you want to use them, you should implement your own version in your class. |
|
String Conversion |
One of the functionalities provided by the Object class consists of converting a class to a string. Because this can mean different things to different classes. The Object class provides the method named ToString. Its syntax is: Public Overridable Function ToString() As String In some cases, you can directly call this method as it is available to your class already. Here is an example: Imports System Public Class Square Public Side As Double Function CalculatePerimeter() As Double Return Side * 4 End Function Function CalculateArea() As Double Return Side * Side End Function Shared Sub Main() Dim sqr As Square = New Square sqr.ToString() End Sub End Class Otherwise, most of the time, you will need to indicate to the compiler how this method should be interpreted by your class, which is done by overriding it. To override this method, follow the rules of overriding a method by associating the Overrides keyword with the syntax of the method. In the body of the method, implement it as you see fit. Here is an example: Imports System Public Class Square Public 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 Shared Sub Main() Dim sqr As Square = New Square Console.Write("Enter Side: ") sqr.Side = Double.Parse(Console.ReadLine()) Console.WriteLine() sqr.ToString() Console.WriteLine() End Sub End Class Because the Object.ToString() method returns a String object, you can assign its result to a string or pass it to a function or method that takes a string as argument. Here is an example: Imports System Public Class Square Public 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 Shared Sub Main() Dim sqr As Square = New Square Console.Write("Enter Side: ") sqr.Side = Double.Parse(Console.ReadLine()) Console.WriteLine("{0}", sqr.ToString()) End Sub End Class
|
Object and Classes Comparisons |
Another valuable method of the Object class is called Equals. This method is used to compare two instances of a class for equality. This method is overloaded with two versions and each returns a Boolean value. One of the versions of the Object.Equals() method has the following syntax: Overloads Public Overridable Function Equals(ByVal obj As Object) As Boolean This method can be called by any class of a .NET Framework application and it takes as argument an instance of the class that the called needs to be compared to. Here is an example: Imports System Class Exercise Public Shared Sub Main() Dim number1 As Integer, number2 As Integer number1 = 248 number2 = 2480 Console.WriteLine("{0} = {1}: {2}", number1, number2, _ number1.Equals(number2)) End Sub End Module This would produce: 248 = 2480: False The second version of the Object.Equals() method has the following syntax: Overloads Public Shared Function Equals(ByVal objA As Object,_ ByVal objB As Object) As Boolean This version is declared as Shared. This means that it is not called by a specific instance of a class. Instead, it takes two arguments that each represents an instance of the classes that need to be compared. Here is an example of calling it: Imports System Class Exercise Public Shared Sub Main() Dim Country As String, Pais As String Country = "Senegal" Pais = "Senegal" Console.WriteLine("{0} = {1}: {2}", Country, Pais, _ Equals(Country, Pais)) End Sub End Class This would produce: Senegal = Senegal: True Although this method is made available to all .NET classes by through inheritance from the Object class, in most cases, to make sure it rightly behaves, you should customize its implementation in most of your classes where you intend to call it. Consider the following program: Imports System Public Class Square Public 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 Shared Sub Main() Dim sqr1 As Square = New Square Dim sqr2 As Square = New Square Console.WriteLine(" =+= First Square =+=") Console.Write("Enter Side: ") sqr1.Side = Double.Parse(Console.ReadLine()) Console.WriteLine(" =+= Second Square =+=") Console.Write("Enter Side: ") sqr2.Side = Double.Parse(Console.ReadLine()) Console.WriteLine() Console.WriteLine("{0}", sqr1.ToString()) Console.WriteLine("{0}", sqr2.ToString()) Console.WriteLine("Squares Equality: {0}", sqr1.Equals(sqr2)) End Sub End Class Here is an example of executing it: =+= First Square =+= Enter Side: 125.84 =+= Second Square =+= Enter Side: 125.84 Square Characteristics Side: 125.84 Perimeter: 503.36 Area: 15835.71 Square Characteristics Side: 125.84 Perimeter: 503.36 Area: 15835.71 Squares Equality: False Notice that, although both square instances have the same Side value and produce the same area, the compiler renders them not equal. This is an indication that the compiler doesn't know how to compare two instances of the Square class. The solution to this type of problem is to override the Equals() method in your class instead of relying on the default implementation from the Object class. Here are two overrides of the Equals() methods as overridden for the above Square class: Imports System Public Class Square Public 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 sqr1 As Square = New Square Dim sqr2 As Square = New Square Console.WriteLine(" =+= First Square =+=") Console.Write("Enter Side: ") sqr1.Side = Double.Parse(Console.ReadLine()) Console.WriteLine(" =+= Second Square =+=") Console.Write("Enter Side: ") sqr2.Side = Double.Parse(Console.ReadLine()) Console.WriteLine() Console.WriteLine("{0}", sqr1.ToString()) Console.WriteLine("{0}", sqr2.ToString()) Console.WriteLine("Squares Equality: {0}", sqr1.Equals(sqr2)) Console.WriteLine() Console.WriteLine(" =+= First Square =+=") Console.Write("Enter Side: ") sqr1.Side = Double.Parse(Console.ReadLine()) Console.WriteLine(" =+= Second Square =+=") Console.Write("Enter Side: ") sqr2.Side = Double.Parse(Console.ReadLine()) Console.WriteLine() Console.WriteLine("{0}", sqr1.ToString()) Console.WriteLine("{0}", sqr2.ToString()) Console.WriteLine("Squares Equality: {0}", Equals(sqr1, sqr2)) End Sub End Class Here is an example of testing the program: =+= First Square =+= Enter Side: 125.84 =+= Second Square =+= Enter Side: 125.84 Square Characteristics Side: 125.84 Perimeter: 503.36 Area: 15835.71 Square Characteristics Side: 125.84 Perimeter: 503.36 Area: 15835.71 Squares Equality: True =+= First Square =+= Enter Side: 38.45 =+= Second Square =+= Enter Side: 16.82 Square Characteristics Side: 38.45 Perimeter: 153.80 Area: 1478.40 Square Characteristics Side: 16.82 Perimeter: 67.28 Area: 282.91 Squares Equality: False Here is another run of the same program: =+= First Square =+= Enter Side: 70.68 =+= Second Square =+= Enter Side: 42.04 Square Characteristics Side: 70.68 Perimeter: 282.72 Area: 4995.66 Square Characteristics Side: 42.04 Perimeter: 168.16 Area: 1767.36 Squares Equality: False =+= First Square =+= Enter Side: 58.26 =+= Second Square =+= Enter Side: 58.26 Square Characteristics Side: 58.26 Perimeter: 233.04 Area: 3394.23 Square Characteristics Side: 58.26 Perimeter: 233.04 Area: 3394.23 Squares Equality: True Notice that, this time, the compiler knows how to perform the comparison of two Square objects using either version of the Equals() method. |
|
||
Previous | Copyright © 2004-2016, FunctionX, Inc. | Next |
|