By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

Passing an array as Object

P: n/a
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 around this problem with no luck. Is there some way to populate an array in a
subroutine without using a specific type or passing it as Object? Passing it as Object seems to recast it from its original type.

Here's the test case.
Private MyObjects() As SomeObjectType

Public Class SomeObjectType
Public someproperty As System.String
Public Sub somemethod()
End Sub
End Class

Private Sub Page_Load()
DoSomethingSucceeds()
DoSomethingFails(MyObjects) ' System.InvalidCastException: Specified cast is not valid.
End Sub

Private Sub DoSomethingSucceeds()
ReDim MyObjects(0)
MyObjects(0) = New SomeObjectType
MyObjects(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In MyObjects
MyObject.somemethod()
Next
End Sub

Private Sub DoSomethingFails(ByRef arr As Object)
ReDim arr(0)
arr(0) = New SomeObjectType
arr(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In arr
MyObject.somemethod()
Next
End Sub

End Class



Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
this worked for me:

private myObjects() as Object

DoSomething(myObjects)

sub DoSomething(obj() as Object)
redim obj(0)
obj(0) = New System.Collections.Hashtable
DirectCast(obj(0), System.Collections.Hashtable).Add("key", "item")
MessageBox.Show(CType(DirectCast(obj(0),
System.Collections.Hashtable).Item("key"), String))
end sub

The problem lies in the fact that your method DoSomethingFails takes in an
object and you are passing in an array of objects. You need to define your
method to take in an array of objects and it should work fine.

hope this helps..
Imran.

"Stephen Travis" <st*****@iname.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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 around this problem with no luck. Is there some way to populate an array in a subroutine without using a specific type or passing it as Object? Passing it as Object seems to recast it from its original type.
Here's the test case.
Private MyObjects() As SomeObjectType

Public Class SomeObjectType
Public someproperty As System.String
Public Sub somemethod()
End Sub
End Class

Private Sub Page_Load()
DoSomethingSucceeds()
DoSomethingFails(MyObjects) ' System.InvalidCastException: Specified cast is not valid. End Sub

Private Sub DoSomethingSucceeds()
ReDim MyObjects(0)
MyObjects(0) = New SomeObjectType
MyObjects(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In MyObjects
MyObject.somemethod()
Next
End Sub

Private Sub DoSomethingFails(ByRef arr As Object)
ReDim arr(0)
arr(0) = New SomeObjectType
arr(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In arr
MyObject.somemethod()
Next
End Sub

End Class


Nov 20 '05 #2

P: n/a
I would use multiple overloads instead.

Easier to troubleshoot later...

Schneider

"Imran Koradia" <no****@microsoft.com> wrote in message
news:ON*************@TK2MSFTNGP12.phx.gbl...
this worked for me:

private myObjects() as Object

DoSomething(myObjects)

sub DoSomething(obj() as Object)
redim obj(0)
obj(0) = New System.Collections.Hashtable
DirectCast(obj(0), System.Collections.Hashtable).Add("key", "item")
MessageBox.Show(CType(DirectCast(obj(0),
System.Collections.Hashtable).Item("key"), String))
end sub

The problem lies in the fact that your method DoSomethingFails takes in an
object and you are passing in an array of objects. You need to define your
method to take in an array of objects and it should work fine.

hope this helps..
Imran.

"Stephen Travis" <st*****@iname.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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 around this problem
with no luck. Is there some way to populate an array in a
subroutine without using a specific type or passing it as Object?
Passing it as Object seems to recast it from its original type.

Here's the test case.
Private MyObjects() As SomeObjectType

Public Class SomeObjectType
Public someproperty As System.String
Public Sub somemethod()
End Sub
End Class

Private Sub Page_Load()
DoSomethingSucceeds()
DoSomethingFails(MyObjects) ' System.InvalidCastException: Specified

cast is not valid.
End Sub

Private Sub DoSomethingSucceeds()
ReDim MyObjects(0)
MyObjects(0) = New SomeObjectType
MyObjects(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In MyObjects
MyObject.somemethod()
Next
End Sub

Private Sub DoSomethingFails(ByRef arr As Object)
ReDim arr(0)
arr(0) = New SomeObjectType
arr(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In arr
MyObject.somemethod()
Next
End Sub

End Class



Nov 20 '05 #3

P: n/a
Thanks, always treating it as an object (private myObjects() as Object) did the trick.

"Imran Koradia" <no****@microsoft.com> wrote in message news:ON*************@TK2MSFTNGP12.phx.gbl...
this worked for me:

private myObjects() as Object

DoSomething(myObjects)

sub DoSomething(obj() as Object)
redim obj(0)
obj(0) = New System.Collections.Hashtable
DirectCast(obj(0), System.Collections.Hashtable).Add("key", "item")
MessageBox.Show(CType(DirectCast(obj(0),
System.Collections.Hashtable).Item("key"), String))
end sub

The problem lies in the fact that your method DoSomethingFails takes in an
object and you are passing in an array of objects. You need to define your
method to take in an array of objects and it should work fine.

hope this helps..
Imran.

"Stephen Travis" <st*****@iname.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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 around this problem with

no luck. Is there some way to populate an array in a
subroutine without using a specific type or passing it as Object? Passing

it as Object seems to recast it from its original type.

Here's the test case.
Private MyObjects() As SomeObjectType

Public Class SomeObjectType
Public someproperty As System.String
Public Sub somemethod()
End Sub
End Class

Private Sub Page_Load()
DoSomethingSucceeds()
DoSomethingFails(MyObjects) ' System.InvalidCastException: Specified

cast is not valid.
End Sub

Private Sub DoSomethingSucceeds()
ReDim MyObjects(0)
MyObjects(0) = New SomeObjectType
MyObjects(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In MyObjects
MyObject.somemethod()
Next
End Sub

Private Sub DoSomethingFails(ByRef arr As Object)
ReDim arr(0)
arr(0) = New SomeObjectType
arr(0).someproperty = "somevalue"

For Each MyObject As SomeObjectType In arr
MyObject.somemethod()
Next
End Sub

End Class



Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.