Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - How to override GetHashCode with Lists in object
    primarykey
    data
    text
    <p>I am trying to create a "KeySet" to modify UIElement behaviour. The idea is to create a special function if, eg. the user clicks on an element while holding a. Or ctrl+a.</p> <p>My approach so far, first lets create a container for all possible modifiers. If I would simply allow a single key, it would be no problem. I could use a simple Dictionary, with</p> <pre><code>Dictionary&lt;Keys, Action&gt; _specialActionList </code></pre> <ul> <li>If the dictionary is empty, use the default action.</li> <li>If there are entries, check what action to use depending on current pressed keys</li> </ul> <p>And if I wasn't greedy, that would be it... Now of course, I want more. I want to allow multiple keys or modifiers. So I created a <strong>wrapper class, wich can be used as Key to my dictionary.</strong></p> <p>There is an obvious problem when using a more complex class. Currently two different instances would create two different key, and thereby he would never find my function (see code to understand, really obvious)</p> <p>Now I checked this post: <a href="https://stackoverflow.com/questions/638761/c-gethashcode-override-of-object-containing-generic-array">GetHashCode override of object containing generic array</a> which helped a little.</p> <p><strong>But my question is, is my basic design for the class ok. Should I use a hashset to store the modifier and normal keyboardkeys (instead of Lists). And If so, how would the GetHashCode function look like?</strong></p> <p>I know, its a lot of code to write (boring hash functions), some tips would be sufficient to get me started. Will post tryouts here...</p> <p>And here comes the code so far, the Test obviously fails...</p> <pre><code> public class KeyModifierSet { private readonly List&lt;Key&gt; _keys = new List&lt;Key&gt;(); private readonly List&lt;ModifierKeys&gt; _modifierKeys = new List&lt;ModifierKeys&gt;(); private static readonly Dictionary&lt;KeyModifierSet, Action&gt; _testDict = new Dictionary&lt;KeyModifierSet, Action&gt;(); public static void Test() { _testDict.Add(new KeyModifierSet(Key.A), () =&gt; Debug.WriteLine("nothing")); if (!_testDict.ContainsKey(new KeyModifierSet(Key.A))) throw new Exception("Not done yet, help :-)"); } public KeyModifierSet(IEnumerable&lt;Key&gt; keys, IEnumerable&lt;ModifierKeys&gt; modifierKeys) { foreach (var key in keys) _keys.Add(key); foreach (var key in modifierKeys) _modifierKeys.Add(key); } public KeyModifierSet(Key key, ModifierKeys modifierKey) { _keys.Add(key); _modifierKeys.Add(modifierKey); } public KeyModifierSet(Key key) { _keys.Add(key); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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