473,386 Members | 1,644 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,386 software developers and data experts.

ArrayList.ToArray() question

If i have an ArrayList, add a bunch of MyClass classes to it, how do I get a
MyClass[] out of it? I've tried:

MyClass[] clses = ArrayList.ToArray(typeof(MyClass));

but get errors about not being able to cast. ?

Thanks!

Derrick
Nov 15 '05 #1
4 12966
Derrick,

Yes, because the return type of the ToArray method is Array. You have
to cast it to (MyClass[]), like so:

MyClass[] clses = (MyClass[]) ArrayList.ToArray(typeof(MyClass));

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Derrick" <de*********@excite.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If i have an ArrayList, add a bunch of MyClass classes to it, how do I get a MyClass[] out of it? I've tried:

MyClass[] clses = ArrayList.ToArray(typeof(MyClass));

but get errors about not being able to cast. ?

Thanks!

Derrick

Nov 15 '05 #2
Hi,

You should do this inside a try/catch block as ArrayList do not check that
all the elements being of the same type.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eb*************@TK2MSFTNGP11.phx.gbl...
Derrick,

Yes, because the return type of the ToArray method is Array. You have
to cast it to (MyClass[]), like so:

MyClass[] clses = (MyClass[]) ArrayList.ToArray(typeof(MyClass));

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Derrick" <de*********@excite.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If i have an ArrayList, add a bunch of MyClass classes to it, how do I
get a
MyClass[] out of it? I've tried:

MyClass[] clses = ArrayList.ToArray(typeof(MyClass));

but get errors about not being able to cast. ?

Thanks!

Derrick


Nov 15 '05 #3
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
You should do this inside a try/catch block as ArrayList do not check that
all the elements being of the same type.


Yes it does - it *has* to when you provide a type to ToArray, otherwise
it wouldn't be able return an array of the appropriate type. So ToArray
will throw an exception if the elements aren't of an appropriate type.
Or did you mean that it doesn't check the elements when they're put
into the list?

I wouldn't suggest putting it in a try/catch block though - if that
fails, chances are you've got some nasty internal error, and the
exception should probably bubble up to a higher level.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Hi ,
Yes it does - it *has* to when you provide a type to ToArray, otherwise
it wouldn't be able return an array of the appropriate type. So ToArray
will throw an exception if the elements aren't of an appropriate type.
Or did you mean that it doesn't check the elements when they're put
into the list?
When they are inserted on the list I meant.
I wouldn't suggest putting it in a try/catch block though - if that
fails, chances are you've got some nasty internal error, and the
exception should probably bubble up to a higher level.


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Oueis | last post by:
ArrayList arrayList = new ArrayList(); foreach (Blabla bla in Blablas) { arrayList.Add(Convert.ToInt32(blabla.something)); } so far this works great.. but now
2
by: Sunny | last post by:
Hi, I have a small function, that returns a string, inside the function, there is an arraylist, being processed. At the end of the function all the elements of the ArrayList are copied to my...
4
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties...
1
by: Jack Addington | last post by:
I have a method that takes an array of nodes (UltraNode) as a parameter. I've got an arrayList of objects that I am trying to convert to UltraNodes and pass to the method but I cannot seem to get...
2
by: Dirk | last post by:
Hello The following code does not work ArrayList __gc* allControls = new ArrayList(); ..... // put some Controls in allControls .... Control __gc* arr = dynamic_cast<Control...
2
by: Russ Green | last post by:
I am currently working on a small VB.NET utility that accesses the Microstation VBA object model via COM. I have this code which I am trying to get to work... The key bits of information are ...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
3
by: wolima | last post by:
I have to create a arraylist of arrays. So, at the end, I have to convert this arraylist in a two dimmensional array. I wrote something like this. ArrayList myAL = new ArrayList(); double...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.