473,396 Members | 1,935 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,396 software developers and data experts.

Using Arraylist with arrays

I am trying to uae ArrayList to store arrays of structures, but I can't get my
data back after storing the arrays.
My (highly shrunk and modified) code is below. The problem is at the line
labled Line 1. I get an exception thrown saying:
"An unhandled exception of type 'System.InvalidCastException' occurred in
SLAP.exe
Additional information: Specified cast is not valid."

I have a watch at line 1 for al, and before the line is run, al is correctly
populated and shows the list of arrays of type A.

What is the problem, and how do you fix it. Any help would be most helpful.
By the way, I am new to VisualC++.NET, and I am trying to learn it by redoing
program that I have written in VC++.

Chuck Gantz
First, I have a structure

__gc struct A
{
public:
double a, b;
A(double ain, double bin)
: a(ain), b(bin){};
};

Then I have my main class that has an ArrayList
__gc class B
{
private:
//Only allow one copy of al to exist
static ArrayList* al;
public:
B()
{
al= new ArrayList();
ReadData();
A* llp = __try_cast<A*>(al->Item[2]); //Line 1- gives Exception
}

void ReadData()
{
Jul 19 '05 #1
2 3291
Hi Chuck,

Since each item in your ArrayList is itself an array, when you grab a
single item out of the ArrayList, you need to cast it to an array type.
The following worked for me:

A* llp[] = __try_cast<A*[]>(al->Item[2]);

Also, thanks for reducing the code down to the simplest repro case ... that
made it easy to find the problem. I don't know how "real" the code is that
you posted, but I did find a couple other errors that I wanted to bring to
your attention:

1.) Your "count" variable needs to be initialized to zero just before the
"do...while" loop, otherwise "count" keeps getting bigger and bigger and
you walk off the end of the "ar" array.

2.) The "do...while" loop has an off-by-one error. The "while" condition
should be "while (count < 5)" in order to correctly fill up all five
elements of the "ar" array.

Hope this helps.

--Rajeev
Visual Studio .NET
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '05 #2

Hi Rajeev,

Thanks for the fast reply. I put in the square brackets and everything
worked fine then.

Also, in the actual code, the errors you found are not present. The
program reads a file, and "count" is found in the file to determine the
size of the arrays.

Chuck Gantz
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vaden95 | last post by:
I am trying to uae ArrayList to store arrays of structures, but I can't get my data back after storing the arrays. My (highly shrunk and modified) code is below. The problem is at the line labled...
5
by: Kevin | last post by:
Hi All Can someone please tell me the difference between an Array and an ArrayList, and where I would be likely to use one over the other Thanks alot for any help
4
by: Ricardo | last post by:
how can i acess the members of an array that i put into a arraylist, like this: int dez = new int; ArrayList arList = new ArrayList(); arList.add(dez); I want to get the dez value.
5
by: Howard Kaikow | last post by:
How does one determine the number of lines before using ReadLine. This would enable the ReDim to be out of the loop. sReader = New StringReader(xxx.GetObjectText_) If Not (sReader Is Nothing)...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
3
by: Zenon | last post by:
I have a function which returns array of structs. I need to create a collection of those arrays and thought that an ArrayList would be a good way to do this since the count is variable. The...
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
1
by: cowboyrocks2009 | last post by:
Hi. I want to return values from multiple Arraylist. How can I do that ? later I want to use these values in another class. Can somebody help class myClass{ public ArrayList<Rectangle>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.