Reputation: 21
Hello I have the following dictionary and can add and read from it but not change the value.
class GamesPlayersClass
{
public string nickname { get; set; }
public int status { get; set; } //0 not ready 1 ready 2 finished
public int dies { get; set; }
public int score { get; set; }
public int totaltime { get; set; }
}
static Dictionary<int, Dictionary<int, GamesPlayersClass>> gamesplayers = new Dictionary<int, Dictionary<int, GamesPlayersClass>>();
Adding an entry to the dictionary :
Dictionary<int, GamesPlayersClass> playerinfo = new Dictionary<int, GamesPlayersClass>();
playerinfo.Add(games.Count,
new GamesPlayersClass
{
nickname = nickName,
status = 0,
dies = 0,
score = 0,
totaltime = 0
});
gamesplayers.Add(games.Count, playerinfo);
Any idea how I could for example change the following value ? gamesplayers[0]dictionary2[0].Value.status = 1;
I hope I asked my question clear as Im new to Stackoverflow. Thanks for helping.
[EDIT] I know I can check if the key exist but not how to change it for key exist i try
if (gamesplayers[0].ContainsKey(0));
[EDIT2] Beside this any idea how I can quickly check if the second dictonary has a certain value
foreach (var game in gamesplayers )
{
foreach (var playerinfo in game)
{
if (playerinfo.Value.nickname == nickName)
{
}
}
Upvotes: 1
Views: 1425
Reputation: 6528
You can use this:
foreach (var outer in gamesplayers)
{
foreach (var inner in outer.Value)
{
if(inner.Key == "Something")
{
// do something
inner.Value = “test”;
}
}
}
Upvotes: 0
Reputation: 67
Struggling to understand why you need the second Dictionary, due to being able to just put the count against the game already. You also may want to look into using Lists of the class with a unique identifier on the class due to Dictionary's being inherently difficult to understand the deeper they get. However to fix your problem would recommend using linq as below
gameplayers.Where(gp => gp.Key == "insertExpectedIntHere" &&
gp.Any(p => p.Key == "insertExpectedIntHere" && p.nickname == "nicknameHere");
if (gameplayers != null)
{
Do the thing
}
else
{
Do the other thing
}
I believe this would work for you. But dictionary's are confusing so might require a bit of moving around (Again recommend trading out for unique identifiable list classes. Can provide example if needed)
Upvotes: 0
Reputation: 116
You can use LINQ
var gKey = 1;
var pKey = 1;
var game = (from g in gamesplayers where g.key == gKey).FirstOrDefault();
var playerinfo = (from p in game where p.key == pKey).FirstOrDefault();
playerinfo[pKey].Value.status = "status";
Upvotes: 0
Reputation: 81583
Assuming the keys exist, you can access a dictionary value by its indexer (Dictionary[key]
notation). Since you have a nested dictionary, you can call both the indexers
gamesplayers[0][0].status = 1;
Note 1 : this is not thread safe
Note 2 : you should probably use TryGetValue
and add the appropriate fault tolerance
Update
Your edited code is fine, just use Contains
, however you'll find TryGetValue
more convenient in most situations. If you wanted to get fancy you could write your own Extension Method
public static class Extensions
{
// checks if both keys exists and returns true or false
// returns a result if valid
public static bool TryGetValue<TKey1, TKey2, TValue>(this Dictionary<TKey1, Dictionary<TKey2, TValue>> dict, TKey1 key1, TKey2 key2, out TValue result)
{
if(dict == null)
throw new ArgumentNullException(nameof(dict));
result = default;
return dict.TryGetValue(key1, out var nestedDict) && nestedDict.TryGetValue(key2, out result);
}
}
Usage
If(gamesplayers.TryGetValue(key1, key2, out var result))
Debug.WriteLine($"yay we checked and returned a result {result.nickname}");
Note 3 : this is for academic purposes only, and may not be the best solution to what you actually want. However it does show you how to use TryGetValue
Upvotes: 2