That throws the same error but this approach works;
Public Sub addTwoChildrenF ails(ByVal parentobject1 As System.Object)
Dim parentobject As New ParentType
parentobject = parentobject1
ReDim parentobject.Ch ild(1) ' Doesn't throw an exception any more
parentobject.Ch ild(0) = New ChildType
parentobject.Ch ild(0).ChildNam e = "Brian"
parentobject.Ch ild(1) = New ChildType
parentobject.Ch ild(1).ChildNam e = "Gail"
End Sub
However, I need to make the Type a variable. For example;
Public Sub addTwoChildrenF ails(ByVal parentobject1 As System.Object)
If parentobject1.G etType Is GetType(ParentT ype) Then Dim parentobject As New ParentType
parentobject = parentobject1
ReDim parentobject.Ch ild(1) ' Doesn't throw an exception any more
parentobject.Ch ild(0) = New ChildType
parentobject.Ch ild(0).ChildNam e = "Brian"
parentobject.Ch ild(1) = New ChildType
parentobject.Ch ild(1).ChildNam e = "Gail"
End Sub
This creates a scope problem. Your original approach avoids this problem. Any ideas?
"The Grim Reaper" <gr*********@bt openworld.com> wrote in message news:cc******** **@hercules.bti nternet.com...
You're nearly there...
Public Sub addTwoChildrenF ails(ByVal parentobject As System.Object)
parentobject = DirectCast(pare ntobject, ParentType)
ReDim parentobject.Ch ild(1) ' Doesn't throw an exception any
more
parentobject.Ch ild(0) = New ChildType
parentobject.Ch ild(0).ChildNam e = "Brian"
parentobject.Ch ild(1) = New ChildType
parentobject.Ch ild(1).ChildNam e = "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******** ********@TK2MSF TNGP12.phx.gbl. .. I'm trying to ReDim a child object after passing the parent object as
System.Object and it throws a System.InvalidC astException: 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
addTwoChildrenS ucceeds(NewPare nt)
addTwoChildrenF ails(NewParent)
End Sub
Public Sub addTwoChildrenF ails(ByVal parentobject As System.Object)
ReDim parentobject.Ch ild(1) ' Throws the exception.
parentobject.Ch ild(0) = New ChildType
parentobject.Ch ild(0).ChildNam e = "Brian"
parentobject.Ch ild(1) = New ChildType
parentobject.Ch ild(1).ChildNam e = "Gail"
End Sub
Public Sub addTwoChildrenS ucceeds(ByVal parentobject As ParentType)
ReDim parentobject.Ch ild(1)
parentobject.Ch ild(0) = New ChildType
parentobject.Ch ild(0).ChildNam e = "Brian"
parentobject.Ch ild(1) = New ChildType
parentobject.Ch ild(1).ChildNam e = "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