Results 1 to 20 of 20

Thread: [RESOLVED] ArrayList in My.Settings problem

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Resolved [RESOLVED] ArrayList in My.Settings problem

    What I need to do is read an ArrayList in My.Settings and add another ArrayList to it.

    The first think I'm trying to do is load My.Settings.Records into myArrayList. I'm not quite sure on the proper way to do this.
    I've tried things such as
    Code:
    Dim myArrayList As ArrayList = My.Settings.Records
    I'm not sure if I need New and I'm not sure if I need .Clone or .CopyTo

    Then I have another ArrayList myArrayList2. To get this into myArrayList I've done:
    Code:
    Dim myArr As String() = myArrayList2.ToArray(GetType(String))
    myArrayList.AddRange(myArr)
    Then to save it back to My.Settings I've done
    Code:
    My.Settings.Records = myArrayList
    The problem is I keep on getting Object reference not set to an instance of an object error on the AddRange line.

    My.Settings.Records is definitely an ArrayList and it will sometimes = Nothing. The objects will only ever be strings.

  2. #2
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Try add this to your Form_Load event to substantiate your My.Settings arraylist:

    Code:
            If My.Settings.Records Is Nothing Then
                My.Settings.Records = New System.Collections.ArrayList
            End If
    And you don't need to assign a new array that is equal to the My.Settings array to add to it, the My.Settings.Records should behave the same way as a normal array. Here's an example:

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim str() As String = (New String() {"Test1", "Test2", "Test3"})
    3.         If My.Settings.Records Is Nothing Then
    4.             My.Settings.Records = New Collections.ArrayList
    5.         End If
    6.  
    7.         My.Settings.Records.AddRange(str)
    8.         My.Settings.Save()
    9.         My.Settings.Reload()
    10.  
    11.         For Each s As String In My.Settings.Records
    12.             MsgBox(s)
    13.         Next
    14.     End Sub

    NOTE: I only added this to a form_load event because I was testing, it doesn't have to be there.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: ArrayList in My.Settings problem

    Thanks for that. Checking if it is Nothing took care of it. The problem arose because of setting myArrayList to My.Settings.Records when the latter is Nothing.

  4. #4
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Yeah I had the same problem when using the StringCollection class, until I browsed google and found a solution on the social MSDN thing, then it was so obvious :P So I figured it'd probably work.

    Are you still using this?
    vb Code:
    1. Dim myArrayList As ArrayList = My.Settings.Records

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: ArrayList in My.Settings problem

    Yeah I am. It's just easier to type it once and then refer to myArrayList each time.

  6. #6
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Hmm yeah I mean it will still work, but if your array is getting pretty big it'll take longer times to transfer your My.Settings array to a new array, add items to it and then transfer it all back again.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: ArrayList in My.Settings problem

    It will only have a few items in it any time so it's not too big a deal but you're right it's not the most efficient way.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ArrayList in My.Settings problem

    Actually... reading the My.Settings.SomeProperty into a variable and using it from there is most efficient... if you constantly read the My.Settings.SomeProperty directly, that's accessing the config file each time. Loading it into memory, manipulating it, then writing it back is actually going to be best overall.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Quote Originally Posted by techgnome View Post
    Actually... reading the My.Settings.SomeProperty into a variable and using it from there is most efficient... if you constantly read the My.Settings.SomeProperty directly, that's accessing the config file each time. Loading it into memory, manipulating it, then writing it back is actually going to be best overall.

    -tg
    Oh okay, thanks for the info, I somehow thought it would be less efficient but I didn't take into account that My.Settings has to access the config file.

    Cheers.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ArrayList in My.Settings problem

    OK... after I said that, something didn't sound right, so I did a little test, and it turns out I'm full of it... so ignore what I said.

    What I remembered after my previous post was that when an app is loaded, the machine.config file is loaded, the app.confic file is then loaded and overlaid what loaded from the machine file, then the user.config file is loaded and that is overlaid as well... that means all settings from my.settings is loaded into memory.

    Since it is an arraylist (and if you can, since you are storing strings, the Specialized.StringsColleciton would be best) is an object... assigning it to a new variable just points the reference in memory... so the list isn't actually getting copied... jsut the pointer to the list. In short, it doesn't matter if you use a varaiable or the my.settings.someproperty directly... both are going to point to the same location. In fact, it would not surprise me to find out that assignign your variable back to the settings property isn't necessary... since they both reference the same memory location (as both are objects).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Quote Originally Posted by techgnome View Post
    OK... after I said that, something didn't sound right, so I did a little test, and it turns out I'm full of it... so ignore what I said.

    What I remembered after my previous post was that when an app is loaded, the machine.config file is loaded, the app.confic file is then loaded and overlaid what loaded from the machine file, then the user.config file is loaded and that is overlaid as well... that means all settings from my.settings is loaded into memory.

    Since it is an arraylist (and if you can, since you are storing strings, the Specialized.StringsColleciton would be best) is an object... assigning it to a new variable just points the reference in memory... so the list isn't actually getting copied... jsut the pointer to the list. In short, it doesn't matter if you use a varaiable or the my.settings.someproperty directly... both are going to point to the same location. In fact, it would not surprise me to find out that assignign your variable back to the settings property isn't necessary... since they both reference the same memory location (as both are objects).

    -tg
    Oh nice, didn't know much of that at all, but whether it would add to settings as you add to the array that is equal to the settings..i.e

    Code:
    Dim theArray as arraylist = My.Settings.Records
    So as you add to "theArray" you say it may add to My.Settings.Records?

    An interesting thought, I'll go test it now. I always thought that when you assign a variable a value from another object, in this case whatever is contained in "My.Settings.Records", it will obtain all those values. But from then on, "theArray" isn't linked to My.Settings.Records. I better go test, otherwise my previous thoughts are wrong :P

    Thanks again for the info! (rated )

    EDIT: Well here's the results, I just tested it all out and guess what! You don't need to reassign the array back into your My.Settings object, you just have to do My.Settings.Save somewhere to make sure the array is saved and then that's it, techgnome was right
    Last edited by J-Deezy; Sep 21st, 2010 at 10:12 AM.

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: ArrayList in My.Settings problem

    Why is StringsCollections best? Aren't they the same except that ArrayList can contain any type.

    I tested your theory and you were right. There is no need to have My.Settings.Records = myArrayList after myArrayList = My.Settings.Records and myArrayList.addRange(myArr).

    The problem is sometimes My.Settings.Records = Nothing and if I have myArrayList = My.Settings.Records when it = Nothing then I get the error from my original post on the addRange line, though I don't really understand why. EDIT: I think I just got it. Setting myArrayList to Nothing removes it from memory so there would be the need to have myArrayList = New ArrayList which is just a mess I think.

    Maybe I'll try without using the myArrayList variable. The query I have though is that if I use as I would have to
    Code:
    If My.Settings.Records Is Nothing Then
           My.Settings.Records = New ArrayList
    End If
    and then there is nothing to add to it (which will sometimes be the case) will it still = Nothing the next time or will it contain an empty ArrayList (I assume there is a difference between Nothing and an empty Arraylist here)?
    Last edited by natrap; Sep 21st, 2010 at 10:17 AM.

  13. #13
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Quote Originally Posted by natrap View Post
    Why is StringsCollections best? Aren't they the same except that ArrayList can contain any type.

    I tested your theory and you were right. There is no need to have My.Settings.Records = myArrayList after myArrayList = My.Settings.Records and myArrayList.addRange(myArr).

    The problem is sometimes My.Settings.Records = Nothing and if I have myArrayList = My.Settings.Records when it = Nothing then I get the error from my original post on the addRange line, though I don't really understand why.

    Maybe I'll try without using the myArrayList variable. The query I have though is that if I use as I would have to
    Code:
    If My.Settings.Records Is Nothing Then
           My.Settings.Records = New ArrayList
    End If
    and then there is nothing to add to it (which will sometimes be the case) will it still = Nothing the next time or will it contain an empty ArrayList (I assume there is a difference between Nothing and an empty Arraylist here)?
    Well always add that snippet to your Form_Load regardless and if it's "nothing" then it will create a new arraylist. String collections would be best because you said you're only saving string, are you not? And without the "myArrayList" variable you still need to check for null reference exception in the "My.Settings" class because that's what was throwing the error in the first place, not the myArrayList variable!

    Also, when doing:

    Code:
    myArrayList = My.Settings.Records
    Make sure that's done AFTER ensuring My.Settings.Records is not null i.e the

    Code:
    If My.Settings.Records Is Nothing then....
    Also, once the above statement has been executed, provided you save settings somewhere before closing, you won't get the "nothing" error again as you've created an instance of the object
    Last edited by J-Deezy; Sep 21st, 2010 at 10:20 AM.

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: ArrayList in My.Settings problem

    My point was that if My.Setting.Records = Nothing then I don't have myArrayList = My.Setting.Records so there is a need to have My.Settings.Records = myArrayList.

    I am only saving strings. I've got quite a few ArrayLists in this program that all only have strings. Is there any benefit to going through and changing them to StringCollections?

  15. #15
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Quote Originally Posted by natrap View Post
    My point was that if My.Setting.Records = Nothing then I don't have myArrayList = My.Setting.Records so there is a need to have My.Settings.Records = myArrayList.

    I am only saving strings. I've got quite a few ArrayLists in this program that all only have strings. Is there any benefit to going through and changing them to StringCollections?
    Well if you don't have anything in My.Settings.Records, how can you have anything in "myArrayList"? The declaration you showed us for "myArrayList" set it to the contents of My.Settings.Record, therefore if your settings are empty, your "myArrayList" will be empty too?

    Also if for some reason what you said above IS necessary, this should sort it out (although you should have been able to see this yourself)

    vb Code:
    1. If My.Settings.Records Is Nothing then
    2.      My.Settings.Records = New ArrayList
    3.      My.Settings.Records = myArrayList
    4. Else
    5.      myArrayList = My.Settings.Records
    6. End If

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: ArrayList in My.Settings problem

    I probably should have explained the logic from the beginning.
    1.Check if certain strings (contained in another arraylist but this is unimpotant) are contained in the setting using My.Settings.Records.Contains (which starts out blank [I left the value field blank in the application settings])
    2.The strings (if any) that are NOT contained in the setting are added to an arraylist called tempArrayList.
    3.The user is prompted whether or not to add the strings in tempArrayList to the setting.

    As I suspected, if My.Settings.Records = New ArrayList is called then even though nothing is added to it it will no longer = Nothing and will thereafter = an ArrayList of Count = 0.

    I've gotten rid of myArrayList and it works well. If you say that running .Contains directly on the setting is no less efficient then I'll do that.

    Just so that I could remove the check for Nothing am I able in the application settings window to set the value to equal an ArrayList of Count = 0 rather than Nothing so that on the first runtime it won't = Nothing.

  17. #17
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Ahh okay, yeah I didn't think of that, good idea :P

    So is this solved now, or did you have some other queries?

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    37

    Re: ArrayList in My.Settings problem

    Yeah it's solved now. Thanks very much both of you for your assistance.

  19. #19
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: ArrayList in My.Settings problem

    Make sure to take advantage of the reputation system techgnome was particularly informative.

  20. #20
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ArrayList in My.Settings problem

    Also please mark the thread "Resolved" - it's under the Thread Tools at the top of the first post. This allows others searching for similar problems that it's been answered.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width