PC Review


Reply
Thread Tools Rate Thread

Casting an Array of Objects to a typed Array

 
 
Phil Jones
Guest
Posts: n/a
 
      14th Nov 2004
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)


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      14th Nov 2004
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)
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      14th Nov 2004
"Phil Jones" <(E-Mail Removed)> 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/>


 
Reply With Quote
 
Imran Koradia
Guest
Posts: n/a
 
      14th Nov 2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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)
>



 
Reply With Quote
 
Phil Jones
Guest
Posts: n/a
 
      15th Nov 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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)
>>

>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      15th Nov 2004
> Sorry dude. What do you mean by "cross-posted"? Is that putting all the
> groups within the address of one message?


Yes


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      15th Nov 2004
"Phil Jones" <(E-Mail Removed)> 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/>


 
Reply With Quote
 
Phil Jones
Guest
Posts: n/a
 
      15th Nov 2004
Understood - thank you


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem instantiating an array of objects or array of class instances raylopez99 Microsoft C# .NET 3 17th Sep 2007 07:16 PM
Easy way to cast an array of strings to an array of objects? ssg31415926 Microsoft C# .NET 3 30th Aug 2005 08:39 PM
Casting an Array of Objects to a typed Array Phil Jones Microsoft Dot NET Framework 2 14th Nov 2004 09:31 AM
Casting objects in an array (well, a SortedList) doesn't work Marc W. Microsoft C# .NET 7 23rd Jan 2004 02:24 AM
Is an array of Objects in vb.net a good equivalent for an array of Variants in v WayneM Microsoft VB .NET 4 21st Nov 2003 10:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:35 AM.