Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

756 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