Results 1 to 11 of 11

Thread: [RESOLVED] My.Settings ArrayLists

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [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?

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: My.Settings ArrayLists

    Why do you want to use an array list, wouldn't a System.Collections.Specialized.StringCollection be more appropriate?

  3. #3

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: My.Settings ArrayLists

    Quote 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?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: My.Settings ArrayLists

    StringCollections work just like a list(of string)

  5. #5
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    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.

  6. #6

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: My.Settings ArrayLists

    Quote 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).

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    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

  8. #8
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    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.

  9. #9

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: My.Settings ArrayLists

    Quote 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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  11. #11

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: My.Settings ArrayLists

    Quote 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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

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