473,327 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 3515
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steven Bartley | last post by:
Error Message on execution: An unhandled exception of type 'System.InvalidCastException' occurred in CastObjectToDecimal.exe Additional information: Specified cast is not valid. The code in...
1
by: David | last post by:
hello... i've a litlle problem and don't know how to handle this... i have collection (System.Collections.ArrayList) with number of diffrent objects within it... now i need to get all the...
2
by: MattC | last post by:
Hi, How can do runtime casting? MyCollection derives from ArrayList I will store lost of different objects that all derive from the same parent class. I then want to be able to pass in the...
4
by: Valerie Hough | last post by:
I have a class that contains an ArrayList object. All items in the arrayList are of the same type, but that type depends on how the class is initialized. I would like multiple public methods (with...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
1
by: Tedmond | last post by:
Dear all, How can I convert a list of items from ArrayList into an array of non system type objects? e.g. The following codes have error: public class myClass { int i;
3
by: Andy Chen | last post by:
Hi, I have a Hashtable, key is string and value is ArrayList. The problem is I cannot cast the value from object to ArrayList. like this: Hashtable ht = new Hashtable(); ArrayList al = new...
5
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran...
8
by: scottc | last post by:
i'm stuck and i need a little direction. i'm only getting 2 error messages. i have 2 files: import java.util.ArrayList; import java.util.*; //author public class StudentTester { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.