Solved

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

Posted on 2004-04-09
7
248 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now