Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:05:27+00:00 2026-06-06T21:05:27+00:00

I am getting an InvalidCastException and I don’t understand why. Here is the code

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:05:28+00:00Added an answer on June 6, 2026 at 9:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
getting an unusual error:- 'void' type not allowed here import javax.sound.midi.*; public class MiniMusicPlayer1
I am getting an IndexOutOfRange exception on the following line of code: var searchLastCriteria
Getting this error when I follow the simple code example from jsoncpp, that basically
Why I'm getting this error? System.InvalidCastException was unhandled by user code Message=Specified cast is
I am getting InvalidCastException when i am trying to use this code. I am
Getting an InvalidCastException when trying something like this : IEnumerable<object> test = (IEnumerable<object>)new List<KeyValuePair<string,
Getting an Undefined Offset error here -- apparently from the $newval array. Note that
Getting the following Arguement Exception (Expression must be readableParameter name: right) running this code
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; Using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.