Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did it. I just had to slightly change my indexer. Here is the way to make this work:</p> <pre><code>public class TranslateDictionary&lt;TKey, TValue&gt; { private Dictionary&lt;TKey, TValue&gt; Dictionary = new Dictionary&lt;TKey,TValue&gt;(); public TValue this[TKey lang] { get { if (!Dictionary.ContainsKey(lang)) { Dictionary.Add(lang, Activator.CreateInstance&lt;TValue&gt;()); } return Dictionary[lang]; } set { Dictionary[lang] = value; } } } </code></pre> <p>Now when you have</p> <pre><code>TranslateDictionary&lt;string, TranslateDictionary&lt;TranslationKeys, string&gt;&gt; Dictionary { get; set; } </code></pre> <p>You just need to create an instance of the dictionary:</p> <pre><code>Dictionary = new TranslateDictionary&lt;string, TranslateDictionary&lt;TranslationKeys, string&gt;&gt;(); </code></pre> <p>And you are free to initialize it's elements like this:</p> <pre><code>Dictionary["PL"][TranslationKeys.SubTitle] = "SubTitle in Polish"; Dictionary["EN"][TranslationKeys.SubTitle] = "SubTitle in English"; Dictionary["DE"][TranslationKeys.SubTitle] = "SubTitle in German"; </code></pre> <p>Maybe this approach is slower than normal dictionary because of checking if the inner dictionary contains key every time when you try to read value, but at least you can assign elements to it in "cleaner" way.</p> <p><strong>UPDATE</strong> I've upgraded my TranslateDictionary a little. I've added boolean property "Initialized". Now after initialization i can set this value to "true" and there won't be no more checking if value exists in dictionary. And it will act then more like normal Dictioanry type.</p> <pre><code>public class TranslateDictionary&lt;TKey, TValue&gt; { public bool Initialized { get; set; } private Dictionary&lt;TKey, TValue&gt; Dictionary = new Dictionary&lt;TKey,TValue&gt;(); public TValue this[TKey lang] { get { if (!Initialized) { if (!Dictionary.ContainsKey(lang)) { Dictionary.Add(lang, Activator.CreateInstance&lt;TValue&gt;()); } } return Dictionary[lang]; } set { Dictionary[lang] = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload