469,282 Members | 2,404 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Casting an Array of Objects to a typed Array

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
(Auckland | Aotearoa)
Nov 21 '05 #1
9 9214
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
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
(Auckland | Aotearoa)

Nov 21 '05 #2
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
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
(Auckland | Aotearoa)

Nov 21 '05 #3
"Phil Jones" <ph************@hotmail.com> schrieb:
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?


<URL:http://groups.google.de/groups?selm=...2MSFTNGP11.phx
..gbl>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #4
"Phil Jones" <ph************@hotmail.com> schrieb:
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?


<URL:http://groups.google.de/groups?selm=...2MSFTNGP11.phx
..gbl>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #5
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" <ph************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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
(Auckland | Aotearoa)

Nov 21 '05 #6
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" <no****@microsoft.com> wrote in message
news:uP**************@TK2MSFTNGP15.phx.gbl...
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" <ph************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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
(Auckland | Aotearoa)


Nov 21 '05 #7
> Sorry dude. What do you mean by "cross-posted"? Is that putting all the
groups within the address of one message?


Yes
Nov 21 '05 #8
"Phil Jones" <ph************@hotmail.com> schrieb:
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.).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #9
Understood - thank you
Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by rihad | last post: by
231 posts views Thread by Brian Blais | last post: by
3 posts views Thread by Kurzweil | last post: by
4 posts views Thread by Fabrizio | last post: by
3 posts views Thread by Barry Mossman | last post: by
reply views Thread by Jon the Blind | last post: by
104 posts views Thread by Leszek | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.