473,402 Members | 2,072 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,402 software developers and data experts.

ArrayList to Array

I'm baffled by something. I want to turn an ArrayList (which stores
strings) into an array of strings. I know I can use the ToArray method
and pass in a type. But I can't get the code right. Any help here? Am I
supposed to pass in a Type?

ArrayList myarr;
myarr.Add("foo");
myarr.Add("goo");
string[] mystrings= myarr.ToArray(??);

Mathew
Nov 16 '05 #1
6 3152
string[] strs = myArrayList.ToArray(typeof(string));

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<10*************@corp.supernews.com>

I'm baffled by something. I want to turn an ArrayList (which stores
strings) into an array of strings. I know I can use the ToArray method
and pass in a type. But I can't get the code right. Any help here? Am I
supposed to pass in a Type?

ArrayList myarr;
myarr.Add("foo");
myarr.Add("goo");
string[] mystrings= myarr.ToArray(??);

Mathew

Nov 16 '05 #2
string[] strs = myArrayList.ToArray(typeof(string));

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<10*************@corp.supernews.com>

I'm baffled by something. I want to turn an ArrayList (which stores
strings) into an array of strings. I know I can use the ToArray method
and pass in a type. But I can't get the code right. Any help here? Am I
supposed to pass in a Type?

ArrayList myarr;
myarr.Add("foo");
myarr.Add("goo");
string[] mystrings= myarr.ToArray(??);

Mathew

Nov 16 '05 #3
duh. Nothing like learning a new language to make you feel stupid.
Thx Richard.

Mathew

Richard Blewett [DevelopMentor] wrote:
string[] strs = myArrayList.ToArray(typeof(string));

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<10*************@corp.supernews.com>

I'm baffled by something. I want to turn an ArrayList (which stores
strings) into an array of strings. I know I can use the ToArray method
and pass in a type. But I can't get the code right. Any help here? Am I
supposed to pass in a Type?

ArrayList myarr;
myarr.Add("foo");
myarr.Add("goo");
string[] mystrings= myarr.ToArray(??);

Mathew

Nov 16 '05 #4
duh. Nothing like learning a new language to make you feel stupid.
Thx Richard.

Mathew

Richard Blewett [DevelopMentor] wrote:
string[] strs = myArrayList.ToArray(typeof(string));

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<10*************@corp.supernews.com>

I'm baffled by something. I want to turn an ArrayList (which stores
strings) into an array of strings. I know I can use the ToArray method
and pass in a type. But I can't get the code right. Any help here? Am I
supposed to pass in a Type?

ArrayList myarr;
myarr.Add("foo");
myarr.Add("goo");
string[] mystrings= myarr.ToArray(??);

Mathew

Nov 16 '05 #5
Actually, in my haste I missed one thing:

string[] strs = (string[])myArrayList.ToArray(typeof(string));

You need to cast the returned array

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<10*************@corp.supernews.com>

duh. Nothing like learning a new language to make you feel stupid.
Thx Richard.

Mathew

Nov 16 '05 #6
Actually, in my haste I missed one thing:

string[] strs = (string[])myArrayList.ToArray(typeof(string));

You need to cast the returned array

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<10*************@corp.supernews.com>

duh. Nothing like learning a new language to make you feel stupid.
Thx Richard.

Mathew

Nov 16 '05 #7

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

Similar topics

1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
7
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
6
by: rdi | last post by:
I have 3 classes: mailBox, mailFilter & mailFilterSet I then have an arraylist. Each element in the array is of type mailBox (variable name is mbox). One item of the mailbox class is an...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
5
by: Paul | last post by:
Off the cuff, does anyone know if arraylist is more efficeint at adding items to an array than redim preserve? Paul <begin loop> Dim c As Integer = SomeArray.GetUpperBound(0) + 1 ReDim...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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...

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.