473,385 Members | 1,927 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,385 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 3289
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.