none
Saving class data in vs2008 vb.net my.settings RRS feed

  • Question

  • I have a class object defined as follows:

    Friend Class clsMisc
      Friend Name1 as String
      Friend Name2 as String
      Friend Description as String
      Friend Private as Boolean
      Friend MaxP as Integer
    End Class

    In My.Settings, I have value called Descripts with a type of System.Collections.SortedList. When the program starts, I check to see if this value is "nothing" and then if so, I create a New System.Collections.SortedList.

    At various points, I check to see if the key value (Name1) passed exists, and if not I create a new variable and assign it to the class, populate the class members with data, and then add that class data back into settings with this type of code:

    g = New clsMisc
    g.Name1 = string1
    g.Name2 = string2
    g.Description = string3
    g.Private = False
    g.MaxP = numbervalue
    My.Settings.Descripts.Add(string1, g)
    My.Settings.Save

    As the program executes, I can get those values back into a class holder variable, manipulate them, etc... and at some point the program terminates. The next time I run it, the values are not there.

    I've put a break point on when the main form closes, and I have inspected the values in "Descripts" and all of the keys and data are there in memory, but they are not saved into settings. When I look at the user.config file My.Settings saves into, this is all that is shown:

    <setting name="Descripts" serializeAs="Xml">
        <value />
    </setting>

    Whats the secret on saving "class object data" into the settings file? I had thought maybe the config file was glitched, so I deleted it, allowing the program to make a fresh clean one, and it's made no difference.

    Thanks kindly in advance for your time and assistance with this.

    Gary :)

    Monday, February 4, 2013 5:14 PM

Answers

  • Thanks again Mike. I have looked over your list of links, and from reading between the lines it would seem that doing "these types" of defined data within the My.Settings object isn't supported. I looked at the "connect" site, and it seems this is for reporting bugs within VS2012.

    I get how managing this would be difficult... there is nothing there telling the program exactly which data it needs to export. Seems like the perfect place for "defining" what object it is supposed to be looking at would be in the project settings "value" field. For example in my case I could put in "clsMisc" as the value, and then the compiler and program would know what object to use.

    And yes, I can see that getting more complex if the object also stores another object type in it. For instance, if my class definition also included another dictionary or array object type. I am surprised on how long Visual Studio has been out, and this matter hasn't really been addressed. Shrug..

    I made some startup code, that creates an xml file if it doesn't exist, and then loads the xml values into my class object.. As things change within the program, the internal memory is all updated for the object and then when the program closes, I just write out the class data. Little bit of a PITA but I guess it is what it is...

    Monday, February 11, 2013 7:14 PM

All replies

  • Hi Gary,

    >>at some point the program terminates. The next time I run it, the values are not there.

    Based on your description, it seems that your program didn't save the settings before the termination.

    Generally, the termination is an unexpected behavior, it would generate the unexpected result.

    >>Whats the secret on saving "class object data" into the settings file? 

    Simple answer is: the settings file is an xml file, and your program will write/read this xml file. It is the similar when you manipulation an XML file with XML namespace: http://msdn.microsoft.com/en-us/library/system.xml(v=vs.110).aspx

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 5, 2013 9:18 AM
    Moderator
  • Thanks for the response Mike. You would be correct in the fact that this variable in My.Settings is not being saved. There is code in multiple places that calles the save function, plus the project is set to save on close and the main form close event also has the save code in it. So from the aspect of "termination", it was putting a break point on the main form close event, and inspecting the values in the My.Settings memory object.

    And those values ARE there in memory. What I find curious, in looking at the user.config file, I have another variable using a System.Collections.Specialized.StringCollection, and the xml code for that is:

    <setting name="FavEmotes" serializeAs="Xml">
        <value>
          <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
        </value>
    </setting>

    So this has an opening "<value>" with other definitions and yet the Descripts entry is missing the opening tag. Again, I have deleted this file and let it be created fresh, and it's always the same. To be honest, I am not sure why you supplied the XML link (thanks though) because I thought the "My.Settings" object within Visual Studio handled all of that code.

    Is there perhaps a different object type I should have Descripts set to, rather than a sorted dictionary list? I still need the object class to be as defined, and I can easily do code that would put all the class values into all strings, instead of the combination of string, int, bool etc.

    Maybe Visual Studio just isn't smart enough to store this data this way.

    Gary

    Tuesday, February 5, 2013 9:21 PM
  • Hi Gary,

    Does the termination not mean the program exited uncorrectly? I said this because that you can set a break point in the source code and check the memory value. I think my meaning of termination is different from yours. Anyway, if you can handle the close event, so just call the save method again in the close event handler.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 6, 2013 5:40 AM
    Moderator
  • Yes, it would be different meanings, sorry for the confusion. Mine was "program ends normally, forms close" etc. And yes, I do call the save method. And no, it does not save the data. :(

    I can see it there in memory... the keys, the values... all correctly formatted. It just won't save it.

    I was unfamiliar with how the XML tags worked, and from doing a little playing around with a separate file, I see that if a value has no data with it, the settings code just uses a closing tag. Which sort of explains my last response a bit... My.Settings is not writing the values it has.

    My system is XP Pro, SP3, all updates and patches in place. All Visual Studio updates and patches done. From everything I can see, my code is correct, the data is there. It appears that Visual Studio is not smart enough to know how to save this particular data type.

    Looks like I will have to resort to manually creating a XML file when the program starts, have it load any existing data into the My.Settings.Descripts object... any changes the program makes in that area (data added, values changed etc) will then have to be written back to that XML file when the program closes.

    Sad I have to do 1980's style coding in this century of OOP.

    Wednesday, February 6, 2013 4:42 PM
  • Hi Fickle,

    Thank you for clarification.

    Actually, the method: 

    My.Settings.Save()

    is dedicated for this purpose. When the settings are not saved, please check:

    Although you can change and save the values of user-scope settings at run time, application-scope settings are read-only and cannot be changed programmatically. You can change application-scope settings when creating the application, through the Project Designer, or by editing the application's configuration file. For more information, see Managing Application Settings.

    If this is not the cause, would you please tell me how complex the settings you try to save, and I will try to reproduce it on my side.

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 7, 2013 8:55 AM
    Moderator
  • Yes, I call My.Settings.Save() before the main application form closes. I also have the project set to save settings on shutdown. The setting name of "Descripts" is defined in user scope... all settings in My.Settings are defined as 'user scope'.

    Create a new project, create a few string and integer settings... perhaps set some default values for those. Create one setting that uses System.Collections.Specialized.StringCollection, and another one named "Descripts" with a System.Collections.SortedList.

    Form open event, set both of the collections to a New instance (if the current instance is Nothing, which they will be on first run).

    Create the Class clsMisc as shown in the first post... run some code that will add 2 or 3 entries into the My.Settings.Descripts side as shown also in the first post. Obviously the "g.Name1" entry needs to be different for each entry seeing its the key value.

    Save settings all you want to. In the main form close event, pop My.Settings.Deescripts into a watch window (or hover over the code when you step through it) and you should see the entries you made.

    Run the form again... note the My.Settings.Descripts will be "nothing" when it loads... or look at the user.config file your program made... and I bet you will see nothing there.

    My current VS2008 help file says "Version 9.0.21022.8 RTM" with .Net framework version 3.5 SP1.

    If your system works, then I really want to know why mine doesn't. For what it's worth, a new motherboard was installed a few months back, and the system hard drive was fully formatted, new fresh XP Pro SP3 installed, all updates done, fresh install of vs2008, all updates done. So that leads me to believe my system is "current". And yes, full AV and spyware scans (from multiple sources) show no problems.

    Thanks for your attempts to help understand and solve this. I *have* created the xml file code which does loop through the My.Settings.Descripts object at shutdown, and loads the values from the same file at run time... so this isn't "urgent" to completing my task. But it would sure be nice to know why it's not being saved, so that either MS can fix the bug or I can fix my system so it works in future projects.

    Gary :)

    Thursday, February 7, 2013 8:23 PM
  • Hi Gary,

    Thank you for your detailed reproduce steps. I followed it and found that, the StringCollection value has been saved successfully, but the SortedList value not. So I can say I reproduced your scenario, right?

    You can report this issue on connect site: http://connect.microsoft.com/VisualStudio , when you finished, please post the link here, so the other community members can follow this issue. Thank you.

    In addition, for some other classes can be added in the settings by designer, you can also create it by providing SettingProvider: http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx 

    The ApplicationSettingsBase class does not itself persist or load settings; this job falls to the settings provider, a class that derives from SettingsProvider. If a derived class ofApplicationSettingsBase does not specify a settings provider through the SettingsProviderAttribute, then the default provider, LocalFileSettingsProvider, is used.

    And here is a blog about customer defined type: http://blogs.msdn.com/b/johan_stenbergs_blog/archive/2005/11/17/494163.aspx

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 8, 2013 6:54 AM
    Moderator
  • Thanks again Mike. I have looked over your list of links, and from reading between the lines it would seem that doing "these types" of defined data within the My.Settings object isn't supported. I looked at the "connect" site, and it seems this is for reporting bugs within VS2012.

    I get how managing this would be difficult... there is nothing there telling the program exactly which data it needs to export. Seems like the perfect place for "defining" what object it is supposed to be looking at would be in the project settings "value" field. For example in my case I could put in "clsMisc" as the value, and then the compiler and program would know what object to use.

    And yes, I can see that getting more complex if the object also stores another object type in it. For instance, if my class definition also included another dictionary or array object type. I am surprised on how long Visual Studio has been out, and this matter hasn't really been addressed. Shrug..

    I made some startup code, that creates an xml file if it doesn't exist, and then loads the xml values into my class object.. As things change within the program, the internal memory is all updated for the object and then when the program closes, I just write out the class data. Little bit of a PITA but I guess it is what it is...

    Monday, February 11, 2013 7:14 PM
  • Hi Gary,

    >> it would seem that doing "these types" of defined data within the My.Settings object isn't supported. I looked at the "connect" site, and it seems this is for reporting bugs within VS2012.

    You can click the Submit an Idea button: http://visualstudio.uservoice.com/forums/121579-visual-studio 

    >>Little bit of a PITA but I guess it is what it is...

    Thank you for your confirmation. 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 13, 2013 2:53 AM
    Moderator