That throws the same error but this approach works;
Public Sub addTwoChildrenFails(ByVal parentobject1 As System.Object)
Dim parentobject As New ParentType
parentobject = parentobject1
ReDim parentobject.Child(1) ' Doesn't throw an exception any more
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub
However, I need to make the Type a variable. For example;
Public Sub addTwoChildrenFails(ByVal parentobject1 As System.Object)
If parentobject1.GetType Is GetType(ParentType) Then Dim parentobject As New ParentType
parentobject = parentobject1
ReDim parentobject.Child(1) ' Doesn't throw an exception any more
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub
This creates a scope problem. Your original approach avoids this problem. Any ideas?
"The Grim Reaper" <gr*********@btopenworld.com> wrote in message news:cc**********@hercules.btinternet.com...
You're nearly there...
Public Sub addTwoChildrenFails(ByVal parentobject As System.Object)
parentobject = DirectCast(parentobject, ParentType)
ReDim parentobject.Child(1) ' Doesn't throw an exception any
more
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub
Untested... I'm sure someone else will contradict me.... tis the way of it
:D
_____________________________
The Grim Reaper
"Stephen Travis" <st*****@iname.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... 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