473,320 Members | 1,933 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,320 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 3151
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.