473,804 Members | 3,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3546
On Apr 18, 1:01 pm, "Martin Arvidsson, Visual Systems AB"
<martin.arvids. ..@vsab.netwrot e:
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.netwrot e:
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.Collecti ons.ArrayList a = new System.Collecti ons.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.WriteLi ne(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
503
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 question is: object x = 15; decimal d = 0;
1
223
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 objects from that collection wich are dirived from some inteface... how can i do that..?
2
5481
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 object type and collection type I have a number of classes that derive from ArrayList and have if pick out on ly those i asked for.
4
343
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 the same name) for retrieving my objects WITHOUT having to have the caller cast them, i.e. public OBJECT_TYPE1 GetAt( int arrayIndex ) { return( (OBJECT_TYPE1) arrayList ); }
16
6434
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 sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of objects). So I need a way to sort the ArrayList, based on the data - that is within the objects that...
1
2191
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
1231
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 ArrayList(); al.Add("test"); ht.Add("key1", al);
5
3012
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 into a problem using C-style casting to an instance of a derived class from its interface type to a variable of its concrete type. I was confused because I thought C-style casts always compiled even if they wouldn't run correctly at runtime. I...
8
2516
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 { public static void main(String args)
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.