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