Casting an Array of Objects to a typed Array

  • Thread starter Thread starter Phil Jones
  • Start date Start date
P

Phil Jones

If I have an array of [Object]'s, all of the same type (say [String] for
example).

Is there a quick way to cast that to a typed String array?

Presently I'm having to copy each object to a new array. I'm wondering if
there a conversion class within the framework or VB statement that makes
this quick and simple.

Cheers everyone.
 
Phil,

You mean something as this?
Dim a() As Object = {"You", "never", "know", "why"}
MessageBox.Show(DirectCast(a(2), String))

I do not know a method to make a deep copy in one time and never saw it
although it is more time asked in this newsgroup.

Cor
 
Please don't multi-post. You could have cross-posted this instead to
whatever groups you wanted the question to appear in.

Here's your VB version (for reference types):

Dim str() As String = DirectCast(objArray, String())

Note that objArray is an object array which contains string elements. Again,
this won't work for value types. You can either use Array.Copy or cast each
element individually.

Dim iArray(objIntArray.Length - 1) As Integer
Array.Copy(objIntArray, iArray, objIntArray.Length)


hope that helps..
Imran.
 
Sorry dude. What do you mean by "cross-posted"? Is that putting all the
groups within the address of one message?

Thanks for the VB version! And thank you everyone else who responded.

--
===
Phil
(Auckland | Aotearoa)


Imran Koradia said:
Please don't multi-post. You could have cross-posted this instead to
whatever groups you wanted the question to appear in.

Here's your VB version (for reference types):

Dim str() As String = DirectCast(objArray, String())

Note that objArray is an object array which contains string elements.
Again, this won't work for value types. You can either use Array.Copy or
cast each element individually.

Dim iArray(objIntArray.Length - 1) As Integer
Array.Copy(objIntArray, iArray, objIntArray.Length)


hope that helps..
Imran.


Phil Jones said:
If I have an array of [Object]'s, all of the same type (say [String] for
example).

Is there a quick way to cast that to a typed String array?

Presently I'm having to copy each object to a new array. I'm wondering
if
there a conversion class within the framework or VB statement that makes
this quick and simple.

Cheers everyone.
 
Phil Jones said:
Sorry dude. What do you mean by "cross-posted"? Is
that putting all the groups within the address of one message?

Yes, but /only/ post the question to relevant groups (three groups at max.).
 

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

Back
Top