Solved

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

Posted on 2004-04-09
7
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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