|
-
Mar 1st, 2009, 02:46 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] My.Settings ArrayLists
I was trying to use ArrayLists with My.Settings but I can't seem to figure out how to get them to work.
If I make a setting called "Words" and make it an ArrayList without a value, the program has an error every time I try to run it. If I try to give it a value in the project properties window it always gives me an incorrect data type error. Besides, I don't want it to have a value, I want it to be blank such as when declaring a New ArrayList. The only way I can get an ArrayList My.Setting to work is by clicking in the Value box, clicking the "..." button in the corner and adding some kind of Object with the add button. This automatically puts some kind of wierd object in the first index of the array list. If I don't convert Item(0) of the arraylist to a string then it also gives me an error when I try to read it. As a string it comes up as System.Object.
Even though I can use the my.settings.Words ArrayList with an object in the first index, how can I get the setting to start off blank without having to constantly avoid the first index?
-
Mar 1st, 2009, 03:58 PM
#2
Re: My.Settings ArrayLists
Why do you want to use an array list, wouldn't a System.Collections.Specialized.StringCollection be more appropriate?
-
Mar 1st, 2009, 04:16 PM
#3
Thread Starter
Fanatic Member
Re: My.Settings ArrayLists
 Originally Posted by Negative0
Why do you want to use an array list, wouldn't a System.Collections.Specialized.StringCollection be more appropriate?
I don't know, I've never used one. I use ArrayLists because of their .Add feature. With normal arrays you have to resize the whole array and use .length to calculate where to put variables on the end.
Do StringCollections have anything similar to .add? Also what do I set its value to?
-
Mar 1st, 2009, 04:19 PM
#4
Re: My.Settings ArrayLists
StringCollections work just like a list(of string)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 1st, 2009, 07:10 PM
#5
Re: My.Settings ArrayLists
Don't forget to put an Index variable into you settings as well; otherwise, you'll be loading up the List(of string) and its index will be -1, which will throw errors every time you load your program.
-
Mar 1st, 2009, 08:32 PM
#6
Thread Starter
Fanatic Member
Re: My.Settings ArrayLists
 Originally Posted by Campion
Don't forget to put an Index variable into you settings as well; otherwise, you'll be loading up the List(of string) and its index will be -1, which will throw errors every time you load your program.
I don't know how to do that, but it sounds like it's what I need. Can you explain how to set the index variable? (sorry if that's being dense, haven't used arrays much).
-
Mar 1st, 2009, 08:44 PM
#7
Re: My.Settings ArrayLists
its not an arraylist. i don't know where Campion has got the index idea, but you don't need to add 1.
just goto Project-->Properties-->Settings. add a stringcollection setting.
for an example of how to use it, have a look at the my.settings.stringcollection link in my signature
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 1st, 2009, 09:01 PM
#8
Re: My.Settings ArrayLists
The index variable is like a bookmark, or sorts. Take an example of a book. You read the book for a while, and put a bookmark to show where you left. If you return to that book sometime later, you know where to start, because the bookmark is there. The index is the same way. It basically says, "I want this item from the list."
I should add that it's not every case that requires an index, as .paul. mentions; rather, just those that require you to keep track of what has been selected, like I do for option forms that use combo and listboxes. If you aren't going to keep track of the selections, then saving an index is not neccessary.
To save an index, though, you add an integer variable to your settings, just like you did your other settings variables, then name it so you know what it's for. For example, if I have a list called LstDogs, I'd add a variable called LstDogsIndex to note that it is the index for that list.
TO save the index:
Code:
My.Settings.lstDogsIndex = ComboDogs.Selectedindices(0) ' Assuming 1 selected index
To select that item again, you would use this:
Code:
LstDogs.Item(LstDogsIndex)
Also of note, make sure that your lists are initialized when the program starts up. Just because they are in your settings, does not make them automatically initialize.
Code:
If LstDogs Is Nothing then LstDogs = New System.Collections.Specialized.StringCollection
Last edited by Campion; Mar 1st, 2009 at 09:05 PM.
-
Mar 11th, 2009, 10:04 PM
#9
Thread Starter
Fanatic Member
Re: My.Settings ArrayLists
 Originally Posted by Campion
Also of note, make sure that your lists are initialized when the program starts up. Just because they are in your settings, does not make them automatically initialize.
Code:
If LstDogs Is Nothing then LstDogs = New System.Collections.Specialized.StringCollection
I was just about to ask about how to declare a My.Setting as "New" and avoid getting that initialized error, I'll try this out first thing tomorrow when I get back to my program.
Also thanks to Negative for suggesting StringCollection, works great.
EDIT: I just noticed Campion that you just used LstDogs, assuming that it was My.Settings.LstDogs instead it would still work right?
Last edited by Vectris; Mar 11th, 2009 at 10:07 PM.
-
Mar 11th, 2009, 11:58 PM
#10
Re: My.Settings ArrayLists
You don't necessarily have to use that code to initialise the StringCollection. You can do it in the Settings designer. When you first add a setting of type StringCollection you'll notice that the Value column is empty, i.e. the setting is Nothing. If you then edit the setting and add an item you'll see that the Value column now contains some XML code. That code is a serialised StringCollection containing a single item. You can then edit the setting again and remove the item and you'll see that the XML code for an empty StringCollection is left behind.
-
Mar 12th, 2009, 04:24 PM
#11
Thread Starter
Fanatic Member
Re: My.Settings ArrayLists
 Originally Posted by jmcilhinney
You don't necessarily have to use that code to initialise the StringCollection. You can do it in the Settings designer. When you first add a setting of type StringCollection you'll notice that the Value column is empty, i.e. the setting is Nothing. If you then edit the setting and add an item you'll see that the Value column now contains some XML code. That code is a serialised StringCollection containing a single item. You can then edit the setting again and remove the item and you'll see that the XML code for an empty StringCollection is left behind.
I noticed the Add Item but I never realized that removing it would leave it empty yet initialized at the same time. This is exactly what I needed.
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
|