Home

A Class Instance

 

With a Class Instance

After declaring a variable of a class, you can initialize it and access any of its members as you see fit. To initialize an object of a class, we saw that you can access any or each of its public member variables and assign it the appropriate value. Here is an example:

Imports System

Module Exercise

    Enum EmploymentStatus
        esPartTime
        esFullTime
        esContractor
    End Enum

    Class Employee
        Public FirstName As String
        Public LastName As String
        Public Address As String
        Public City As String
        Public State As String
        Public ZIPCode As Long
        Public Gender As Char
        Public EmplStatus As EmploymentStatus
    End Class

    Sub Main()
        Dim Empl As New Employee

        Empl.FirstName = "Emilie"
        Empl.LastName = "Gudson"

        Console.WriteLine("Employee Information")
        Console.WriteLine("Full Name: {0}, {1}", Empl.LastName, Empl.FirstName)

        Console.WriteLine()
    End Sub

End Module

This would produce:

Employee Information
Full Name: Gudson, Emilie

If a class has many members and you want to change the values of many or all of them, you can use the With keyword to type the name of the class variable once. After doing this, use the period operator to access each desired member. Here is an example:

Imports System

Module Exercise

    Enum EmploymentStatus
        esPartTime
        esFullTime
        esContractor
    End Enum

    Class Employee
        Public FirstName As String
        Public LastName As String
        Public Address As String
        Public City As String
        Public State As String
        Public ZIPCode As Long
        Public Gender As Char
        Public EmplStatus As EmploymentStatus
    End Class

    Sub Main()
        Dim Empl As New Employee

        With Empl
            .FirstName = "Emilie"
            .LastName = "Gudson"
            .Address = "824 Lauseanne Ave"
            .City = "Takoma Park"
            .State = "Maryland"
            .ZIPCode = 20910
            .Gender = "F"
            .EmplStatus = EmploymentStatus.esFullTime

            Console.WriteLine("Employee Information")
            Console.WriteLine("Full Name:   {0}, {1}", .LastName, .FirstName)
            Console.WriteLine("Address:     {0}", .Address)
            Console.WriteLine("             {0}, {1}, {2}", .City, .State, .ZIPCode)
            Console.Write("Gender:      ")

            If .Gender = "M" Then
                Console.WriteLine("Gender")
            ElseIf .Gender = "F" Then
                Console.WriteLine("Female")
            Else
                Console.WriteLine("Unknown")
            End If

            Console.Write("Empl Status: ")
            If .EmplStatus = EmploymentStatus.esContractor Then
                Console.WriteLine("Contractor")
            ElseIf .EmplStatus = EmploymentStatus.esFullTime Then
                Console.WriteLine("Full Time")
            Else
                Console.WriteLine("Part Time")
            End If
        End With

        Console.WriteLine()
    End Sub

End Module

This would produce:

Employee Information
Full Name:   Gudson, Emilie
Address:     824 Lauseanne Ave
             Takoma Park, Maryland, 20910
Gender:      Female
Empl Status: Full Time
 

Shared Member Variables

When you declare a variable of a class, you are said to have created an instance of the class. This makes an object available so you can initialize and use its member variables and methods as you see fit.

Just as you can declare one instance of a class, in the same way, you can declare as many instances as you judge it necessary. After declaring various variables of the same class and initializing them, each variable has and keeps its own values. Here is an example:

Imports System

Module Exercise

    Class TRectangle
        Public Length As Double
        Public Height As Double

        Function Perimeter#()
            Return (Length + Height) * 2
        End Function

        Function Area#()
            Return Length * Height
        End Function
    End Class

    Sub Main()
        Dim RegistrationCard As New TRectangle
        Dim Invoice As New TRectangle

        RegistrationCard.Length = 24.55 : RegistrationCard.Height = 20.68
        Invoice.Length = 8.5 : Invoice.Height = 11.65

        Console.WriteLine("Paper Registration Characteristics")
        Console.WriteLine("Length:     {0}", RegistrationCard.Length)
        Console.WriteLine("Height:      {0}", RegistrationCard.Height)
        Console.WriteLine("Perimeter: {0}", RegistrationCard.Perimeter)
        Console.WriteLine("Area:         {0}", RegistrationCard.Area)

        Console.WriteLine("Paper Invoice Characteristics")
        Console.WriteLine("Length:    {0}", Invoice.Length)
        Console.WriteLine("Height:     {0}", Invoice.Height)
        Console.WriteLine("Perimeter: {0}", Invoice.Perimeter)
        Console.WriteLine("Area:         {0}", Invoice.Area)
    End Sub

End Module

This would produce:

Paper Registration Characteristics
Length:    24.55
Height:    20.68
Perimeter: 90.46
Area:      507.694

Paper Invoice Characteristics
Length:    8.5
Height:    11.65
Perimeter: 40.3
Area:      99.025

In order to access the member variables of the above TRectangle class, you must declare a variable of the class. VBasic provides an alternative. You can declare a member variable so that you can directly access it from anywhere in your code without primarily declaring a variable of the class. In order to have such a member variable, you must explicitly create it as shared.

To create a shared member variable, type the Shared keyword on its left when declaring it. After the shared member variable has been declared, you can use it like any other member variable of that class, except that you don't need to declare an instance of that class when you want to access that member variable. Here is an example:

Imports System

Module Exercise

    Class TRectangle
        Public Length As Double
        Public Shared Height As Double

        Function Perimeter#()
            Return (Length + Height) * 2
        End Function

        Function Area#()
            Return Length * Height
        End Function
    End Class
    Sub Main()
        REM Notice that a TRectangle variable is not declared
        TRectangle.Height = 20.68

        Console.WriteLine("Rectangle Characteristics")
        Console.WriteLine("Height:    {0}", TRectangle.Height)
    End Sub

End Module

This would produce:

Rectangle Characteristics
Height:    20.68

Based on this, when creating a class, you will decide whether you want a particular member variable to be shared or not. You can have only one, two, more, or all member variables shared in a class. Experience and your own goal will guide you.

 

Shared Methods

Consider the following class:

Imports System

Module Exercise

    Class TRectangle
        Public Length As Double
        Public Shared Height As Double

        Function Perimeter#()
            Return (Length + Height) * 2
        End Function

        Function Area#()
            Return Length * Height
        End Function
    End Class

    Sub Main()
        REM Notice that a TRectangle variable is not declared
        TRectangle.Height = 20.68
        TRectangle.Length = 32.47

        Console.WriteLine("Rectangle Characteristics")
        Console.WriteLine("Length:    {0}", TRectangle.Length)
        Console.WriteLine("Height:    {0}", TRectangle.Height)
    End Sub

End Module

This would produce:

Rectangle Characteristics
Length: 32.47
Height: 20.68

Like member variables, a method can be shared among classes. In some cases, shared methods are more used than shared member variables because a shared method allows performing an action on a class without declaring an instance of that class.

To create a shared method, type the Shared keyword on the left of the method's name. Like a shared member variable, once a method has been created as shared, it can be accessed directly from anywhere. Remember that you would need to type the name of the class before accessing the method. The name of the class allows you to "qualify" the method. Here is an example:

Imports System

Module Exercise

    Class TRectangle
        Public Shared Length As Double
        Public Shared Height As Double

        Shared Function Perimeter#()
            Return (Length + Height) * 2
        End Function

        Shared Function Area#()
            Return Length * Height
        End Function
    End Class

    Sub Main()
        REM Notice that a TRectangle variable is not declared
        TRectangle.Height = 20.68
        TRectangle.Length = 32.47

        Console.WriteLine("Rectangle Characteristics")
        Console.WriteLine("Length:    {0}", TRectangle.Length)
        Console.WriteLine("Height:    {0}", TRectangle.Height)
        Console.WriteLine("Perimeter: {0}", TRectangle.Perimeter)
        Console.WriteLine("Area:      {0}", TRectangle.Area)
    End Sub

End Module

 

 

Sharing the Main Method

So far, we were creating the Main() procedure as a Sub. This was a requirement for it when belonging to a module. If you want to create your application from a class, you can include the Main() procedure in such a class. The rule you must observe is that Main() must be created as a shared procedure. Based on this, you can create Main() as follows:

Imports System

Class Exercise

   Shared Sub Main()

      Console.WriteLine("Microsoft Visual Basic .NET Programming Language")

   End Sub

End Class

For the rest of our lessons, we will use either the module or the class version.

 

Me

We have mentioned two techniques of accessing the members of a class, one consisted of declaring a variable of the class, the other had to do with Shared members. None of these techniques is important if you want to access a member variable or method of a class from another method of the same class. We know already that the members of a class are made available to all other members of the same class without being declared or qualified. Consider the following class:

Public Class Triangle
        Public Base As Double
        Public Height As Double
        Public Area As Double

        Public Sub Show()
            Dim Area As Double
            Area = Base * Height / 2
        End Sub
End Class

When the Area variable is used in the Show() method, there are two variables available and named Area. It makes it confusing to know what particular variable is being accessed. You can use a special member of a class that allows you to specify the member of a class when accessing it. This member is called Me.

When using Me, you can access any member of a class within any method of the same class. Here is an example:

Imports System

Class Exercise

    Public Class Triangle
        Public Base As Double
        Public Height As Double

        Public Sub Show()
            Dim Area As Double

            ' Using "this" to access the members of this class
            Me.Base = 24.55
            Me.Height = 20.75

            ' You cannot use this to access Area because Area 
            ' is not a member of this class
            Area = Me.Base * Me.Height / 2

            Console.WriteLine("Triangle Characteristics")
            Console.WriteLine("Base:   {0}", Me.Base)
            Console.WriteLine("Height: {0}", Me.Height)
            Console.WriteLine("Area:   {0}", Area) ' Area is not a member of the Exercise class
        End Sub
    End Class

    Shared Sub Main()
        Dim tri As Triangle = New Triangle
        tri.Show()
    End Sub

End Class

This would produce:

Triangle Characteristics
Base:   24.55
Height: 20.75
Area:    254.70625

There are exceptions when using Me:

  • Me can never be declared: it is automatically implied when you create a class
  • Me cannot be used in a class A to access a member of class B 
  • Me cannot be used in a Shared method. The following program will not compile because Me is used in the Show() method declared as a Shared method:
     
    Imports System
    
    Class Exercise
    
        Public Class Triangle
            Public Base As Double
            Public Height As Double
    
            Public Shared Sub Show()
                Dim Area As Double
    
                ' Using "this" to access the members of this class
                Me.Base = 24.55
                Me.Height = 20.75
    
                ' You cannot use this to access Area because Area 
                ' is not a member of this class
                Area = Me.Base * Me.Height / 2
    
                Console.WriteLine("Triangle Characteristics")
                Console.WriteLine("Base:   {0}", Me.Base)
                Console.WriteLine("Height: {0}", Me.Height)
                Console.WriteLine("Area:    {0}", Area)
            End Sub
        End Class
    
        Sub Main()
            
        End Sub
    
    End Class
 
 
 

Previous Copyright © 2004-2012, FunctionX Next