473,748 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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******** ******@TK2MSFTN GP12.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

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" <thomasdotspink atsp@mntlworldd otcom> wrote in message news:Ol******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP12.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


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


Nov 20 '05 #5
Public Sub addTwoChildren( ByRef parentobject As System.Object)
If parentobject.Ge tType Is GetType(ParentT ype) Then
ReDim DirectCast(pare ntobject, ParentType).Chi ld(1)
End If
parentobject.Pa rentName = "Susan"
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

Although it would be better if I could DirectCast the whole object 8-)

Thanks for the help.

"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


Nov 20 '05 #6

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

Similar topics

3
14944
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
6
2800
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 in C# and we're using remoting for client to server communication.
8
2117
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, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
3
2540
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 Object, it throws a cast error. The only way I've been able to get this to work is to return an ArrayList and then use the .CopyTo method of the ArrayList to stuff the ArrayList into my array. I've tried DirectCast and several other methods to get...
11
8128
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
7
3305
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
3
7305
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 a bit of web research). However, now I've tried passing complex data types between the two, and could not get that to work. What I did was to declare my data type on the Axis/Java side as an Object implementing java.io.Serializable, and when I...
8
8903
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 webmethod1(object a) where a can be of type string, DateTime, float, etc..
17
7253
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 to show the array data to the end user. Can I do that? How?
0
8983
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9528
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.