1. ## Multidimensional SortedList (C#)

I am trying to create a 2-Dimensional SortedList in C#. I need to be able to look up a value using two keys, much like x,y coordinates.

See the table below. I need to be able to input two keys (A,C) and have the value '3' returned. The keys are an enumeration I have and the values will be integers. The standard C# SortedList only supports one key to one value. How can I best do this in C#?

Using this:
Code:
```public enum letters
{
A=1,B=2,C=3
}

A B C
A 1 2 3
B 2 3 1
C 3 1 2```  Reply With Quote

2. AxessTerminated,

coming from a c world I would think a two dimensional array like int array [][]; You can use this directly array[A][C] bur I am not sure if that works for c#.

Cheers,
Duncan  Reply With Quote

3. Hi

If you want to go with the typesafe generics, you may think about
an invertible mapping from your 2-d parameter space to a 1-d parameter
space. Usually, there are two approaches for this:

1. (A,B) -> "A_B"

The function maps the two letters (A,B) to a string "A_B".
Your SortedList thus is of type SortedList(of string,integer).

2. (A,B) -> 5

The function maps the two corresponding numbers to an integer
using the function x*3+y:

(A,A) = 4
(A,B) = 5
(A,C) = 6
(B,A) = 7

Your SortedList thus is of type SortedList(of integer,integer).

If performance is an issue go with the latter (which does in principle
the same as a 2-d array).

If performance really is an issue you may think about using a
SortedList/SortedDictionary versus an unsorted Dictionary: if you
read more often than write, a SortedList/SortedDictionary is a good
choice, otherwise use the Dictionary.

...Alternatively, you certainly could work with multidimensional arrays as
proposed by dspeidel.

/edit:
I forgot: What you could do if performance is not an issue but
nice design: create a class MyKey implementing IComparable
on your custom class (storing the two keys (A,B)) such that you could use
a SortedList/SortedDictionary(Of MyKey, Integer) Cheers  Reply With Quote

4. In Java it's just as follows. I don't know if there is a .length method/function in C# though!

Code:
`int[][] arrayName = new int;`
Then you could just tranverse the array with a nested loop to either look up a value or fill the array.

Code:
```(for int i=0; i < arrayName.length; i++)
{
for(int j = 0; j < arrayName[i].length; j++)
{
//do whatever
}
// do whatever
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•