![]() |
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 |
|
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. |
|
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:
|
|
|
||
| Previous | Copyright © 2004-2005 FunctionX, Inc. | Next |
|
|
||