DNAunion2000
08-03-2003, 01:17 AM
/*DNAunion*/ Incremental evolution of a simple VB .NET example of using classes, properties, and inheritance. (PS: I have tested all of the code listed and it works).
First, the obligatory “Hello World!” example.
Module Module1
Sub Main()
Console.WriteLine("Hello World!”)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
End Module
The last two calls to Console functions are used so that the previous output, “Hello World!”, remains on screen to be read . They are needed because when you choose Start from the debug menu (or use F5) VB .NET will run straight through a console app in a millisecond and then immediately close. Putting a command that waits for user input (the ReadLine() function) prevents that from occurring instantaneously. An alternative to adding those extra lines is to instead choose Start Without Debugging from the debug menu (or use Shift + F5). However, turning off debugging might be a bad habit to get into.
Okay, now up one level...a very simple class.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece
myChessPiece.Value = 15000
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Public Value As Integer
End Class
End Module
The new code at the bottom defines about the simplest class one can have: it has only a single data member and no methods…and so its data member has to be public (generally not a good idea). In main(), an instance named myChessPiece is created and the public data member is assigned a value: an invalid value. That’s one problem with having classes use public data members.
So let’s change that field to be private instead and implement it as a property.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece
myChessPiece.Value = 15000
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
End Module
Now the myChessPiece object cannot take on an invalid state: the Set portion of the Property prevents invalid values from being assigned.
Next, instead of making the program create the object, and then assign a value to its instance variables, we can add a constructor that does that during initialization.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
End Module
In C-style languages, a constructor’s name is the same as its class, but in Visual Basic .NET, it is Sub New().
Note that the constructor does not assign the value directly to the class’s private data member, even though it could. Instead, the constructor does so indirectly, through the Property. This is by no means required, but it does make the class a bit less “fragile” (i.e., there would be fewer changes needed were the name of the private variable changed).
Next, we’ll setup a default constructor: one that has no parameters.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New()
Value = 0
End Sub
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
End Module
We now have two functions with the same name in the same class, which is known as function overloading. This requires that each of the overloaded functions have a unique function signature, which is derived from the function name and the number, order, and data types of the parameters.
That class has evolved a fair amount, so let’s switch now to adding inheritance. Most of the stuff in the new class will be like in the other one…only new things will be addressed.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New()
Value = 0
End Sub
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
Public Class Rook
Inherits ChessPiece
Public Sub New(ByVal HasRookMoved As Boolean)
myBase.New(10)
HasMoved = HasRookMoved
End Sub
Private mHasMoved As Boolean
Public Property HasMoved() As Boolean
Get
Return mHasMoved
End Get
Set (ByVal HasRookMoved As Boolean)
mHasMoved = HasRookMoved
End Set
End Property
End Class
End Module
First note the first line of code after the class name. This (obviously) indicates that the Rook class is inheriting from (or “extending”) the base class ChessPiece. Therefore, Rook automatically gains all of the data members and methods of ChessPiece (they’re in the Rook class, you just can’t see them written out in code).
The other highlighted line of code calls the base class’s (ChessPiece’s) constructor. Once the base class is fully setup, then the derived class finishes setting itself up.
Of course, we defined the new class but didn’t use it. So as the final step, let’s see how to do so.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Dim myRook = New Rook(True)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Rook value is " & myRook.Value)
Console.WriteLine("Rook has moved is “ & myRook.HasMoved)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New()
Value = 0
End Sub
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
Public Class Rook
Inherits ChessPiece
Public Sub New(ByVal HasRookMoved As Boolean)
myBase.New(10)
HasMoved = HasRookMoved
End Sub
Private mHasMoved As Boolean
Public Property HasMoved() As Boolean
Get
Return mHasMoved
End Get
Set (ByVal HasRookMoved As Boolean)
mHasMoved = HasRookMoved
End Set
End Property
End Class
End Module
“Voila..the trick, she is done!”
First, the obligatory “Hello World!” example.
Module Module1
Sub Main()
Console.WriteLine("Hello World!”)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
End Module
The last two calls to Console functions are used so that the previous output, “Hello World!”, remains on screen to be read . They are needed because when you choose Start from the debug menu (or use F5) VB .NET will run straight through a console app in a millisecond and then immediately close. Putting a command that waits for user input (the ReadLine() function) prevents that from occurring instantaneously. An alternative to adding those extra lines is to instead choose Start Without Debugging from the debug menu (or use Shift + F5). However, turning off debugging might be a bad habit to get into.
Okay, now up one level...a very simple class.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece
myChessPiece.Value = 15000
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Public Value As Integer
End Class
End Module
The new code at the bottom defines about the simplest class one can have: it has only a single data member and no methods…and so its data member has to be public (generally not a good idea). In main(), an instance named myChessPiece is created and the public data member is assigned a value: an invalid value. That’s one problem with having classes use public data members.
So let’s change that field to be private instead and implement it as a property.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece
myChessPiece.Value = 15000
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
End Module
Now the myChessPiece object cannot take on an invalid state: the Set portion of the Property prevents invalid values from being assigned.
Next, instead of making the program create the object, and then assign a value to its instance variables, we can add a constructor that does that during initialization.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
End Module
In C-style languages, a constructor’s name is the same as its class, but in Visual Basic .NET, it is Sub New().
Note that the constructor does not assign the value directly to the class’s private data member, even though it could. Instead, the constructor does so indirectly, through the Property. This is by no means required, but it does make the class a bit less “fragile” (i.e., there would be fewer changes needed were the name of the private variable changed).
Next, we’ll setup a default constructor: one that has no parameters.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New()
Value = 0
End Sub
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
End Module
We now have two functions with the same name in the same class, which is known as function overloading. This requires that each of the overloaded functions have a unique function signature, which is derived from the function name and the number, order, and data types of the parameters.
That class has evolved a fair amount, so let’s switch now to adding inheritance. Most of the stuff in the new class will be like in the other one…only new things will be addressed.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New()
Value = 0
End Sub
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
Public Class Rook
Inherits ChessPiece
Public Sub New(ByVal HasRookMoved As Boolean)
myBase.New(10)
HasMoved = HasRookMoved
End Sub
Private mHasMoved As Boolean
Public Property HasMoved() As Boolean
Get
Return mHasMoved
End Get
Set (ByVal HasRookMoved As Boolean)
mHasMoved = HasRookMoved
End Set
End Property
End Class
End Module
First note the first line of code after the class name. This (obviously) indicates that the Rook class is inheriting from (or “extending”) the base class ChessPiece. Therefore, Rook automatically gains all of the data members and methods of ChessPiece (they’re in the Rook class, you just can’t see them written out in code).
The other highlighted line of code calls the base class’s (ChessPiece’s) constructor. Once the base class is fully setup, then the derived class finishes setting itself up.
Of course, we defined the new class but didn’t use it. So as the final step, let’s see how to do so.
Module Module1
Sub Main()
Dim myChessPiece = New ChessPiece(50)
Dim myRook = New Rook(True)
Console.WriteLine("ChessPiece value is " & myChessPiece.Value)
Console.WriteLine("Rook value is " & myRook.Value)
Console.WriteLine("Rook has moved is “ & myRook.HasMoved)
Console.WriteLine("Press [Enter] to continue...")
Console.ReadLine()
End Sub
Public Class ChessPiece
Private mValue As Integer
Public Sub New()
Value = 0
End Sub
Public Sub New(ByVal PieceValue As Integer)
Value = PieceValue
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal PieceValue As Integer)
If (PieceValue >= 0 AndAlso PieceValue <= 100)
mValue = PieceValue
Else
‘ could inform the user of the error
mValue = 0
EndIf
End Set
End Property
End Class
Public Class Rook
Inherits ChessPiece
Public Sub New(ByVal HasRookMoved As Boolean)
myBase.New(10)
HasMoved = HasRookMoved
End Sub
Private mHasMoved As Boolean
Public Property HasMoved() As Boolean
Get
Return mHasMoved
End Get
Set (ByVal HasRookMoved As Boolean)
mHasMoved = HasRookMoved
End Set
End Property
End Class
End Module
“Voila..the trick, she is done!”