tags:

views:

1249

answers:

2

hi, In C#, i have created ArrayList using structures. so it creates multi dimensional array list.

public struct ParameterValues { public ArrayList al; };

ArrayList alCombined = new ArrayList();

for(int i=0; i < CONDITION , i++) alCombined.Add(obj.pValue.al);

The dimension of ArrayList alCombined depends on the CONDITION. if its 1, then 1-D arraylist is created. Else multidimensional Arraylist is getting created.

Now in order to access the elements of alCombined, i'm typecasting it and accessing. like, (((ArrayList)al[i])[j])

But if its a 1-D arraylist then error occurs as type casting to Arraylist is not possible.

So I need a solution for this, or how to find if its a single/ multi dimensional arraylist. FYI: it should not depend on CONDITION variable. like if d condition is more than one, then it will be multi dimensional for sure.

Thanks in advance.

+1  A: 

How about

if(al[i] is ArrayList)
{
   ...
}
Gerrie Schenck
+5  A: 

There's really no such thing as a "multi-dimensional ArrayList". ArrayLists don't have a particular element type. You might have one element which is an integer, another which is a string, another which is an int[] and another which is an ArrayList itself.

A few suggestions on your code:

  • Mutable structs are a bad idea. Avoid them. Chances are you don't really want a struct in the first place, and mutable structs can cause all kinds of unexpected behaviour.
  • Public fields are a bad idea. Avoid them. Fields are an implementation detail, and shouldn't be part of the API.
  • ArrayList is effectively deprecated - use generic collections instead. That will make the type information much more clearer.
  • You should design your data structure so that you don't get into this sort of situation. Ideally, there should be very few execution-time type checks going on in your code. Perhaps if you could give us more information on what you're trying to achieve, we could help you redesign it.
Jon Skeet