Why is my {get; private set} variable returning an empty Dictionary?

Hale Jan

I am trying to use a {get; private set;} for a MorseCode translator I am building right now. The MorseToText Dictionary is supposed to be built from a TextToMorse Dictionary I have already defined. For some reason my Dictionary is empty when I use it though.

    private static Dictionary<char, string> TextToMorse = new Dictionary<char, string>()
    {
        {'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
        {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
        {'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
        {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
    };

    private static Dictionary<string, char> _MorseToText = new Dictionary<string, char>();

    public static Dictionary<string, char> MorseToText
    {
        get { return _MorseToText; }

        private set
        {
            foreach (KeyValuePair<char, string> pair in TextToMorse)
            {
                _MorseToText.Add(pair.Value, pair.Key);
            }
        }
    }

...

        for (int i = 0; i < splitInput.Length; i++)
        {
            MorseToText.TryGetValue(splitInput[i], out char value);
            output += $"{value} ";
        }
404

One of the easiest ways to invert a Dictionary is using the ToDictionary linq function and then just swapping the Key and Value:

static Dictionary<char, string> TextToMorse = new Dictionary<char, string>()
{
    {'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
    {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
    {'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
    {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
};
static Dictionary<string, char> MorseToText = TextToMorse.ToDictionary(x => x.Value, x => x.Key);

Currently you are using the setter wrong. The set function will be called when you assign a value to MorseToText. As example it will run when you try to set MorseToText to any instance such as MorseToText = null overriding the default behaviour you would expect with the code from your setter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

my variable is returning empty why?

Why is my variable returning as 'undefined' despite being set a value?

Why is my variable empty?

Concurrent Dictionary with Get and Private Set - C#

Why is my call to WinGetTitle returning an empty string?

Why is my Entity Manager returning empty ResultLists?

Why is my JavaScriptSerializer returning empty json objects?

Why is my preorder traversal returning an empty list?

Why is my function returning an empty string?

Why is my object empty after constructing and returning it?

Why is my array is returning to the test empty?

Why is my recursive function returning an empty list?

Why my function is returning empty string in python?

Why is my function returning an empty array?

Why is GetMetricData returning an empty set of values?

MySQL Query returning "Empty Set", Why?

Get and Set String Property returning Empty Property

Why is this get_pages returning an empty array?

Why doesn't my boolean variable get set to True?

Why is Firestore's getDocument method returning an empty dictionary?

Why is my variable tabledata returning undefined?

Why is my variable returning undefined in javascript?

Why is my tableView variable returning nil?

Why is my Swift string variable returning nil?

Why is my function returning 1 and not the variable value?

Why is dictionary.get() returning values of all the dictionary entries?

nx.get_node_attributes in networkx returning an empty dictionary

How to set and get a private variable in javaFX?

Why is my string reference member variable set to an empty string in C++?