Search code examples
c#dictionaryargumentexception

List or Dictionary or something else?



I want to save some coordinates in a dictionary, BUT the xPos should be twice or more in the dictionary.

The problem is that the following exception appears:
ArgumentException: An element with the same key already exists in the dictionary.

How can I solve the problem ?

I allready thought that I can use a List or an Array, but I want a Key and a Value.

After I saved the coordinates in a Dict (or something else) I want to check whether a new coordinate is a certain distance of the existing ones.

The xPos is allways the same:
There is a "chart" where I place some blocks in a row with different yPos.
1. Block: xPos = 0, yPos = random
2. Block: xPos = 1, yPos = random
...
n. Block: xPos = 80, yPos = random
n+1. Block: xPos = 0, yPos = 20 + random

I have three iterations, for each 80 Blocks are placed.

enter image description here

SORRY for my bad english :|
I hope you could understand.


Solution

  • Or you can use List of Tuple to store list of int-int pairs without creating new class and without worrying about duplicate values :

    .....
    List<Tuple<int, int>> blocks = new List<Tuple<int, int>>();
    blocks.Add(Tuple.Create(0, random));
    blocks.Add(Tuple.Create(1, random));
    .....