Here is a test program I wrote to explore a facet of object casting ...
The problem comes with the TryCast statement on class3. My intention is that class3 remains as a baseclass, but when the TryCast is applied, class3 morphs itself into an Inherited class which is not what I wanted.
So is there a gap in my OOP/VB.NET knowledge and this action is legitimate or should class3 still be regarded as a base class?
VB.NET:
Module Module1
Sub Main()
Dim class1 As MyBaseClass = New MyBaseClass()
Dim class2 As MyInheritedClass = New MyInheritedClass()
Dim class3 As MyBaseClass = Nothing
If TypeOf class1 Is MyBaseClass Then
Console.WriteLine("Class1 is MyBaseClass")
Else
Console.WriteLine("Class1 is not MyBaseClass")
End If
If TypeOf class1 Is MyInheritedClass Then
Console.WriteLine("Class1 is MyInheritedClass")
Else
Console.WriteLine("Class1 is not MyInheritedClass")
End If
If TypeOf class2 Is MyBaseClass Then
Console.WriteLine("Class2 is MyBaseClass")
Else
Console.WriteLine("Class2 is not MyBaseClass")
End If
If TypeOf class2 Is MyInheritedClass Then
Console.WriteLine("Class2 is MyInheritedClass")
Else
Console.WriteLine("Class2 is not MyInheritedClass")
End If
class3 = TryCast(class2, MyBaseClass)
If TypeOf class3 Is MyBaseClass Then
Console.WriteLine("Class3 is MyBaseClass")
Else
Console.WriteLine("Class3 is not MyBaseClass")
End If
If TypeOf class3 Is MyInheritedClass Then
Console.WriteLine("Class3 is MyInheritedClass")
Else
Console.WriteLine("Class3 is not MyInheritedClass")
End If
Console.ReadKey()
End Sub
End Module
The problem comes with the TryCast statement on class3. My intention is that class3 remains as a baseclass, but when the TryCast is applied, class3 morphs itself into an Inherited class which is not what I wanted.
So is there a gap in my OOP/VB.NET knowledge and this action is legitimate or should class3 still be regarded as a base class?