Passing Objects as type System.Object

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
 
T

The Grim Reaper

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 said:
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
 
T

Tom Spink

Hi, May I ask why you pass it as an object... will you have different
classes being ReDim'med?

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
Stephen Travis said:
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
 
S

Stephen Travis

Hi Tom, I'm developing a front-end for a c/s web application that has to support several versions of an XML dialect. For example, my
application has to support two versions of the WMS XML dialect, version 1.3.0 and 1.1.1. One version might have a property that the
other doesn't but otherwise are identical.

So, I create an object of the correct version and pass it to the function as System.Object, which then populates the object
according to it's type and returns it to the caller. Otherwise, I would have to have separate functions to populate every XML
version.
 
S

Stephen Travis

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?
 
S

Stephen Travis

Public Sub addTwoChildren(ByRef parentobject As System.Object)
If parentobject.GetType Is GetType(ParentType) Then
ReDim DirectCast(parentobject, ParentType).Child(1)
End If
parentobject.ParentName = "Susan"
parentobject.Child(0) = New ChildType
parentobject.Child(0).ChildName = "Brian"
parentobject.Child(1) = New ChildType
parentobject.Child(1).ChildName = "Gail"
End Sub

Although it would be better if I could DirectCast the whole object :cool:

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top