Hello,
i like to have a way to find out what key was not found and write it to a logfile when i get a KeyNotFoundException: The given key was not present in the dictionary.
I assume you’re doing something like:
string data = myDict[5];
Replace it with:
string data;
if (!myDict.TryGetValue(5, out data)) {
Debug.LogError("myDict doesn't have the value " + 5 + ", I can write that to a log file!");
}
else {
//do whatever you were planning to do
}
TryGetValue assigns returns true if the key exists. It also assigns to the out variable, so you can use it. Always use TryGet if there’s a possibility that the key doesn’t exist.
I understand that this would have to be placed everywhere where i want to access the dictionary?
I think it will be on some places right thankyou, but I still would like to have something where i dont need to change my code much.
my dictionary custom class looks like this:
[Serializable]
public class d1 : Dictionary<string, string> {
public d1() {
}
}
Sadly do i not know enough about custom classes to change it myself.
I found something:
//Extended dictionary
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
public TValue this[TKey key]
{
get
{
TValue val;
if(base.TryGetValue(key, out val))
{
return val;
}
else
{
throw new KeyNotFoundException(string.Format("The given key ({0}) was not present in the dictionary.", key));
}
}
set
{
base[key] = value;
}
}
}
public class Program
{
public static void Main(string[] args)
{
//Dictionary<string, string> dic = new Dictionary<string, string>()
//{
// {"a", "b"}
//};
MyDictionary<string, string> dic = new MyDictionary<string, string>()
{
{"a", "b"}
};
Console.WriteLine(dic["aa"]);
}
}
}
You shouldn’t change the behaviour. The way to do it with that approach would be to derive from Dictionary<TKey, TValue> like you’ve already done and hide the inherited indexer ( ) using the “new” keyword.
But I (personally) strongly recommend to not do it, as it changes the behaviour in an unexpected way. So if you were to reference your custom dictionary as a normal dictionary, the indexer would suddenly act differently.
The other way would be to wrap a dictionary and define your own behaviour.
But this might also confuse you in the future if you use both, normal dictionaries and your custom dictionary, as you always need to keep that implementation detail in mind.
This being said, you should follow the given advice (by @Baste ) and use the appropriate way to access a dictionary’s values.
Off-topic, but related:
The same applies to adding a value. There’s a tiny but important difference between Add() and using the indexer ( ). One (the indexer) allows to add & replace values for a given key, the other one (the Add() method) only allows to add and throws an exception if the specified key does already exist.
I tried it already my app still works .which has some 50-100 references to the custom dictionarys.
Well i am not sure about this how would it look like?
Essentially i need it for debug while i like to keep it if there is an issue i could remove it in the final product.
I will not rewrite some 100 lines to get a new debug feature. I could do that only at few parts of the app.
Have you tried attaching the debugger to unity (in VisualStudio or MonoDevelop, select ‘attach to unity’), put in some break points and run your project.
Furthermore, when unity prints an exception, it includes the stack trace. The stack trace will tell you the specific line of code it happened at, so you can put the break point at that specific line.
In the unity console, where the exception shows, click to highlight it expanding the full text of the exception. It’s look something like:
In this case you can see the error occured on the function ‘OnStartOrEnable’ at line ‘36’ of my script called ‘OverheadCamera’.
Lastly:
It’s not a new debug feature (it’s not a debug feature at all). It’s effective well formed code to use the TryGetValue method of a dictionary when you are accessing it with volatile keys that may or may not exist in the dictionary.
We as programmers often end up having to rewrite code sometimes because our early version is error prone… it’s just a fact of writing code.
Anyways, you want a new debug feature… it exists, I already described it. Use the built in debugger!