Access to array elements List <object []> in foreach loop

This is what I have:

List<object[]> markers = new List<object[]>();

object[] marker = new object[2];
marker[0] = new Vector2(1, 2);
marker[1] = new Vector4(1, 2, 3, 4);
markers.Add(marker);

foreach (var item in markers)
{
    int x = (Vector2)item[0].X; // Error: 'object' does not contain a definition
                                // for 'X' and no extension method 'X' accepting
                                // a first argument of type 'object' could be
                                // found (are you missing a using directive or
                                // an assembly reference?)
    // other stuff
}

Is there a way to make this work without losing code readability, or should I try using a class instead of an array of objects?

+5
source share
7 answers

I decided to go with Tuple. Thanks for the idea of โ€‹โ€‹a poster that later deleted its answer. Your deeds of valor will be remembered.

Edit: had to change Vector4to Color(alpha channel always 255).

Result:

// declaration
List<Tuple<Vector2, Color>> markers = new List<Tuple<Vector2, Color>>();

// adding items
Tuple<Vector2, Color> marker = new Tuple<Vector2, Color>(
    new Vector2(x, y), Color.FromNonPremultiplied(250, 200, 150, 100));       
markers.Add(marker);

// retrieving item values
foreach (var item in markers)
{
    int x = item.Item1.X;
    int y = item.Item1.Y;
    Color mycolor = item.Item2;
}

This works, but I still have not tested the effectiveness of the CPU, if it turns out to be slower than the other proposed solutions, I will think about their use.

+1
source

You need to do this (operator priority problem)

((Vector2)item[0]).X;

. ( ArrayList).

- :

ArrayList vectors = new ArrayList { new Vector2(22, 33), new Vector4(223, 43, 444, 33}};

.Net 2.0. .

+4

Vector2 Vector4 . object.

- :

public class MyVector2 : IVector {

    //...
}

public class MyVector4 : IVector {

    //...
}

public interface IVector {


}

:

List<IVector> list = new List<IVector>();
IVector[] arr = new IVector[someSize];

Vector2 Vector4 , System.Windows.Vector2 System.Windows.Vector4, , :

public class MyVector2 : Vector2, IVector {

}

public class MyVector4 : Vector4, IVector {

}

.

+1

.NET 4 , Tuple - , :

List<Tuple<Vector2, Vector4>> markers = new List<Tuple<Vector2, Vector4>>();
+1

, . . , . , vector2 vector4. , , .

, . , , .

- :

interface IVector<T>
{
   List<T> Someobjectcollections
   void YourCommonOperation()

}

class Vector2 : IVector<Vector2>
{
 // override

}

class Vector4 : IVector<Vector4>
{
   // override
}

List<IVector> markers = new List<IVector>();

...


foreach (var item in markers)
{ 
     items.commonoperation();

      ....
}
+1

You must absolutely reorganize this to use classes. Basically, you need a list of markers, each of which can contain one or more vectors, which in turn contain one or more numbers. This is an easy way to achieve this without using inheritance:

public class Marker
{
    public List<Vector> { get; set; }
}

public class Vector
{
    public int[] Numbers { get; private set; }

    public Vector(params int[] numbers)
    {
        Numbers = numbers;
    }
}

Then you can just use List<Marker>.

+1
source

Here might be a good starting point:

public class Markers
{
    public Vector2 V2 { get; set; }
    public Vector4 V4 { get; set; }

    public Markers() {}
}
0
source

All Articles