Using Arraylist with arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vaden95

    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.Invalid CastException' 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()
    {
  • Rajeev Goel

    #2
    RE: Using Arraylist with arrays

    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.

    Comment

    • Chuck Gantz

      #3
      RE: Using Arraylist with arrays


      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!

      Comment

      Working...