S
Stephen Travis
I'm trying to ReDim a child object after passing the parent object as System.Object and it throws a System.InvalidCastException:
Cast from type 'Object()' to type 'ChildType()' is not valid. If I pass the parent object as its type, the ReDim succeeds. How can I
ReDim a child object from a parent object passed as System.Object?
Here's the problem...
Private Sub Page_Load
Dim NewParent As New ParentType
addTwoChildrenSucceeds(NewParent)
addTwoChildrenFails(NewParent)
End Sub
Public Sub addTwoChildrenFails(ByVal parentobject As System.Object)
ReDim parentobject.Child(1) ' Throws the exception.
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub
Public Sub addTwoChildrenSucceeds(ByVal parentobject As ParentType)
ReDim parentobject.Child(1)
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub
Public Class ParentType
Public ParentName As System.String
Public Child() As ChildType
End Class
Public Class ChildType
Public ChildName As System.String
End Class
Cast from type 'Object()' to type 'ChildType()' is not valid. If I pass the parent object as its type, the ReDim succeeds. How can I
ReDim a child object from a parent object passed as System.Object?
Here's the problem...
Private Sub Page_Load
Dim NewParent As New ParentType
addTwoChildrenSucceeds(NewParent)
addTwoChildrenFails(NewParent)
End Sub
Public Sub addTwoChildrenFails(ByVal parentobject As System.Object)
ReDim parentobject.Child(1) ' Throws the exception.
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub
Public Sub addTwoChildrenSucceeds(ByVal parentobject As ParentType)
ReDim parentobject.Child(1)
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub
Public Class ParentType
Public ParentName As System.String
Public Child() As ChildType
End Class
Public Class ChildType
Public ChildName As System.String
End Class