tags:

views:

2289

answers:

6

I would like to create an object array in C# of undefined length and then populate the array in a loop like so...

    string[] splitWords = message.Split(new Char[] { ' ' });

    Word[] words = new Word[];
    int wordcount = 0;
    foreach (string word in splitWords)
    {
        if (word == "") continue;
        words[wordcount] = new Word(word);
        wordcount++;
    }

However, I get the error... "Array creation must have array size or array initializer"

I'm doing a lot more logic in the foreach loop that I've left out for brevity.

+10  A: 

What you want to do is create:

List<Word> words = new List<Word>();

and then:

words.Add(new Word(word));

And finally when the loop is done if you need an array:

words.ToArray();
jasonh
+3  A: 

You can't create an array of undefined length. This is where you'd use a generic List.

List<Word> words = new List<Word>();
statenjason
+5  A: 

If you're using C# 3.5, you can just do the following.

var words = message
  .Split(new char[]{' '}) 
  .Where(x => x != "")
  .Select(x => new Word(x))
  .ToArray();
JaredPar
If there's not a LINQ solution to your problem, you're not trying hard enough :) Nice one.
Jonathan
Good stuff. A note, using StringSplitOptions.RemoveEmptyEntries as your second Split() parameter would eliminate the Where() call. Although it's longer, it saves another loop through the string array.
statenjason
A: 

I solved it by using an ArrayList and then casting it to the object array after iterating...

    string[] splitWords = message.Split(new Char[] {' '});
    ArrayList wordList = new ArrayList();
    int wordcount = 0;
    foreach (string word in splitWords)
{
        if (word == "") continue;
        Word newWord = new Word(word);
        wordList.Add(newWord);
        wordcount++;
}
    Word[] words = (Word[])wordList.ToArray(typeof(Word));

I've heard the whole "create question/answer just to document it for others" is acceptable. Plus I'd like to hear if there are better suggestions. Thanks.

Lyndal
The other answers (ie. List<Word>) is generlaly considered better, as the ToArray() is less code, and it's type-safe (you can't accidentally put non-Words in it.
Jonathan
Great answer of using a generic List... not sure why I didn't think of that first. Thanks! Also, I guess putting my first guess as an "answer" was not such a good idea considering the down votes and ding to my rep! ha
Lyndal
A: 

Actually you may use list to populate your words first and then convert it easily to array like this:

string[] splitWords = message.Split(new Char[] { ' ' });

List<Word> words = new List<Word>();
int wordcount = 0;
foreach (string word in splitWords)
{
    if (word == "") continue;
    words.add(new Word(word));
    //wordcount++;
}

wordcount = words.count;
return words.ToArray();
David.Chu.ca
+1  A: 

A friendly note, you can pass option to split to ignore empty entries. Assuming no other logic to prune out entries you can preinitialize your array like so:

string[] splitWords = message.Split(new Char[] {' '},
  StringSplitOptions.RemoveEmptyEntries);
Word[] words = new Word[splitWords.Length];
Talljoe
Thanks, I did this based on another comment to an answer. Thanks, I had not yet discovered this option.
Lyndal