Solved

How can I assign this list of strings to an integer array?

Posted on 2004-04-09
7
251 Views
Last Modified: 2010-04-15
How can I assign this list of strings to an integer array?

I have a list of numbers that is of a string type.  I want to stuff those into an integer array.
At the end of the process I want to have an int array that is dynamic in size.

tagIDList = "1230,12312,12312";
int[] foodWeightLossSystemTags = tagIDList.Split(new char[] {','});
0
Comment
Question by:andw928
7 Comments
 

Expert Comment

by:BillyX
ID: 10793520
string[] dummy = tagIDList.Split(new char[] {','});
int[] foodWeightLossSystemTags = new int[dummy.Length];
for(i=0;i<dummy.Length;i++) {
  foodWeightLossSystemTags[i] = Int32.Parse(dummy[i]);
}
0
 
LVL 7

Author Comment

by:andw928
ID: 10793589
So I knew that when I do a loop I will be able to parse it out, but I though there is a trick without a loop.  Sorry I should have put that in my question.

I'm going to assume there is no trick and I will loop through.

Thanks.

Andrzej
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 24 total points
ID: 10794693


nah.. there is no trick.  You have to "brute force" it.

If you wanted to add all of those elements to an array list, there is a trick for that....  Since all arrays inherit from ICollectionBase you can simply instantiate your arraylist with an array as a prameter


string[] myStrs = {"1","2","3","4"};
ArrayList al = new ArrayList(myStrs);

but then if you want the integer value you'll still need to parse the object

int i = int.Parse(al[2]);




0
 
LVL 12

Assisted Solution

by:monosodiumg
monosodiumg earned 23 total points
ID: 10795553
No 'tricks' on C# 1.1 but in C# 2.0 you get generics whic will allow you do this this somewhat more elegantly than ye olde for loop.
0
 
LVL 9

Assisted Solution

by:msdixon
msdixon earned 23 total points
ID: 10806778
you can simplify this line from:
string[] dummy = tagIDList.Split(new char[] {','});

to:

string[] dummy = tagIDList.Split(',');

you don't need to create an array
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question