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.
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.
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:
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).
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:
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.
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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
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
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:
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.
|
|
||||||||||||||||||||||||||||||||||
|