Trouble accessing ArrayList with index

Posted on 2006-06-02
Last Modified: 2010-04-16
I have an ArrayList populated from a DataReader:

object[] values = new object[dr.FieldCount];

then I try to access it like this:


however I can't even get the thing to compile.  I get this error:
"Cannot apply indexing with [] to an expression of type 'object'"

I have tried casting it every way I could think of without success.  If comment out the part where I try to access it, and then run the program, and then put it in break mode right next to where I am trying to access it, I am able to get what I need in the command window using the exact same line: EndSegData[x][y].  So, it works in the command window but not in the program.

I could use some help getting it to work in my program.

Thank you.
Question by:ng_king
    LVL 48

    Accepted Solution

    Try this:

    LVL 96

    Expert Comment

    by:Bob Learned
    Here is a little test that I came up with:

          ArrayList test = new ArrayList();

          for (int i = 0; i < 10; i++)
            object[] values = new object[]{"1", 5, 10.1, "Bob", 'C'};

          int n = Convert.ToInt32(((object[])test[0])[1].ToString());


    Author Comment

    I didn't try it with the right paren around the first element-- I guess that's the trick.  Thank you very much.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now