473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 10036
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: rihad | last post by:
Hi, I've this question: suppose we have two differently typed pointers: struct foo *foo; char **array; Becase one can always legally cast any pointer to (void *), and becase (void *) is...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurzweil | last post by:
I need to make a two dimensional array of objects. These objects are of type Influence. How do I declare such an array? Now I use: private object influences; influences = GetInfluences(); //...
4
by: Fabrizio | last post by:
Hi I cannot figure why it isn't possible to cast a struct array to an object array. I written a structure like this: public struct Test { private int TestA; private int TestB;
3
by: Barry Mossman | last post by:
Hi, I get the feeling that I am missing something with regards to casting. The CopyTo method allows me to copy the contents of a collection into an array. My collection is a MatchCollection...
0
by: Jon the Blind | last post by:
I have a set of business object classes built from the typed dataset tool in Visual Studio. Since the individual objects (such as the Order object, for example) inherit from DataRow, a common...
0
by: Phil Jones | last post by:
If I have an array of 's, all of the same type (say 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...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
1
by: conchur | last post by:
Is there an elegant way to have a class utilise an inherited method to return a Typed Collection of itself? Example: class MyList<T> : List<T> where T : MyRoot { Add... Remove... }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.