{"record":{"id":"d73b8fcc61e73c85","repo":"NickeManarin/ScreenToGif","slug":"index-out-of-range-while-trying-to-save-the-resour","errorCode":null,"errorMessage":"Index out of range while trying to save the resource dictionary.","messagePattern":"Index out of range while trying to save the resource dictionary\\.","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"warning","filePath":"ScreenToGif.Util/LocalizationHelper.cs","lineNumber":453,"sourceCode":"\n            //Insert at the new position.\n            Application.Current.Resources.MergedDictionaries.Insert(newIndex, dictionaryAux);\n\n            return true;\n        }\n        catch (Exception ex)\n        {\n            LogWriter.Log(ex, \"Move Resource\", selectedIndex);\n            return false;\n        }\n    }\n\n    public static void SaveSelected(int selectedIndex, string path)\n    {\n        try\n        {\n            if (selectedIndex < 0 || selectedIndex > Application.Current.Resources.MergedDictionaries.Count - 1)\n                throw new IndexOutOfRangeException(\"Index out of range while trying to save the resource dictionary.\");\n\n            var settings = new XmlWriterSettings { Indent = true };\n\n            using var writer = XmlWriter.Create(path, settings);\n            XamlWriter.Save(Application.Current.Resources.MergedDictionaries[selectedIndex], writer);\n        }\n        catch (Exception ex)\n        {\n            LogWriter.Log(ex, \"Save Resource\", selectedIndex);\n            //Rethrowing, because it's more useful to catch later\n            throw;\n        }\n    }\n\n    public static bool Remove(int selectedIndex)\n    {\n        try\n        {","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif.Util/LocalizationHelper.cs#L435-L471","documentation":"Thrown by LocalizationHelper.SaveSelected when the selectedIndex parameter is negative or exceeds the last index of Application.Current.Resources.MergedDictionaries. The method saves a specific merged dictionary to a file via XamlWriter.Save.","triggerScenarios":"SaveSelected is called with selectedIndex < 0 or selectedIndex > MergedDictionaries.Count - 1. This happens when the UI list selection is out of sync with the actual merged dictionaries collection — for example, after dictionaries were removed but the selection index wasn't updated.","commonSituations":"User removes a localization dictionary and then immediately tries to save the previously selected index, which no longer exists. Race condition where the MergedDictionaries collection is modified between the UI binding update and the SaveSelected call. selectedIndex was set to -1 (no selection) and not validated by the caller.","solutions":["Validate selectedIndex against the current MergedDictionaries.Count before calling SaveSelected.","After removing or reordering dictionaries, reset the UI selection to a valid index.","Use data binding with proper SelectedIndex handling so the UI cannot reference a stale index.","Clamp selectedIndex to valid bounds or disable the save button when no valid selection exists."],"exampleFix":"// before\nif (selectedIndex < 0 || selectedIndex > Application.Current.Resources.MergedDictionaries.Count - 1)\n    throw new IndexOutOfRangeException(\"Index out of range while trying to save the resource dictionary.\");\n\n// after: clamp and return false instead of throwing\nvar count = Application.Current.Resources.MergedDictionaries.Count;\nif (selectedIndex < 0 || selectedIndex >= count)\n    return false; // caller treats as 'nothing to save'","handlingStrategy":"validation","validationCode":"var count = Application.Current.Resources.MergedDictionaries.Count;\nif (selectedIndex < 0 || selectedIndex >= count)\n{\n    // Don't call SaveSelected; the index is stale\n    return;\n}","typeGuard":"static bool IsValidDictionaryIndex(int index)\n{\n    return index >= 0 && index < Application.Current.Resources.MergedDictionaries.Count;\n}","tryCatchPattern":"try\n{\n    LocalizationHelper.SaveSelected(selectedIndex, path);\n}\ncatch (IndexOutOfRangeException ex)\n{\n    LogWriter.Log(ex, $\"Invalid dictionary index: {selectedIndex}\");\n    // Reset UI selection to a valid index\n}","preventionTips":["After adding/removing/reordering dictionaries, immediately update the UI selection to a valid index.","Disable the Save button when selectedIndex is -1 or out of range.","Use ObservableCollection data binding to keep the UI in sync with the dictionaries list."],"tags":["localization","resource-dictionary","validation","index-out-of-range","xaml"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}