469,636 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Q: Casting objects from an ArrayList

Hi!

I am going crayz, i cant get this to work. and i don't know what the problem
is.

I have this method

public ArrayList ResolveData()
{
ArrayList workingList = new ArrayList();
Loop and stuff
// do stuff

workingList.Add(resolvedString.Split(new char[]{';', '--'});
loop ends here

return workingList
}

Then i have this method

private void MyPrivate()
{
ArrayList myArray = ResolveData();

foreach (ArrayList obj in myArray)
{
string[] resultSet = obj as string[]; <<<< This is where i get the
compile error.
}
}

What am i doing wrong?

Regards
Martin
Apr 18 '07 #1
3 3397
On Apr 18, 1:01 pm, "Martin Arvidsson, Visual Systems AB"
<martin.arvids...@vsab.netwrote:
I am going crayz, i cant get this to work. and i don't know what the problem
is.
<snip>
What am i doing wrong?
This line:

foreach (ArrayList obj in myArray)

should just be:

foreach (string[] resultSet in myArray)

The way you've got it at the moment is expecting the ArrayList to be
full of other ArrayLists. There's no way of casting an ArrayList to a
string[], which is why you're getting a compile-time error.

Jon

Apr 18 '07 #2
On 18 Apr, 13:01, "Martin Arvidsson, Visual Systems AB"
<martin.arvids...@vsab.netwrote:
Hi!

I am going crayz, i cant get this to work. and i don't know what the problem
is.

I have this method

public ArrayList ResolveData()
{
ArrayList workingList = new ArrayList();

Loop and stuff
// do stuff

workingList.Add(resolvedString.Split(new char[]{';', '--'});
loop ends here

return workingList

}

Then i have this method

private void MyPrivate()
{
ArrayList myArray = ResolveData();

foreach (ArrayList obj in myArray)
{
string[] resultSet = obj as string[]; <<<< This is where i get the
compile error.
}

}

What am i doing wrong?

Regards
Martin
Well you've got ArrayList in your foreach instead of string[]. Try
this:

System.Collections.ArrayList a = new System.Collections.ArrayList();
a.Add("this is a test".Split(' '));
a.Add("this is a test".Split(' '));
string[] s = (string[])a[0]; // <-- example normal cast
foreach(string[] ss in a) // <--- string array
{
Console.WriteLine(ss.Length);
}

Apr 18 '07 #3
Jon and DeveloperX have hopefully answered your original question; as
an additional - if you can, I strongly suggest moving to .Net 2.0 and
using generics, such as List<T>; because this is strongly typed, it
makes it absolutely clear what the problem is *at compile-time*,
rather than having to debug the issue at run-time.

Marc
Apr 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steven Bartley | last post: by
1 post views Thread by David | last post: by
2 posts views Thread by MattC | last post: by
4 posts views Thread by Valerie Hough | last post: by
3 posts views Thread by Andy Chen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.