2

I'm trying to paste an array of custom class instances into a 2d array of them in a specific position with this code:

arr.Array.SetValue(stripe, topleft.X, topleft.Y);

…and it gives me a System.InvalidCastException with the message Object cannot be stored in an array of this type.

arr.Array is MyClass[,], and stripe is MyClass[].

What am I doing wrong here?

This line of code is a part of a larger method that loads a rectangular piece of map for a 2d platformer. The goal is to load separate stripes of tiles into a 2d array so that they form a rectangle of certain dimensions within the 2d array of tiles of larger dimensions.

Of course, this can be done bit by bit, but isn't there some method that allows to do that?

12
  • 1
    You will need to show the object types, the array an the object you put into the array. Commented May 30, 2013 at 2:15
  • I think the array should be defined as MyClass[,][] instead. Commented May 30, 2013 at 2:20
  • 1
    A 2d array of 1d arrays? I'm making a game with a 2d tile map, why would I need that? Commented May 30, 2013 at 2:20
  • you are trying to insert an array into a multi-dimensional array. Commented May 30, 2013 at 2:21
  • Maybe I'm using a wrong method. What I want is to copy a horizontal sequence of elements into a 2d array beginning from topleft. Commented May 30, 2013 at 2:22

2 Answers 2

1

I propose you use a long 1d array instead of a 2d array. Here is an example:

static void Main(string[] args)
{
    int rows = 100, cols = 100;
    // array has rows in sequence
    // for example:
    //  | a11 a12 a13 |    
    //  | a21 a22 a23 | = [ a11,a12,a13,a21,a22,a23,a31,a32,a33]
    //  | a31 a32 a33 |    
    MyClass[] array=new MyClass[rows*cols];
    // fill it here

    MyClass[] stripe=new MyClass[20];
    // fill it here

    //insert stripe into row=30, column=10
    int i=30, j=10;
    Array.Copy(stripe, 0, array, i*cols+j, stripe.Length);

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

2 Comments

Maybe I will. I'll just need to create a class that will handle the old 2d array calls and transform them into 1d array calls internally. Then I'll be able to insert the stripes of tiles easily.
Just make sure you check the length of the stripe versus how many element remain until the next row.
0

System.InvalidCastException with the message Object cannot be stored in an array of this type.

You would have to mention the index of stripe array, from which you might have to copy the value.

    class MyClass
    {
         public string Name {get;set;}
    }

Usage:

   // Creates and initializes a one-dimensional array.
    MyClass[] stripe = new MyClass[5];

    // Sets the element at index 3.
    stripe.SetValue(new MyClass() { Name = "three" }, 3);


    // Creates and initializes a two-dimensional array.
    MyClass[,] arr = new MyClass[5, 5];

    // Sets the element at index 1,3.
    arr.SetValue(stripe[3], 1, 3);

    Console.WriteLine("[1,3]:   {0}", arr.GetValue(1, 3));

5 Comments

I guess this is not faster than doing double for loops and copying one by one?
@user1306322: About speed, why do you need a double for. You could loop only stripe.. Sorry if I am not in context..
You're right. I was thinking about pasting a list of all stripes.
Each stripe index should have a definite location in the arry right? which is not arranged in order/sequential?
map is in a binary file, tiles are saved row after row and the stripes are horizontal lines of specified length. They form a rectangle smaller than the map.

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.