tags:

views:

381

answers:

6

Hi All,

I have an array being returned but as I'm new to programming I can't work out how to read each value. Can someone please point me in the right direction?

My code is below.

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
 foreach (int name in getNames()) //<----I'm wrong here I guess
 {
    MessageBox.Show(name.ToString());
 }
}

private ArrayList getNames()
{
 //some code...
 ..
 ...
 return names;
}

Thanks all

A: 

Rather than using ArrayList, try using List<int>.

I'm guessing that in the foreach statement, you're hitting a cast exception. It's hard to tell this because you've left out the body of getNames().

Drew Noakes
+2  A: 

Firstly, you're not returning an array, but rather an ArrayList instance. Secondly, why would a name be an int? Surely you mean string? Finally, if you're on .NET 2.0 or later, you can use generics to improve your code. So, use List<string> rather than ArrayList.

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
    foreach (string name in getNames())
    {
        MessageBox.Show(name);
    }
}

private List<string> GetNames()
{
    var names = new List<string>();
    names.Add("Kent");
    return names;
}

HTH, Kent

Kent Boogaart
I couldn't see the int for looking, it should have been string. Thanks
Scott
Even better if the GetNames() function signature returns IEnumerable<string>
Joel Coehoorn
+2  A: 

The issue here is that getNames returns an arraylist of objects--not ints.

You could rewrite your code with this change:

 foreach (object nameobj in getNames())
 {
    string name = (string)nameobj;
    MessageBox.Show(name);
 }

Alternatively, you could (and should) use generics to keep everything nice and tidy. That might look like this (untested) code:

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
 foreach (string name in getNames()) 
 {
    MessageBox.Show(name);
 }
}

private IList<string> getNames()
{
 //some code...
 List<string> names = new List<string>();
 names.Add("Scott");
 ..
 ...
 return names;
}

Add a using for System.Collections.Generic. Also, as others noticed, I changed it to string from int.

Michael Haren
The foreach will attempt the cast and throw if it fails.
Kent Boogaart
A: 

Hi Scott! Try return a list of int instead. Like this:

private List<int> getNames()
{
    //some code...
     ..
     ...
     return names;
}

Either that, or check that it's really int's in your list, so you've not done anything like list.add("5"). Then the 5 would be a string, not an int.

Marcus L
A: 

This may get you started:

String key, val;

foreach (DictionaryEntry de in myArrayList)
{
     key = (String)de.Key;
     val = (MyObjectType)de.Value;
}

EDIT: Updated to remove the IEnumerator version to a foreach version.

Wayne
This was posted incase the parent was being abstract in the implementation. Using GetEnumerator() is the correct way to work thru these objects like ArrayLists, HashTables etc...
Wayne
even if you want to work with the enumerator you can (and should) still use foreach. It's simpler, more expressive, and more open to compiler optimisation than the while loop.Also, you didn't fix the "string instead of int" bug
Phil Nash
@Phil - I understand about the function being called getNames() but that does not mean we should assume that it is supposed to return a string. Thats why I answered with the IEnumerable implementation. As long as the parent can learn, thats why we are all here :)
Wayne
A: 

ArrayList stores items as objects. So you need to unbox it to the required type (int in your case). But why int and not string for name?

Mahatma