AddNewKeyPersistent() public static method

Adds a new key to a dictionary and does not stop until a unique key is found
public static AddNewKeyPersistent ( LocalizedObject>.Dictionary thisDictionary, string desiredKey, LocalizedObject, newValue ) : string
thisDictionary LocalizedObject>.Dictionary
desiredKey string
newValue LocalizedObject,
return string
    /// <summary>
    /// Adds a new key to the dictionary
    /// </summary>
    private void AddNewKey()
    {
        LocalizedObject dummyObject = new LocalizedObject();

        dummyObject.ObjectType = LocalizedObjectType.STRING;
        dummyObject.TextValue  = "New Value";

        string addedKey = LocFileUtility.AddNewKeyPersistent(parsedRootValues, "New Key", dummyObject);

        LocalizedObject copyObject = new LocalizedObject();

        copyObject.ObjectType = LocalizedObjectType.STRING;
        copyObject.TextValue  = "New Value";
        changedRootKeys.Add(new SerializableStringPair(addedKey, addedKey));
        changedRootValues.Add(new SerializableLocalizationObjectPair(addedKey, copyObject));
    }
    void OnGUI()
    {
        if (EditorWindowUtility.ShowWindow())
        {
            undoManager.CheckUndo();
            GUILayout.Label("Root Values", EditorStyles.boldLabel);

            //Search field
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Search for Key:", GUILayout.Width(100));
            searchText = EditorGUILayout.TextField(searchText);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Key Type", GUILayout.Width(100));
            GUILayout.Label("Key");
            GUILayout.Label("Base Value/Comment");
            GUILayout.Label("Delete", EditorStyles.miniLabel, GUILayout.Width(50));
            EditorGUILayout.EndHorizontal();

            //Create the scroll view
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

            //Delete key information
            bool deleteKey     = false;
            int  indexToDelete = 0;

            //Check if the user searched for a value
            bool didSearch = false;
            if (searchText != "")
            {
                didSearch = true;
                GUILayout.Label("Search Results - \"" + searchText + "\":", EditorStyles.boldLabel);
            }

            for (int i = 0; i < changedRootKeys.Count; i++)
            {
                SerializableStringPair rootValue = changedRootKeys[i];
                if (didSearch)
                {
                    //If the name of the key doesn't contain the search value, then skip a value
                    if (!rootValue.originalValue.ToLower().Contains(searchText.ToLower()))
                    {
                        continue;
                    }
                }
                EditorGUILayout.BeginHorizontal();

                //Popup of all the different key values
                changedRootValues[i].changedValue.ObjectType = (LocalizedObjectType)EditorGUILayout.Popup((int)changedRootValues[i].changedValue.ObjectType,
                                                                                                          keyTypes, GUILayout.Width(100));

                rootValue.changedValue = EditorGUILayout.TextField(rootValue.changedValue);
                changedRootValues[i].changedValue.TextValue = EditorGUILayout.TextField(changedRootValues[i].changedValue.TextValue);
                if (GUILayout.Button("Delete", GUILayout.Width(50)))
                {
                    deleteKey     = true;
                    indexToDelete = i;
                }

                EditorGUILayout.EndHorizontal();
            }

            //End the scrollview
            EditorGUILayout.EndScrollView();

            if (GUILayout.Button("Add New Key"))
            {
                AddNewKey();
            }

            //Delete the key outside the foreach loop
            if (deleteKey)
            {
                DeleteKey(indexToDelete);
            }

            if (guiChanged)
            {
                GUILayout.Label("- You have unsaved changes", EditorStyles.miniLabel);
            }

            //If any changes to the gui is made
            if (GUI.changed)
            {
                guiChanged = true;
            }

            GUILayout.Label("Save Changes", EditorStyles.boldLabel);
            GUILayout.Label("Remember to always press save when you have changed values", EditorStyles.miniLabel);
            if (GUILayout.Button("Save Root Language File"))
            {
                Dictionary <string, string> changeNewRootKeys   = new Dictionary <string, string>();
                Dictionary <string, string> changeNewRootValues = new Dictionary <string, string>();

                for (int i = 0; i < changedRootKeys.Count; i++)
                {
                    SerializableStringPair             rootKey   = changedRootKeys[i];
                    SerializableLocalizationObjectPair rootValue = changedRootValues[i];
                    //Check for possible duplicates and rename them
                    string newKeyValue = LocFileUtility.AddNewKeyPersistent(changeNewRootKeys, rootKey.originalValue, rootValue.changedValue.GetFullKey(rootKey.changedValue));

                    //Check for possible duplicates and rename them(same as above)
                    LocFileUtility.AddNewKeyPersistent(changeNewRootValues, newKeyValue, rootValue.changedValue.TextValue);
                }

                //Add the full values before saving
                Dictionary <string, string> changeNewRootKeysToSave   = new Dictionary <string, string>();
                Dictionary <string, string> changeNewRootValuesToSave = new Dictionary <string, string>();

                foreach (KeyValuePair <string, string> rootKey in changeNewRootKeys)
                {
                    LocalizedObject thisLocalizedObject = parsedRootValues[rootKey.Key];
                    changeNewRootKeysToSave.Add(thisLocalizedObject.GetFullKey(rootKey.Key), rootKey.Value);
                    changeNewRootValuesToSave.Add(thisLocalizedObject.GetFullKey(rootKey.Key), changeNewRootValues[rootKey.Key]);
                }

                LocFileUtility.SaveRootLanguageFile(changeNewRootKeysToSave, changeNewRootValuesToSave);

                //Fire the root language changed event
                if (OnRootFileChanged != null)
                {
                    OnRootFileChanged();
                }

                //Reload the window(in case of duplicate keys)
                SetRootValues(LocFileUtility.LoadParsedLanguageFile(null));
                guiChanged = false;
            }

            undoManager.CheckDirty();
        }
    }