473,503 Members | 3,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10047
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
8883
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
22919
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
9960
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
16265
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
1542
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
1057
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
221
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
16851
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
7949
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
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7315
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...
1
6974
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...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.