Casting Exception. Simple Code... Why! Why! Why!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




I have what seems to be a very simple straight-forward program (shown
below), but I'm getting an error:

Unable to cast object of type 'TestApp1.ParentTable' to type
'TestApp1.ChildTable'.

on the line: objTable = .SelectedOptions (the fifth line from
the top in the following code).

Why is this? I should be able to go from a parent to the child,
correct? .SelectedOptions is of type ParentTable, and objTable is of
type ChildTable. ChildTable inherits from ParentTable. I've also
tried the commands Ctype and DirectCast, and this does not solve the
problem.

Can anyone tell me how to resolve this?

Thanks


--------------------------------------------------
Module Toolbars

Public Sub Main()

Dim objTable As ChildTable

With frmClass
objTable = .SelectedOptions
End With

End Sub

End Module
--------------------------------------------------
Public Class frmClass

Public ReadOnly Property SelectedOptions() As ParentTable
Get

Dim objTable As New ParentTable

With objTable
.strParent = "SelectedOption"
End With

Return objTable

End Get
End Property
End Class
--------------------------------------------------
Option Explicit On

Public Class ChildTable
Inherits ParentTable

Public ReadOnly Property strChild() As String
Get
Return "strChild"
End Get
End Property

End Class

Public Class ParentTable
Public strParent As String
End Class
--------------------------------------------------

.



Relevant Pages

  • C#/2.0: Tricky issue with generics.
    ... public class NodeList: ... NodeList can take a Node that is the parent. ... the case should be totally valid in my optinion. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading Faux Pa
    ... the Parent class data from the database and loads all the objects. ... a TCPClient that is wrapped in a 'communicator' class. ... public class ObjectModelController ... private void LoadObjectModel() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: why is a superclass allowed to access protected methods of a subclass?
    ... when both the Parent and Child have a method ... > package FirstGeneration; ... > public class Parent ... protected void doIt { ...
    (comp.lang.java.programmer)
  • Question on inheritance
    ... public class Child: Parent ... public new void Meth() ... though variable cc was assigned Child object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Nested Object passed Parent by ref
    ... I have an object which has a nested object which need to look at the ... (the parent being readonly). ... public class Status ... public string UserCurrentLevel ...
    (microsoft.public.dotnet.languages.csharp)