3

I have defined a new class type called Hotspot. I need 2 dynamic array of Hotspot (I used List) and a third one that allow me to "switch" between them. Here my code:

List<Hotspot> items = new List<Hotspot>();
List<Hotspot> locations = new List<Hotspot>();

Hotspot[][] arrays = new Hotspot[][]{items, locations};

but arrays doesn't work. I just need it so I can easily access to items/locations array.

In F# I did it in this way:

let mutable items = new ResizeArray<Hotspot>()
let mutable locations = new ResizeArray<Hotspot>()

let arrays = [|items; locations|]

but I can't do the same thing in C#. Some help?

1
  • 2
    It doesn't work because a List<Hotspot> is not the same as a Hotspot. In other words, you declare arrays to be a 2d array of Hotspots, yet you try filling it with two Lists. With static typing, that can't happen. Commented Jul 8, 2012 at 21:20

2 Answers 2

4
List<Hotspot>[] arrays = new List<Hotspot>[]{items, locations};
Sign up to request clarification or add additional context in comments.

Comments

3

items and locations are declared (and instantiated) as lists. Lists are not arrays and you're trying to assign them as arrays. Convert them to arrays or don't use arrays at all but a list instead.

Hotspot[][] arrays = new Hotspot[][]{ items.ToArray(), locations.ToArray() };
//or
List<Hotspot>[] lists = new[] { items, locations };

p.s., The F# ResizeArray<T> is essentially an alias to the .NET List<T>. So in effect, the arrays variable in your F# example is equivalent to lists in my example above, you created an array of lists.

2 Comments

The latter is much cleaner in my opinion.
I don't know why there was problems about the position of "arrays" declaration. Done within Form_Load() and it works. It makes no sense for me...

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.