0

I have a model class called UnicodeMap that I use to represent the hexadecimal unicode value and its consonants and values for Amharic characters.

public sealed class UnicodeMap
    {

        public string Letter;

        public string Consonant;

        public string Vowel;


    }

I have stored the mapping in CSV and read in runtime to populate a dictionary.

Here is the excerpt of the code that reads and populates the dictionary:

private Dictionary<string, UnicodeMap> _unicodeMap;

void ReadMap()
{
            var engine = new FileHelperAsyncEngine<UnicodeMap>();

            using (engine.BeginReadFile(MapFile))
            {
                foreach (var record in engine)
                {

                      _unicodeMap.Add(record.Letter, record);
                }
            }
 }

When I try to get a value from the dictionary using TryGetValue, it always returns false:

 public void Translate(string text)
    {
            var words = text.Split(' ', '.');
            foreach (var word in words)
            {
                var chr = Char.Parse(word);
                var unicodeStr = @"\u" + ((int) chr).ToString("X4");
                UnicodeMap map;
                if (_unicodeMap.TryGetValue(unicodeStr, out map)) //returns false
                {
                    _animator.SetTrigger(map.Consonant);
                    _animator.SetTrigger(map.Vowel);
                };
            }
    }

For example, when the string is "ሀ", the value of the unicodeStr variable is "\u1200". And I have a string with the value "\u1200" as a key in the dictionary. However, TryGetValue cannot seem to find the key in the dictionary.

How can I solve this issue? What is the possible cause?

Edit: Added a gif of the debugger looking at the values of _unicodeMap dictionary

Debugger inspecting

xabush
  • 849
  • 1
  • 13
  • 29
  • Can you take a screenshot of the debugger looking at the Keys collection of `_unicodeMap`, specifically the value you are trying to get – Scott Chamberlain May 19 '17 at 14:27
  • @ScottChamberlain I have edited to question to include the screenshot – xabush May 19 '17 at 15:42
  • In your example, if you type in Watch window string.Equals(unicodeStr, _unicodeMap.Keys.First()) does it show 'true'? – Max Venediktov May 19 '17 at 16:01
  • No, it returns false. I think now the problem lies in the string comparison. Also, I don't know if it is relevant but I am developing a Unity game hence the .Net version is 3.5 or less. – xabush May 19 '17 at 16:29
  • Without a good [mcve] that reliably reproduces the problem, it won't be possible to answer your question. However, most likely you have passed some implementation of `IEqualityComparer` when you created your dictionary, and that implementation is broken. Please fix your post if you want an actual answer (and if not, just delete it). – Peter Duniho May 19 '17 at 16:36
  • You can't build a unicode character with @"\u" + hex, the `\uxxxx` syntax is compiler magic, not runtime magic. So if the dictionary contains unicode character keys, which are *shown* as `"\\u1200"`, then it won't compare equal to the *string* `"\\u1200"` – Lasse V. Karlsen May 19 '17 at 17:03
  • If they are not equal, you can still add watch of each string (unicodeStr and fist key), expand them and look at char array. This will point on the problem: are strings different, or you have comparison problems – Max Venediktov May 19 '17 at 17:40
  • @LasseV.Karlsen Unicode character keys would be shown as `"\u1200"` not as `"\\u1200"`. – Scott Chamberlain May 19 '17 at 18:01

1 Answers1

2

It's a little hard to see in the gif, but it appears you have a trailing space after the text in the dictionary keys, so you are actually comparing "\u1200" and "\u1200 ".

Fix the way you populate the dictionary by doing a .Trim() on the key.

private Dictionary<string, UnicodeMap> _unicodeMap;

void ReadMap()
{
        var engine = new FileHelperAsyncEngine<UnicodeMap>();

        using (engine.BeginReadFile(MapFile))
        {
            foreach (var record in engine)
            {

                  _unicodeMap.Add(record.Letter.Trim(), record); //Added .Trim()
            }
        }
}

Or fix FileHelperAsyncEngine to not leave the trailing space in the Letter property.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431