473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Passing Objects as type System.Object

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
Nov 20 '05 #1
5 1293
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

Nov 20 '05 #2
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" <st*****@iname.com> wrote in message
news:#D**************@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

Nov 20 '05 #3
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.

"Tom Spink" <thomasdotspinkatsp@mntlworlddotcom> wrote in message news:Ol**************@TK2MSFTNGP09.phx.gbl...
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" <st*****@iname.com> wrote in message
news:#D**************@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


Nov 20 '05 #4
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


Nov 20 '05 #5
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 8-)

Thanks for the help.

"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


Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
3
by: Stephen Travis | last post by:
I'm trying to write a subroutine that will fill an array of some type with several objects of that type. The subroutine works fine if I directly reference the array but if I pass the array as an...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: andreas.baus | last post by:
Hello. I'm trying to exchange data between a webservice based on Apache Axis and a client written in C#, and so far it has been working with few problems (none that I couldn't solve myself with...
8
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I'm trying to pass values of different data-types to a web-service. I thought it would be easier to box these values and pass them as a System.object parameter, like public void...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.