Reputation: 453
I'm looking to nest dictionaries inside of one another in order to house x y coordinates of blocks. So I would have
IDictionary<IDictionary<int, int>, IDictionary<int, int>>
and the key Dictionary would house the column, row combination while the value Dictionary would house the x and y coordinates. It will be used to draw a 2D wireframe after users input values for the blocks on the plane.
I have a couple of questions: 1) What possible pitfalls might I run into? 2) Is there a better approach for this challenge?
Thank you
Update Not sure how to mark the answer since two different suggestions led to the used solution. I ended up with:
IDictionary<KeyValuePair<int, int>, Point>
and each block sets the x, y coordinates for the next block to its right and next block below.
This is a combination of answers by Mehrdad Afshari, Reed Copsey and CSharpAtl for the KeyValuePair and Cory Charlton for the Point.
If there is a way to mark more than one answer please let me know, but otherwise I'm just marking the first answer in the list as the answer.
Upvotes: 1
Views: 1518
Reputation: 422102
Are you trying to map a pair of integers to a dictionary? If this is the case, you should probably use something like:
Dictionary<Tuple<int, int>, Dictionary<int,int>>
What you are currently doing maps an IDictionary
object, which is a reference type, to another IDictionary
instance. It just checks the reference equality (identity) of the key and won't consider the contents of the key dictionary at all.
If you are trying to map a key consisting of a set of values to a dictionary (e.g. can be useful in memoization and dynamic programming), you need to declare your own comparer to compare the contents of the keys rather than solely considering their references.
Upvotes: 5
Reputation: 68707
Why not create a simple block class:
struct Block
{
public int X {get; set;}
public int Y {get; set;}
public int Column {get; set;}
public int Row {get; set;}
}
And a simple IEnumerable<Block> blocks
. This would make your data strongly typed and much more maintainable.
Upvotes: 4
Reputation: 7512
I made a comment, thought I would make an answer....:)
key should be a KeyValuePair<int, int>
instead of a Dictionary<int, int>
Upvotes: 2
Reputation: 8938
How will you be drawing the Wireframe? Using GDI+? If so why not store the values as Point()?
Upvotes: 1
Reputation: 564631
It seems like this makes more sense as:
Dictionary<KeyValuePair<int,int>, KeyValuePair<int,int>>
The main issue is that an XY coordinate pair (which is basically both the key and value) really shouldn't be a dictionary - there's a single, unique pair value.
That should work fine.
Upvotes: 4
Reputation: 19583
I don't think you want an IDictionary<int, int>
for the key, at least not the way you described it. From what you said, you just need an x,y pair for the key. Why not create an immutable structure for just that?
public struct OrderedPairKey
{
private readonly int _x;
private readonly int _y;
public OrderedPairKey(int x, int y)
{
_x = x;
_y = y;
}
public int X { get { return _x; } }
public int Y { get { return _y; } }
}
Immutable structures still have their place, and as a dictionary key is just about perfect for that.
EDIT: Oh, crap. Forgot about KeyValuePair<int, int>
. D'oh!
Upvotes: 0
Reputation: 19479
Just use a datatable. Sure you may duplicate some of the key information but its only ints, and will save you a good amount of headache in tracking the kvp's.
Upvotes: 0