Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting an Array of Objects to a typed Array

Phil Jones
Guest
 
Posts: n/a
#1: Nov 21 '05
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)



Cor Ligthert
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Casting an Array of Objects to a typed Array


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
[color=blue]
> 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)
>[/color]


Cor Ligthert
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Casting an Array of Objects to a typed Array


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
[color=blue]
> 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)
>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Casting an Array of Objects to a typed Array


"Phil Jones" <phil_newsgroup@hotmail.com> schrieb:[color=blue]
> 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?[/color]

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Casting an Array of Objects to a typed Array


"Phil Jones" <phil_newsgroup@hotmail.com> schrieb:[color=blue]
> 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?[/color]

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Imran Koradia
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Casting an Array of Objects to a typed Array


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" <phil_newsgroup@hotmail.com> wrote in message
news:%23tvgKsiyEHA.3368@TK2MSFTNGP15.phx.gbl...[color=blue]
> 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)
>[/color]


Phil Jones
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Casting an Array of Objects to a typed Array


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" <nojunk@microsoft.com> wrote in message
news:uPL7PDlyEHA.3368@TK2MSFTNGP15.phx.gbl...[color=blue]
> 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" <phil_newsgroup@hotmail.com> wrote in message
> news:%23tvgKsiyEHA.3368@TK2MSFTNGP15.phx.gbl...[color=green]
>> 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)
>>[/color]
>
>[/color]


Cor Ligthert
Guest
 
Posts: n/a
#8: Nov 21 '05

re: Casting an Array of Objects to a typed Array


> Sorry dude. What do you mean by "cross-posted"? Is that putting all the[color=blue]
> groups within the address of one message?[/color]

Yes


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#9: Nov 21 '05

re: Casting an Array of Objects to a typed Array


"Phil Jones" <phil_newsgroup@hotmail.com> schrieb:[color=blue]
> Sorry dude. What do you mean by "cross-posted"? Is
> that putting all the groups within the address of one message?[/color]

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Phil Jones
Guest
 
Posts: n/a
#10: Nov 21 '05

re: Casting an Array of Objects to a typed Array


Understood - thank you


Closed Thread