Home

Built-In Classes: The Object Class

 

Introduction

The .NET Framework is a huge library made of various classes and constants you can directly use in your application without necessarily explicitly loading an external library. To start, this main library provides a class called Object.

When you create a class, it automatically inherits its primary characteristics from the parent of all classes: Object.

Equality of Two Class Variables

When you declare and initialize two variables, one of the operations you may want to subsequently perform is to compare their value. To support this operation, the Object class provides its children with a method called Equals. The Equals() method comes in two versions. The first has the following syntax:

Public Overridable Function Equals(obj As Object) As Boolean

This version allows you to call the Equals() method on a declared variable and pass the other variable as argument. Here is an example:

Imports System

Module Exercise

    Public Function Main() As Integer
        ' First book
        Dim NumberOfPages1 As Integer = 422
        ' Second book
        Dim NumberOfPages2 As Integer = 858
        ' Third book
        Dim NumberOfPages3 As Integer = 422

        If NumberOfPages1.Equals(NumberOfPages2) = True Then
            Console.WriteLine("The first and the second books " & _
                              "have the same number of pages")
        Else
            Console.WriteLine("The first and the second books " & _
                              "have different number of pages")
        End If

        If NumberOfPages1.Equals(NumberOfPages3) = True Then
            Console.WriteLine("The first and the third books " & _
                              "have the same number of pages")
        Else
            Console.WriteLine("The first and the third books " & _
                              "have different number of pages")
        End If

        Return 0
    End Function

End Module

This would produce:

The first and the second books have different number of pages
The first and the third books have the same number of pages

The first version of the Object.Equals method is declared as Overridable, which means you can override it if you create your own class. The second version of the Object.Equals() method is:

Public Shared Function Equals(objA As Object, objB As Object) As Boolean

As a Shared method, to use it, you can pass the variables of the two classes whose values you want to compare.

In both cases, if the values of the variables are similar, the Equals() method returns true. If they are different, the method returns false. If you are using the Equals() method to compare the variables of two primitive types, the comparison should be straight forward. If you want to use this methods on variables declared from your own class, you should provide your own implementation of this method.

 
 
 

 

Stringing a Class

In previous lessons, we learned that, to convert the value of a variable declared from a primitive type to a string, you could call the ToString() function. Here is an example:

Imports System

Module Exercise

    Public Function Main() As Integer
        Dim NumberOfPages As Integer = 422

        Console.Write("Number of Pages: ")
        Console.WriteLine(NumberOfPages.ToString())
        Return 0
    End Function

End Module

In many programming languages, programmers usually have to overload an (extractor) operator to display the value(s) of class' variable to the screen. The Object class provides an alternative to this somewhat complicated solution, through the ToString() method. It syntax is:

Public Overridable Function ToString As String

Although the Object class provides this method as non abstract, its implemented version is more useful if you use a primitive type such as Integer, Double and their variances or a string variable. The best way to rely on it consists of overriding it in your own class if you desire to use its role.

Boxing and Un-Boxing

When we study inheritance, we will learn that all data types used in a C# program are "based on" an object called object. As introduced earlier, you can use this data type to declare a variable that would hold any type of value. Because this is some type of a "universal" data type, it can also be initialized with any value. Here are examples:

Imports System

Module Exercise

    Public Function Main() As Integer
        Dim Number As Object
        Dim Thing As Object

        Number = 244
        Thing = "Professor Kabba"

        Console.WriteLine(Number)
        Console.WriteLine(Thing)
        Return 0
    End Function

End Module

This would produce:

244
Professor Kabba

As you can see, when an object variable is initialized, the compiler finds out the type of value that was assigned to it. This is referred to as boxing. This mechanism is transparently done in Visual Basic.

If you declare a variable using a primitive data type (Integer, Single, Double, etc), at one time, you may be interested in converting the value of that variable into an Object. Here is an example:

Imports System

Module Exercise

    Public Function Main() As Integer
        Dim Number As Integer = 244
        Dim Thing As Object = Number

        Number = 244
        Thing = Number

        Console.WriteLine(Number)
        Console.WriteLine(Thing)
        Return 0
    End Function

End Module

This would produce:

244
244

This operation is referred to as unboxing. As you can see, this operation is performed transparently.

Finalizing a Variable

While a constructor, created for each class, is used to instantiate a class. The Object class provides the Finalize() method as a type of destructor.

 
 
   
 

Home Copyright © 2008-2010 FunctionX, Inc.