nick gowdy
nick gowdy

Reputation: 6511

Assign value into Dictionary that has a key type of <Tuple<int,int> C#

I writing code to generate a grid that is supposed to be a graphical editor. The grid values are being contained in a dictionary. This is my method for generating my dictionary object to give you an idea of what I am working with.

public Dictionary<Tuple<int, int>, string> GenerateTable(int _x, int _y)
        {
            int total = _x * _y;
            var grid = new Dictionary<Tuple<int, int>, string>(); //!Might need this later!

            for (int i = 1; i <= _x; i++) // outer loop is column 
            {
                for (int ii = 1; ii <= _y; ii++) // Inner loop is row -
                {
                    grid.Add(Tuple.Create(i, ii), "O");
                }
            }
            return grid; // Should have same amount of elements as int total
        }

I have another method where I want to change one element in my dictionary, because I am using a key of Tuple I don't know what to provide in the index to change the value. This is the other method.

 public void ColorPixel(Dictionary<Tuple<int, int>, string> _table, int _x, int _y, string _c)
        {
            foreach(var pixel in _table
                .Where(k => k.Key.Item1 == _x && k.Key.Item2 == _y))
            {

            }


            //var tbl = _table.
            //    Where(t => t.Key.Item1 == _x && t.Key.Item2 == _y)
            //    .Select(t => t.Value == _c);

        }

Does anyone know how I change the element in the dictionary by accessing it's key of type Tuple ?

Upvotes: 3

Views: 3415

Answers (3)

fradique
fradique

Reputation: 154

I know this thread is old, but with new C#7 you can do something like:

var grid = new Dictionary<ValueTuple<int, int>, string>();

grid.Add((1,1),"value");

then you can read it using:

string value=grid[(1,1)];

Upvotes: 4

fourpastmidnight
fourpastmidnight

Reputation: 4234

UPDATE

Per Avner's comment, you shouldn't use Linq for this in this way. This is a classic mistake that's often caused by the relative easy of writing Linq statements but hurts performance greatly, especially for dictionaries.

var keyToFind = Tuple.Create(_x, _y);
string pixelValueToUpdate;
_table.TryGetValue(keyTofind, out pixelValueToUpdate);
if (pixelValueToUpdate != null)
    pixelValueToUpdate = "New Value";

Don't Use LINQ

var pixelValue = _table
    .Where(pixel => pixel.Key.Item1 == _x && pixel.Key.Item2 == _y)
    .FirstOrDefault(pixel => pixel.Value);
pixelValue = "NewValue;

That should do it. Since your Tuple<int, int> is a key into a dictionary, you can only have one pixel with a set of (x, y) coordinates, hence, there's no need to iterate over the entire table.

Upvotes: -1

cynic
cynic

Reputation: 5405

Tuple types are "structurally comparable". Which means that to access the value in dictionary keyed by one, you create a new instance of the tuple, and access the value any way you see fit (indexer, TryGetValue etc.).

var key = Tuple.Create(x, y);
var value = dictionary[key];

Upvotes: 5

Related Questions