2

I am getting an InvalidCastException and I don't understand why.

Here is the code that raises the exception:

public static void AddToTriedList(string recipeID)
{
    IList<string> triedIDList = new ObservableCollection<string>();
    try
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        if (!settings.Contains("TriedIDList"))
        {
            settings.Add("TriedIDList", new ObservableCollection<Recipe>());
            settings.Save();
        }
        else
        {
            settings.TryGetValue<IList<string>>("TriedIDList", out triedIDList);
        }
        triedIDList.Add(recipeID);
        settings["TriedIDList"] = triedIDList;
        settings.Save();
    }
    catch (Exception e)
    {
        Debug.WriteLine("Exception while using IsolatedStorageSettings in AddToTriedList:");
        Debug.WriteLine(e.ToString());
    }
}

AppSettings.cs: (extract)

// The isolated storage key names of our settings
const string TriedIDList_KeyName = "TriedIDList";

// The default value of our settings
IList<string> TriedIDList_Default = new ObservableCollection<string>();

...

/// <summary>
/// Property to get and set the TriedList Key.
/// </summary>
public IList<string> TriedIDList
{
    get
    {
        return GetValueOrDefault<IList<string>>(TriedIDList_KeyName, TriedIDList_Default);
    }
    set
    {
        if (AddOrUpdateValue(TriedIDList_KeyName, value))
        {
            Save();
        }
    }
}

GetValueOrDefault<IList<string>>(TriedIDList_KeyName, TriedIDList_Default) and AddOrUpdateValue(TriedIDList_KeyName, value) are the usual methods recommended by Microsoft; you can find the full code here.

EDIT: I got exception in this line:

settings.TryGetValue<IList<string>>("TriedIDList", out triedIDList);
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
maxdelia
  • 858
  • 2
  • 14
  • 35
  • *Where* are you getting the exception? Which line? –  Jun 24 '12 at 19:04
  • It would be extremely helpful if you stepped through the debugger to identify the specific line that's generating the error. – paulsm4 Jun 24 '12 at 19:04
  • I'm sorry, i forget to write. The exception raises here: settings.TryGetValue>("TriedIDList", out triedIDList); – maxdelia Jun 24 '12 at 19:08

1 Answers1

3

You're adding a ObservableCollection<Recipe> to your settings:

settings.Add("TriedIDList", new ObservableCollection<Recipe>());
                             // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

But then you're reading back a IList<string>, which is quite obviously a different type:

settings.TryGetValue<IList<string>>("TriedIDList", out triedIDList);
                  // ^^^^^^^^^^^^^

Your declaration of triedIDList looks like this:

   IList<string> triedIDList = new ObservableCollection<string>();
// ^^^^^^^^^^^^^                // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

As a start, decide on one type and then use the exact same type in all these places (even if you think it's not strictly necessary), then see if the InvalidCastException goes away.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • 1
    I would further suggest that you set up a small sandbox project and run a few experiments of your own to see which kinds of conversions are allowed with generic types (and perhaps arrays, too!), and which are not. That's always useful knowledge! ;) – stakx - no longer contributing Jun 24 '12 at 19:20
  • I'll do, thank you for the suggestion! And thank you again for having solved my exception issue! – maxdelia Jun 24 '12 at 19:25