|
-
Sep 21st, 2010, 04:51 AM
#1
Thread Starter
Member
[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.
-
Sep 21st, 2010, 06:15 AM
#2
Fanatic Member
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim str() As String = (New String() {"Test1", "Test2", "Test3"}) If My.Settings.Records Is Nothing Then My.Settings.Records = New Collections.ArrayList End If My.Settings.Records.AddRange(str) My.Settings.Save() My.Settings.Reload() For Each s As String In My.Settings.Records MsgBox(s) Next End Sub
NOTE: I only added this to a form_load event because I was testing, it doesn't have to be there.
-
Sep 21st, 2010, 07:25 AM
#3
Thread Starter
Member
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.
-
Sep 21st, 2010, 07:32 AM
#4
Fanatic Member
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:
Dim myArrayList As ArrayList = My.Settings.Records
-
Sep 21st, 2010, 07:38 AM
#5
Thread Starter
Member
Re: ArrayList in My.Settings problem
Yeah I am. It's just easier to type it once and then refer to myArrayList each time.
-
Sep 21st, 2010, 07:40 AM
#6
Fanatic Member
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.
-
Sep 21st, 2010, 08:49 AM
#7
Thread Starter
Member
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.
-
Sep 21st, 2010, 09:16 AM
#8
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
-
Sep 21st, 2010, 09:22 AM
#9
Fanatic Member
Re: ArrayList in My.Settings problem
 Originally Posted by techgnome
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.
-
Sep 21st, 2010, 09:26 AM
#10
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
-
Sep 21st, 2010, 10:08 AM
#11
Fanatic Member
Re: ArrayList in My.Settings problem
 Originally Posted by techgnome
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.
-
Sep 21st, 2010, 10:11 AM
#12
Thread Starter
Member
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.
-
Sep 21st, 2010, 10:17 AM
#13
Fanatic Member
Re: ArrayList in My.Settings problem
 Originally Posted by natrap
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.
-
Sep 21st, 2010, 10:21 AM
#14
Thread Starter
Member
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?
-
Sep 21st, 2010, 10:25 AM
#15
Fanatic Member
Re: ArrayList in My.Settings problem
 Originally Posted by natrap
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:
If My.Settings.Records Is Nothing then
My.Settings.Records = New ArrayList
My.Settings.Records = myArrayList
Else
myArrayList = My.Settings.Records
End If
-
Sep 21st, 2010, 10:45 AM
#16
Thread Starter
Member
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.
-
Sep 21st, 2010, 10:50 AM
#17
Fanatic Member
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?
-
Sep 21st, 2010, 11:03 AM
#18
Thread Starter
Member
Re: ArrayList in My.Settings problem
Yeah it's solved now. Thanks very much both of you for your assistance.
-
Sep 21st, 2010, 08:52 PM
#19
Fanatic Member
Re: ArrayList in My.Settings problem
Make sure to take advantage of the reputation system techgnome was particularly informative.
-
Sep 21st, 2010, 09:06 PM
#20
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|