Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot apply indexing with [] to an expression of type `object'

Here is my code: An ArrayList of ArrayList that returns a float:

public ArrayList walls=new ArrayList(); 

public void Start()
{
    walls[0] = ReturnInArrayList(279,275,0,0,90);
    walls[1] = ReturnInArrayList(62,275,0,0,0);
    walls[2] = ReturnInArrayList(62,275,62,0,90);
    walls[3] = ReturnInArrayList(217,275,62,-62,0);
    walls[4] = ReturnInArrayList(62,275,279,0,90);
    walls[5] = ReturnInArrayList(41,275,279,0,0);
    walls[6] = ReturnInArrayList(279,275,320,0,9);
    walls[7] = ReturnInArrayList(320,275,0,-279,0); 

    for (int i = 0; i < walls.Length; i++) {
        float a = (float)walls[i][0];
        float b = (float)walls[i][1];
        float c = (float)walls[i][2];
        float d = (float)walls[i][3];
        float e = (float)walls[i][4];
    }
}

ArrayList ReturnInArrayList(float a,float b,float c, float d, float e)
{
    ArrayList arrayList = new ArrayList();
    arrayList.Add(a);
    arrayList.Add(b);
    arrayList.Add(c);
    arrayList.Add(d);
    arrayList.Add(e);
    return arrayList;
}

It gives me the following error:

error CS0021: Cannot apply indexing with [] to an expression of type `object'

I already did the casting, what is wrong? :(

like image 440
JamesB Avatar asked Dec 22 '12 16:12

JamesB


2 Answers

The problem is that paredes[i] returns an object which is the return type of the ArrayList indexer. You need to cast this to an ArrayList to be able to access it:

float a= (float)((ArrayList)paredes[i])[0];

A better solution though is to use generics and populate a List<float> instead:

List<float> RetornaEmList(float a,float b,float c, float d, float e)
{
    return new List<float> { a, b, c, d, e };
}

then paredes is a List<List<float>> and your accessor can be changed to:

float a = paredes[i][0];
like image 157
Lee Avatar answered Sep 16 '22 14:09

Lee


ArrayList stores objects without limiting what type those objects are. When you access the objects stored in an ArrayList, the compiler doesn't know what type they are, so it just gives you type object.

You're storing an ArrayList of float in your outer ArrayList. Since you're always storing floats, it would be better to use a List<float> for the inner list, and a List<List<float>> for the outer list. This way you won't have to type cast from object:

using System.Collections.Generic;

public List<List<float>> paredes = new List<List<float>>();   


Start() {
    paredes[0]=RetornaEmList(279,275,0,0,90);
    paredes[1]=RetornaEmList(62,275,0,0,0);
    paredes[2]=RetornaEmList(62,275,62,0,90);
    paredes[3]=RetornaEmList(217,275,62,-62,0);
    paredes[4]=RetornaEmList(62,275,279,0,90);
    paredes[5]=RetornaEmList(41,275,279,0,0);
    paredes[6]=RetornaEmList(279,275,320,0,9);
    paredes[7]=RetornaEmList(320,275,0,-279,0);    



    for (int i=0;i<paredes.Length;i++)
    {           
        float a=paredes[i][0];
        float b=paredes[i][1];
        float c=paredes[i][2];
        float d=paredes[i][3];
        float e=paredes[i][4];                   
    }
}

List<float> RetornaEmList(float a,float b,float c, float d, float e)
{
    return new List<float> { a, b, c, d, e };
}

Since the inner list always has 5 floats, you could also use a float[] instead of a List<float>

If you just want to make the code work without moving from ArrayList to List, you need an additional cast:

float a = (float)((ArrayList)paredes[i])[0];

But it's a lot cleaner just to use List<float> instead.

like image 44
Scott Chapman Avatar answered Sep 16 '22 14:09

Scott Chapman