Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-09
7
Medium Priority
?
266 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 72 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 69 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 69 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

885 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