Structures, Classes, and their Methods
Structures, Classes, and their Methods
Structures
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 Visual Basic 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. You do this as if the variable were of a primitive type. That is, you do not have to allocated memory for it 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:
Public 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
Public Function Main() As Integer
Dim ShoeBox As Box
ShoeBox.Length = 22.84
ShoeBox.Width = 18.05
ShoeBox.height = 12.94
MsgBox("Box Characteristics" & vbCrLf & _
"Length:" & vbTab & FormatNumber(ShoeBox.Length) & vbCrLf & _
"Width:" & vbTab & FormatNumber(ShoeBox.Width) & vbCrLf & _
"Height:" & vbTab & FormatNumber(ShoeBox.height))
Return 0
End Function
End Module
This would produce:

By default, the memory allocated for a variable of a structure type is in the stack. If you want to dynamically memory for the variable, you can initialize it using the New operator. This would be done as follows:
Public Module Exercise
Public Structure Box
End Structure
Public Function Main() As Integer
Dim ShoeBox As Box
ShoeBox = New Box
Return 0
End Function
End Module
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.
|
|
REM Each item of our store has a number, a name, and a price
REM Therefore, we will use a structure
REM to hold these pieces of information
Public Structure ItemIdentification
Public ItemNumber As Long
Public ItemName As String
Public UnitPrice As Double
End Structure
Public Class StoreItem
Public ItemID As ItemIdentification
Friend ItemSize As String
REM Default Constructor, used to initialize an unidentified object
Public Sub New()
Me.ItemID.ItemNumber = 0
Me.ItemID.ItemName = "Unknown"
Me.ItemID.UnitPrice = 0D
Me.ItemSize = "Unknown or Fits All"
End Sub
REM This constructor takes a structure as argument
Public Sub New(ByVal SampleItem As ItemIdentification)
Me.ItemID = SampleItem
Me.ItemSize = "Unknown or Fits All"
End Sub
REM This constructor takes a structure and a primitive type as arguments
Public Sub New(ByVal SampleItem As ItemIdentification, _
ByVal Size As String)
Me.ItemID = SampleItem
Me.ItemSize = Size
End Sub
REM This method is used to display a description of an item
REM The caller will determine whether the item has a size or not
Public Sub ShowItem(ByVal HasSize As Boolean)
If HasSize = True Then
MsgBox("=-= Department Store =-=" & vbCrLf & _
"--- Store Inventory ---" & vbCrLf & _
"Item #:" & vbTab & vbTab & ItemID.ItemNumber & vbCrLf & _
"Item Name:" & vbTab & ItemID.ItemName & vbCrLf & _
"Size:" & vbTab & vbTab & ItemSize & vbCrLf & _
"Unit Price:" & vbTab & FormatCurrency(ItemID.UnitPrice), _
MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, _
"Department Store")
Else
MsgBox("=-= Department Store =-=" & vbCrLf & _
"--- Store Inventory ---" & vbCrLf & _
"Item #:" & vbTab & vbTab & ItemID.ItemNumber & vbCrLf & _
"Item Name:" & vbTab & ItemID.ItemName & vbCrLf & _
"Unit Price:" & vbTab & FormatCurrency(ItemID.UnitPrice), _
MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, _
"Department Store")
End If
End Sub
End Class
|
Module DepartmentStore
Public Function Main() As Integer
Dim Size As String
Dim SaleItem As StoreItem
Dim AnItem As ItemIdentification
REM Using the default constructor
SaleItem = New StoreItem
SaleItem.ShowItem(True)
AnItem.ItemNumber = 608432
AnItem.ItemName = "Silver 1/10-ct. T.W. Diamond Heart-Link Bracelet"
AnItem.UnitPrice = 95.85
REM Using the constructor that takes 4 arguments
SaleItem = New StoreItem(AnItem)
SaleItem.ShowItem(False)
AnItem.ItemNumber = 437876
AnItem.ItemName = "Crinkled Georgette Dress"
AnItem.UnitPrice = 42.95
Size = "Medium"
SaleItem.ShowItem(True)
REM Using the constructor that takes 5 arguments
REM after initialing a Women object
SaleItem = New StoreItem(AnItem, Size)
SaleItem.ShowItem(True)
AnItem.ItemNumber = 790475
AnItem.ItemName = "New Wool Comfort Pants"
AnItem.UnitPrice = 38.75
SaleItem = New StoreItem(AnItem, Size:="36x33")
SaleItem.ShowItem(True)
Return 0
End Function
End Module
|
|
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:
Public Module Exercise
Public Class Circle
Public Radius As Double
Public Const Twice As Integer = 2
Public Sub New()
End Sub
End Class
Public Function Main() As Integer
Dim circ As Circle
Dim Diameter As Double
circ = New Circle
circ.Radius = 32.86
Diameter = circ.Radius * Circle.Twice
MsgBox("Circle Characteristics" & vbCrLf & _
"Radius:" & vbTab & circ.Radius & vbCrLf & _
"Diameter:" & vbTab & Diameter)
Return 0
End Function
End Module
This would produce:

|
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:
Public 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
Public Function Main() As Integer
Dim circ As Circle
Dim Diameter As Double
Dim Circumference As Double
circ = New Circle
circ.Radius = 32.86
Diameter = circ.Radius * Circle.Twice
Circumference = Diameter * circ.PI
MsgBox("Circle Characteristics" & vbCrLf & _
"Radius:" & vbTab & vbTab & circ.Radius & vbCrLf & _
"Diameter:" & vbTab & vbTab & Diameter & vbCrLf & _
"Circumference:" & vbTab & Circumference)
Return 0
End Function
End Module
This would produce:

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:
Public 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
Public Function Main() As Integer
Dim Circ As Circle = New Circle
Circ.Radius = 32.86
MsgBox("Circle Characteristics" & vbCrLf & _
"Radius:" & vbTab & vbTab & Circ.Radius & vbCrLf & _
"Diameter:" & vbTab & vbTab & Circ.Diameter & vbCrLf & _
"Circumference:" & vbTab & Circ.Circumference & vbCrLf & _
"Area:" & vbTab & vbTab & Circ.Area)
Return 0
End Function
End Module
This would produce:

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).
|
Classes Combinations |
|
Class Nesting |
A class can be created inside of another class. A class created inside of another is referred to as nested. To nest a class, simply create it as you would any other. Here is an example of a class called Inside that is nested in a class called Outside:
Public Class Outside
Public Class Inside
End Class
End Class
In the same way, you can nest as many classes as you wish in another class and you can nest as many classes inside of other nested classes if you judge it necessary. Just as you would manage any other class so can you exercise control on a nested class. For example, you can declare all necessary fields, properties, or methods in the nested class or in the nesting class. When you create one class inside of another, there is no special programmatic relationship between both classes: just because a class is nested does not mean that the nested class has immediate access to the members of the nesting class. They are two different classes and they can be used separately as you judge it necessary.
The name of a nested class is not "visible" outside of the nesting class. To access a nested class outside of the nesting class, you must qualify the name of the nested class anywhere you want to use it. For example, if you want to declare an Inside variable somewhere in the program but outside of Outside, you must qualify its name. Here is an example:
Public Module Exercise
Public Class Outside
Public Class Inside
Public Sub New()
MsgBox(" =- Inside -=")
End Sub
End Class
Public Sub New()
MsgBox(" -= Outside =-")
End Sub
End Class
Public Function Main() As Integer
Dim Out As Outside = New Outside
Dim Ins As Outside.Inside = New Outside.Inside
Return 0
End Function
End Module
This would produce:


Because there is no programmatically privileged relationship between a nested class and its "container" class, if you want to access the nested class in the nesting class, you can use its static members. In other words, if you want, you can declare static all members of the nested class that you want to access in the nesting class. Here is an example:
Public Module Exercise
Public Class Outside
Public Class Inside
Public Shared InMessage As String
Public Sub New()
MsgBox("=- Insider -=")
InMessage = "Sitting inside while it's raining"
End Sub
Public Shared Sub Show()
msgbox("Show me the wonderful world of C# Programming")
End Sub
End Class
Public Sub New()
MsgBox("-= Outside =-")
End Sub
Public Sub Display()
msgbox(Inside.InMessage)
Inside.Show()
End Sub
End Class
Public Function Main() As Integer
Dim Recto As Outside = New Outside
Dim Ins As Outside.Inside = New Outside.Inside
Recto.Display()
Return 0
End Function
End Module
In the same way, if you want to access the nesting class in the nested class, you can go through the static members of the nesting class. To do this, you can declare static all members of the nesting class that you want to access in the nested class. Here is an example:
Public Module Exercise
Public Class Outside
Public Class Inside
Public Shared InMessage As String
Public Sub New()
MsgBox("=- Insider -=")
InMessage = "Sitting inside while it's raining"
End Sub
Public Shared Sub Show()
msgbox("Show me the wonderful world of C# Programming")
End Sub
Public Sub FieldFromOutside()
msgbox(Outside.OutMessage)
End Sub
End Class
Private Shared OutMessage As String
Public Sub New()
MsgBox(" -= The Parent =-")
OutMessage = "Standing outside! It's cold and raining!!"
End Sub
Public Sub Display()
MsgBox(Inside.InMessage)
Inside.Show()
End Sub
End Class
Public Function Main() As Integer
Dim Recto As Outside = New Outside
Dim Ins As Outside.Inside = New Outside.Inside
Recto.Display()
Ins.FieldFromOutside()
Return 0
End Function
End Module
This would produce:
![]() |
![]() |
![]() |
|
![]() |
![]() |
||
Instead of static members, if you want to access members of a nested class in the nesting class, you can first declare a variable of the nested class in the nesting class. In the same way, if you want to access members of a nesting class in the nested class, you can first declare a variable of the nesting class in the nested class. Here is an example:
Public Module Exercise
Public Class Outside
REM A member of the nesting class
Private OutMessage As String
REM The nested class
Public Class Inside
REM A field in the nested class
Public InMessage As String
REM A constructor of the nested class
Public Sub New()
MsgBox("=- Insider -=")
Me.InMessage = "Sitting inside while it's raining"
End Sub
REM A method of the nested class
Public Sub Show()
REM Declare a variable to access the nesting class
Dim Outsider As Outside = New Outside
MsgBox(outsider.OutMessage)
End Sub
End Class REM End of the nested class
REM A constructor of the nesting class
Public Sub New()
Me.OutMessage = "Standing outside! It's cold and raining!!"
MsgBox("-= The Parent =-")
End Sub
REM A method of the nesting class
Public Sub Display()
MsgBox(insider.InMessage)
End Sub
REM Declare a variable to access the nested class
Dim insider As Inside = New Inside
End Class
Public Function Main() As Integer
Dim Recto As Outside = New Outside
Dim Ins As Outside.Inside = New Outside.Inside
Ins.Show()
Recto.Display()
Return 0
End Function
End Module
This would produce:
![]() |
![]() |
![]() |
|
![]() |
![]() |
||
![]() |
![]() |
||
|
A Class as a Field |
Just like any of the variables we have used so far, you can make a class or a structure a member variable of another class. To use a class in your own class, of course you must have that class. You can use one of the classes already available in C# or you can first create your own class. Here is an example of a class:
Public Class Point
Friend x As Short
Friend y As Short
End Class
A field is a member variable created from another class instead of a primitive type. To use one class as a member variable of another class, simply declare its variable as you would proceed with any of the member variables we have declared so far. Here is an example:
Public Class Point
Friend x As Short
Friend y As Short
End Class
Public Class CoordinateSystem
Private Start As Point
End Class
After a class has been declared as a member variable of another class, it can be used regularly. Because the member is a class, declared as a reference, there are some rules you must follow to use it. After declaring the member variable, you must make sure you have allocated memory for it. You must also make sure that the variable is initialized appropriately before it can be used; otherwise you would receive an error when compiling the program.
Public Class StoreItem
Private nbr As Long
Private cat As Char
Private mk As String
Private mdl As String
Private price As Double
Public Function GetItemNumber() As Long
Return nbr
End Function
Public Sub SetItemNumber(ByVal number As Long)
Me.nbr = number
End Sub
Public Function GetCategory() As String
Select Case cat
Case "a", "A"
Return "Audio Cables"
Case "b", "B"
Return "Instructional and Tutorials (Books)"
Case "c", "C"
Return "Cell Phones and Accessories"
Case "d", "D"
Return "Bags and Cases"
Case "e", "E"
Return "Headphones"
Case "f", "F"
Return "Instructional and Tutorials (VHS & DVD)"
Case "g", "G"
Return "Digital Cameras"
Case "h", "H"
Return "Cables and Connectors"
Case "i", "I"
Return "PDAs and Accessories"
Case "j", "J"
Return "Telephones and Accessories"
Case "k", "K"
Return "Surge Protector"
Case "l", "L"
Return "TVs and Videos"
Case Else
Return "Unknown"
End Select
End Function
Public Sub SetCategory(ByVal category As Char)
Me.cat = category
End Sub
Public Function GetMake() As String
Return mk
End Function
Public Sub SetMake(ByVal make As String)
Me.mk = make
End Sub
Public Function GetModel() As String
Return mdl
End Function
Public Sub SetModel(ByVal model As String)
Me.mdl = model
End Sub
Public Function GetUnitPrice() As Double
Return price
End Function
Public Sub SetUnitPrice(ByVal unitPrice As Double)
Me.price = unitPrice
End Sub
End Class
|
|
Returning a Class or Passing a Class |
|
Returning a Class From a Method |
Like a value from a regular type, you can return a class value from a method of a class. To do this, you can first declare the method and specify the class as the return type. Here is an example:
Public Class Point
Friend x As Short
Friend y As Short
End Class
Public Class CoordinateSystem
Private PtStart As Point
Private PtEnd As Point
Public Function GetThePoint() As Point
End Function
End Class
After implementing the method, you must return a value that is conform to the class, otherwise you would receive an error when compiling the application. You can proceed by declaring a variable of the class in the body of the method, initializing the variable, and then returning it. Here is an example:
Public Class Point
Friend x As Short
Friend y As Short
End Class
Public Class CoordinateSystem
Private PtStart As Point
Private PtEnd As Point
Public Function GetThePoint() As Point
Dim Pt As Point = New Point
Pt.x = CShort(InputBox("Enter the x coordinate of the point: "))
Pt.y = CShort(InputBox("Enter the y coordinate of the point: "))
Return Pt
End Function
End Class
Once a method has returned a value of a class, the value can be used as normally as possible. Here is an example:
Public Module Exercise
Public Class Point
Friend x As Short
Friend y As Short
End Class
Public Class CoordinateSystem
Private PtStart As Point
Private PtEnd As Point
Public Function GetThePoint() As Point
Dim Pt As Point = New Point
Pt.x = CShort(InputBox("Enter the x coordinate of the point: "))
Pt.y = CShort(InputBox("Enter the y coordinate of the point: "))
Return Pt
End Function
End Class
Public Function Main() As Integer
Dim Coordinate As Point
Dim Coordinator As CoordinateSystem
Coordinator = New CoordinateSystem
Coordinate = Coordinator.GetThePoint()
Return 0
End Function
End Module
|
Passing a Class as Argument |
Once a class has been created, it can be used like any other variable. For example, its variable can be passed as argument to a procedure or to a method of another class. When a class is passed as argument:
As done for the arguments of primitive types, in the body of the procedure gets the argument, access its public or friendly members and use them as you see fit. Here is an example:
Public Module Exercise
Public Class Point
Friend x As Short
Friend y As Short
End Class
Public Class CoordinateSystem
Private PtStart As Point
Private PtEnd As Point
Public Function GetThePoint() As Point
Dim Pt As Point = New Point
Pt.x = CShort(InputBox("Enter the x coordinate of the point: "))
Pt.y = CShort(InputBox("Enter the y coordinate of the point: "))
Return Pt
End Function
Public Sub ShowThePoint(ByVal Coord As Point)
MsgBox("Point Coordinate P(" & Coord.x & ", " & Coord.y & ")")
End Sub
End Class
Public Function Main() As Integer
Dim Coordinate As Point
Dim Coordinator As CoordinateSystem
Coordinator = New CoordinateSystem
Coordinate = Coordinator.GetThePoint()
Coordinator.ShowThePoint(Coordinate)
Return 0
End Function
End Module
Here is an example of running the program:



As done for the arguments of primitive types, you can pass more than one class as argument to a method.
Because classes are always used as references, when passing a class as argument, it is implied to be passed by reference. To reinforce this, you can type the ByRef keyword to the left of the argument. Here are examples:
Public Module Exercise
Public Class Point
Friend x As Short
Friend y As Short
End Class
Public Class CoordinateSystem
Private PtStart As Point
Private PtEnd As Point
Public Function GetThePoint() As Point
Dim Pt As Point = New Point
Pt.x = CShort(InputBox("Enter the x coordinate of the point: "))
Pt.y = CShort(InputBox("Enter the y coordinate of the point: "))
Return Pt
End Function
Public Sub ShowTheLine(ByRef StartPoint As Point, _
ByRef EndPoint As Point)
MsgBox("Line Characteristics: The line goes from P(" & _
StartPoint.x & ", " & StartPoint.y & ") to Q(" & _
EndPoint.x & ", " & EndPoint.y & ")")
End Sub
End Class
Public Function Main() As Integer
Dim First, Second As Point
Dim Coordinator As CoordinateSystem
Coordinator = New CoordinateSystem
First = Coordinator.GetThePoint()
Second = Coordinator.GetThePoint()
Coordinator.ShowTheLine(First, Second)
Return 0
End Function
End Module
|
|
Public Module ElectronicStore
Dim DiscountAmount As Double
Dim NetPrice As Double
Dim Quantity As Integer
Dim SaleTotal As Double
Public Function GetDiscountRate() As Double
Dim Discount As Double
Discount = _
CDbl(InputBox("Discount Applied (Enter 0 to 100, 0 if no discount): "))
Return Discount
End Function
Public Function GetQuantity() As Integer
Dim q As Integer
q = CInt(inputbox("Enter Quantity: "))
Return q
End Function
Public Function Create() As StoreItem
Dim ItemNumber As Long
Dim Category As Char
Dim Make As String
Dim Model As String
Dim Price As Double
Dim SaleItem As StoreItem = New StoreItem
ItemNumber = CLng(InputBox("Enter the Item #", "Nearson Electonics"))
Category = CChar(InputBox("Category" & vbCrLf & _
"A - Audio Cables" & vbCrLf & _
"B - Instructional and Tutorials (Books)" & vbCrLf & _
"C - Cell Phones and Accessories" & vbCrLf & _
"D - Bags and Cases" & vbCrLf & _
"E - Headphones" & vbCrLf & _
"F - Instructional and Tutorials (VHS & DVD)" & vbCrLf & _
"G - Digital Cameras" & vbCrLf & _
"H - Cables and Connectors" & vbCrLf & _
"I - PDAs and Accessories" & vbCrLf & _
"J - Telephones and Accessories" & vbCrLf & _
"K - Surge Protector" & vbCrLf & _
"L - TVs and Videos" & vbCrLf & _
"Enter Your Choice:", "Nearson Electonics"))
Make = InputBox("Enter Make", "Nearson Electonics")
Model = InputBox("Enter Model", "Nearson Electonics")
Price = CDbl(InputBox("Enter Unit Price:", "Nearson Electonics"))
SaleItem.SetItemNumber(ItemNumber)
SaleItem.SetCategory(Category)
SaleItem.SetMake(Make)
SaleItem.SetModel(Model)
SaleItem.SetUnitPrice(Price)
Return SaleItem
End Function
Public Sub ShowSaleItem(ByVal item As StoreItem)
Dim discountRate As Double = GetDiscountRate()
Quantity = CInt(GetQuantity())
DiscountAmount = item.GetUnitPrice() * discountRate / 100
NetPrice = item.GetUnitPrice() - DiscountAmount
SaleTotal = NetPrice * Quantity
MsgBox("=-= Nearson Electonics =-=" & vbCrLf & _
"Store Item Description" & vbCrLf & _
"Item Number:" & vbTab & item.GetItemNumber() & vbCrLf & _
"Category:" & vbTab & item.GetCategory() & vbCrLf & _
"Make:" & vbTab & vbTab & item.GetMake() & vbCrLf & _
"Model:" & vbTab & vbTab & item.GetModel() & vbCrLf & _
"Unit Price:" & vbTab & _
FormatCurrency(item.GetUnitPrice()) & vbCrLf & _
"Discount Rate:" & vbTab & _
FormatPercent(discountRate / 100) & vbCrLf & _
"Discount Amount:" & vbTab & _
FormatCurrency(DiscountAmount) & vbCrLf & _
"Price/Item:" & vbTab & FormatCurrency(NetPrice) & vbCrLf & _
"Quantity: " & vbTab & Quantity & vbCrLf & _
"Sale Total: " & vbTab & FormatCurrency(SaleTotal), _
MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, _
"Nearson Electonics")
End Sub
Public Function Main() As Integer
Dim Item As StoreItem = New StoreItem()
Item = Create()
ShowSaleItem(Item)
End Function
End Module
|
| Item # | 917305 | ![]() |
| Category | e | |
| Make | Sennheiser | |
| Model | HD280 Pro Closed-Back Headphones | |
| Unit Price | 99.95 | |
| Discount | 20 | |
| Quantity | 4 | |
![]() |
||
|
Passing a Class as its Own Argument |
An instance of a class can be passed as an argument to one of its own methods (if you have programmed in C++, an example of this implementation is the copy constructor; although you can legitimately create a copy constructor in C#, it does not have the exact same concept as in C++, probably because C# has the Equals() method, which is actually a concept of the .NET Framework). To do this, you primarily pass the argument as if it were any class. Here is an example:
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub Equivalent(ByVal Same As Point)
End Sub
End Class
Then, in the body of the method, do whatever you want. You can, or you may not, use the argument. Still, if you decide to use the argument, know that all of the other members of the class are available through the argument. Probably the simplest way to use the argument is the assign each of of its values to the equivalent member of the class. Here is an example:
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub Equivalent(ByVal Same As Point)
Me.x = Same.x
Me.y = Same.y
End Sub
End Class
When calling the method, make sure you pass an instance of the class to it. You can first create and define the class, then pass it. Here is an example:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub Equivalent(ByVal Same As Point)
Me.x = Same.x
Me.y = Same.y
End Sub
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
ShowPoint(Pt)
Dim One As Point = New Point
One.Equivalent(Pt)
ShowPoint(One)
Return 0
End Function
End Module
This would produce:


Instead of first declaring a variable of the class and initializing it, you can create an instance of the class in the parentheses of the calling method. To do this, you may need a constructor that can specify the values of the fields of the class so the argument can be rightfully initialized. Here is an example:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub New()
End Sub
Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer)
Me.x = XCoord
Me.y = YCoord
End Sub
Public Sub Equivalent(ByVal Same As Point)
Me.x = Same.x
Me.y = Same.y
End Sub
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
ShowPoint(Pt)
Dim One As Point = New Point
One.Equivalent(New Point(-3, 2))
ShowPoint(One)
Return 0
End Function
End Module
This would produce:


Instead of a formal method, you can use a constructor of the class to pass an instance of the same class. Then, in the constructor, use the argument as you see fit, knowing that all the members of the class are available. Here is an example:
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub New()
End Sub
Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer)
Me.x = XCoord
Me.y = YCoord
End Sub
Public Sub New(ByVal Same As Point)
Me.x = Same.x
Me.y = Same.y
End Sub
End Class
Obviously the purpose of passing a class to one of its own methods is not to find its equivalent. The C# language (actually the .NET Framework) can also take care of that (through the Equals() built-in method). Instead, you can create a method that takes an instance of the same class but modifies that instance. For example, for our Point class, we may want to create a new point that is distanced by one unit from the current Point object. Here is an example of doing that:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub New()
End Sub
Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer)
Me.x = XCoord
Me.y = YCoord
End Sub
Public Sub Equivalent(ByVal Same As Point)
Me.x = Same.x
Me.y = Same.y
End Sub
Public Sub CreatePointOneUnitAway(ByVal AddUnit As Point)
Me.x = AddUnit.x + 1
Me.y = AddUnit.y + 1
End Sub
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
Dim One As Point = New Point
One.CreatePointOneUnitAway(Pt)
ShowPoint(One)
One.CreatePointOneUnitAway(New Point(-8, -3))
ShowPoint(One)
Return 0
End Function
End Module
This would produce:


|
Returning a Class From its Own Method |
You can create a method in a class that returns an instance of the class. To start, on the left side of the method, enter the name of the class. Here is an example:
Public Class Point
Public Function MethodName() As Point
End Function
End Class
There are various ways you can deal with the method. If you want to return a new value of the class, you can declare an instance of the class, initialize it, and then return it. Here is an example:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub New()
End Sub
Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer)
Me.x = XCoord
Me.y = YCoord
End Sub
Public Sub New(ByVal Same As Point)
Me.x = Same.x
Me.x = Same.x
End Sub
Public Function AdvanceBy5() As Point
Dim Some As Point = New Point
Some.x = 5
Some.y = 5
Return Some
End Function
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
ShowPoint(Pt)
Dim Away5 As Point = Pt.AdvanceBy5()
ShowPoint(Away5)
Return 0
End Function
End Module
This would produce:


Alternatively, you can declare an instance of the class, use the current values of the class combined with the those of the instance to get new values, and then return the instance.
Remember that, to call a method, if it is not static, you will need to declare an instance of the class from where you are calling the method. The second type of implementation consists of modifying the instance of the class that is calling the method. For example, you can add values to its fields or you can perform any other operation you want on the members of the calling instance. is an example:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub New()
End Sub
Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer)
Me.x = XCoord
Me.y = YCoord
End Sub
Public Sub New(ByVal Same As Point)
Me.x = Same.x
Me.x = Same.x
End Sub
REM This method adds 1 to each field of the class
REM to get a new point away North-East of the current point
Public Function CreatePointOneUnitAway() As Point
Me.x = Me.x + 1
Me.y = Me.y + 1
Return Me
End Function
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
ShowPoint(Pt)
Dim One As Point = New Point(-8, 5)
Dim Another As Point = One.CreatePointOneUnitAway()
ShowPoint(Another)
Return 0
End Function
End Module
This would produce:


As we have learned now, you can create a method that takes an argument that is the same type as its parent class. In the method, you can access any member of the class, including calling the other methods of the class.
Static Variables
Introduction
Consider the following program:
Public Module Exercise
Private Sub Starter(ByVal y As Integer)
Dim a As Double = 112.5
Dim b As Double = 175.25
Dim Result As String
a = a / y
b = b + 2
Result = "y = " & vbTab & CStr(y) & vbCrLf & _
"a = " & vbTab & CStr(a) & vbCrLf & _
"b = " & vbTab & CStr(b) & vbCrLf & _
"b/a = " & vbTab & CStr(b / a)
MsgBox(Result)
End Sub
Public Function Main() As Integer
Starter(2)
Starter(2)
Starter(2)
Starter(2)
Return 0
End Function
End Module
When executed, this program would produce:
The Starter() procedure receives one argument passed when it is called. This procedure also receives the same argument every time. Looking at the result, the argument passed to the procedure and the local variables declared inside of the called procedure keep the same value every time the procedure is called. That is, when the Starter() procedure exits, the values remain the same.
We know that, when a procedure is defined, any variable declared locally belongs to the procedure and its influence cannot expand beyond the body of the procedure. If you want a locally declared variable to keep its changed value when its host procedure is exited, declare such a variable as static.
|
Declaring a Static Variable |
To declare a static variable, type the Static keyword on the left of the Dim keyword. You should always initialize a static variable before using it. To make the local variables of our Starter() function static, we can declare them as follows:
Public Module Exercise
Private Sub Starter(ByVal y As Integer)
Static Dim a As Double = 112.5
Static Dim b As Double = 175.25
Dim Result As String
a = a / y
b = b + 2
Result = "y = " & vbTab & CStr(y) & vbCrLf & _
"a = " & vbTab & CStr(a) & vbCrLf & _
"b = " & vbTab & CStr(b) & vbCrLf & _
"b/a = " & vbTab & CStr(b / a)
MsgBox(Result)
End Sub
Public Function Main() As Integer
Starter(2)
Starter(2)
Starter(2)
Starter(2)
Return 0
End Function
End Module
This time, when executing the program, it would produce:




Notice that, this time, each local variable keeps its newly changed value when the function exits.
|
|
|||
| Previous | Copyright © 2008-2026, FunctionX | Monday 14 February 2016 | Next |
|
|
|||