0

I have the following problem, When i am trying to access data from an Array inside another Array List, it dispalys that "cannot apply indexing[] with to an expression of type 'object'".

This is my code

public void getWaypoints() {
ArrayList potentialWPs = new ArrayList();
potentialWPs.Add(containerWaypoint.GetComponentInChildren(typeof(Transform)));
wayPoints = new ArrayList();

foreach (Transform potentialWP in potentialWPs){
    if(potentialWP.transform != containerWaypoint.transform){
        wayPoints[wayPoints.Count] = new ArrayList(2);
        wayPoints[wayPoints.Count][0] = potentialWP;                    
    }
}

The error are shown in the line "wayPoints[wayPoints.Count][0]".

Any one have any idea why this error is occurred?

3
  • 2
    wayPoints is 1-dimensional array not 2-dimensional array Commented Nov 27, 2012 at 8:36
  • 2
    ArrayList is deprecated - use List<T> (System.Collections.Generics) instead. Commented Nov 27, 2012 at 8:42
  • Why are you using ArrayList anyway? Commented Nov 27, 2012 at 8:51

5 Answers 5

1

Since ArrayList is a non-generic collection class, all items retrieved from it are objects, and need to be cast to their real types, like this:

 foreach (Transform potentialWP in potentialWPs){
    if(potentialWP.transform != containerWaypoint.transform){
        wayPoints[wayPoints.Count] = new ArrayList(2);
        ArrayList arr = wayPoints[wayPoint.Count] as ArrayList; <-- THIS
        arr[0] = potentialWP;
    }

Several important things to note:

1) This would have been simpelr if you had simply created the new array and held a reference to it (the arr variable I introduced) and then used that to add to the wayPoints and to assign to.

2) ArrayList really is an old and primitve class. Is there a reason you don't use a List<Transform> instead>?

3) You have a bug in your code, since you're access the ArayList in position Count. An ArrayList of length 2, if accessed at position 2, will crash - ArrayLists are 0-based, so you need to use Count - 1 to access the last position (1) on a length 2 array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for the answer, yeah it's a mistake that i don't use List<Transform> instead. i know that array based on 0, and i just access pointer 0 and 1 for each data in potentialWPs not more.
0

Try this wayPoints[0] = potentialWP; Since you have already declared an array list with size wayPoints.Count , you have to mention the index correctly.

Comments

0

An ArrayList only holds object types; that why you get

"cannot apply indexing[] with to an expression of type 'object'"

You need to cast

wayPoints

to your desired type

EDIT:

You should be using

List<T> (System.Collections.Generics)

Comments

0
        wayPoints[wayPoints.Count] = new ArrayList(2);
        wayPoints[wayPoints.Count][0] = potentialWP;

wayPoints[wayPoints.Count] returns an object. You need to cast it, before treating it like an ArrayList:

((ArrayList)wayPoints[wayPoints.Count])[0] = potentialWP;

However, you shouldn't use ArrayList, as it's deprecated. Use List instead.

Comments

0

The main problem you have is that by using ArrayList which is just a collection of objects, there is no implicit conversion to an array. As others have answered, one way is to cast the result to an array, after which you can access it by index.

A better way, might be to use a Generic List<T> which can be defined as a list of lists:

List<List<Transform>> waypoints = new List<List<Transform>>();

This would make your code a lot easier:

public void getWaypoints() {
    ArrayList potentialWPs = new ArrayList();
    potentialWPs.Add(containerWaypoint.GetComponentInChildren(typeof(Transform)));
    List<Transform[]> waypoints = new List<Transform[]>();

    foreach (Transform potentialWP in potentialWPs){
        if(potentialWP.transform != containerWaypoint.transform){
            wayPoints.Add( new List<Transform>>(){ potentialWP });                 
        }
    }
}

waypoints is now a "multi-dimensional" list of lists of Transform. You can access any element like so

List<Transform> first = waypoints[0];

or you could access a Transform directly

Transform firstOfFirst = waypoints[0][0];

or you could add another Transform to an existing list

waypoints[0].Add(anotherTransform);

2 Comments

thx, but i need to create multi dimentional array, 1st dimention for the waypoints itself which has 2 elements, 1st elements to store it's location, 2nd element to store each neighbour the waypoints have where this 2nd element is an array... may be i'll use struct instead....
@user1855858 - It kind of did already have a multi-dimensional array, but ive updated the answer to hopefully be a bit clearer on how you would go on to use waypoints

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.