locked
Error when Dictionary has no entries

    Question

  • I have a dictionary declared as:

    Private Property SelectedGiftIndex() As New Dictionary(Of String, Integer)

    I store this data using the following code (since a Dictionary cannot be easily serialized):

    ApplicationData.Current.LocalSettings.Values("GiftIndexKeys") = Me.SelectedGiftIndex.Keys.ToArray()
    ApplicationData.Current.LocalSettings.Values("GiftIndexValues") = Me.SelectedGiftIndex.Values.ToArray()

    However, even though the Dictionary is declared as a New Dictionary, I receive the following error when there are no entries:

    Error trying to write application data container value
    Incorrect size argument

    The code works fine when there are 1 or more entries, so shouldn't my code just end up assigning zero-length arrays when there are no entries? Any help would be appreciated. Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, January 9, 2015 4:05 AM

Answers

  • Hi,

    You cannot set an empty array to application local setting. If you do not add value to Dictionary, the array is empty if you used the "SelectedGiftIndex.Keys.ToArray()" method. You should try to edit your code below:

      if(SelectedGiftIndex.Keys.ToArray().Length>0)
                {
                    ApplicationData.Current.LocalSettings.Values["GiftIndexKeys"] = SelectedGiftIndex.Keys.ToArray();
                }
                if (SelectedGiftIndex.Values.ToArray().Length > 0)
                {
                    ApplicationData.Current.LocalSettings.Values["GiftIndexValues"] = SelectedGiftIndex.Values.ToArray();
                }

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.


    Friday, February 6, 2015 8:13 AM

All replies

  • Try show full of your code
    Monday, January 12, 2015 2:28 AM
  • I'm not sure what other code you want to see. I show the declaration, and the line where the error occurs. What do you need to see?

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, January 12, 2015 3:08 AM
  • Hi,

    You cannot set an empty array to application local setting. If you do not add value to Dictionary, the array is empty if you used the "SelectedGiftIndex.Keys.ToArray()" method. You should try to edit your code below:

      if(SelectedGiftIndex.Keys.ToArray().Length>0)
                {
                    ApplicationData.Current.LocalSettings.Values["GiftIndexKeys"] = SelectedGiftIndex.Keys.ToArray();
                }
                if (SelectedGiftIndex.Values.ToArray().Length > 0)
                {
                    ApplicationData.Current.LocalSettings.Values["GiftIndexValues"] = SelectedGiftIndex.Values.ToArray();
                }

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.


    Friday, February 6, 2015 8:13 AM