Results 1 to 7 of 7

Thread: IF My.Settings String Collection is empty.

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    IF My.Settings String Collection is empty.

    How can i check if a string collection is empty? I tried
    VB.NET Code:
    1. My.Settings.IPBank.Count = 0
    But it said
    Object reference not set to an instance of an object.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: IF My.Settings String Collection is empty.

    That means that the object has not been created, or instantiated, yet.
    You'd need to create a new one if it is Nothing:
    Code:
    If My.Settings.IPBank Is Nothing Then
        My.Settings.IPBank = New WhateverTypeIPBankIs()
    End If
    After doing that, you can check if its empty or not.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: IF My.Settings String Collection is empty.

    That error shouldn't rule out the attempt, as it generally means that something was not instantiated. If you put a breakpoint on that line and highlight the:

    My.Settings.IPBank portion, then use Shift+F9 to look at the value, you will probably find that it is Nothing. That test, alone, may be sufficient. If that portion is Nothing, then you might try something like this:

    If My.Settings.IPBank Is Not Nothing AndAlso My.Settings.IPBank.Count>0 Then

    The second half of the condition will only evaluate if the first half evaluates to True, so you won't get the error, and will have a pretty robust way of checking whether the collection even exists, and whether it has more than zero items.

    That answer makes a few assumptions about how you have things set up, though.
    My usual boring signature: Nothing

  4. #4

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: IF My.Settings String Collection is empty.

    I would say that there isn't, even though you think it should be there. The test I suggested will tell you that.
    My usual boring signature: Nothing

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: IF My.Settings String Collection is empty.

    Just because you created a StringCollection setting in the Settings list does not mean it holds a StringCollection object yet. You still need to create a New StringCollection:
    Code:
    My.Settings.yourSetting = New StringCollection()
    This is exactly the same as when you are using a List(Of String) in normal code (which is a similar collection):
    Code:
    Dim list As List(Of String)
    Now, list is Nothing and list.Count will not work. You first need to do:
    Code:
    Dim list As List(Of String)
    list = New List(Of String)
    As you may see, it's exactly the same thing: you need to create a new instance of an object before you can use it.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: IF My.Settings String Collection is empty.

    When you add a StringCollection to your Settings it essentially declares a variable but it does not create an object, as NickThissen suggests. If you don't want to add code to create the StringCollection object you can force it to be created in the Settings. Select the Value field in the Settings editor and then click the browse button to open the string editor. Add a dummy value and click OK. You'll see that there's now some XML code in the Value field. It's that XML code that creates and optionally populates the StringCollection. You can now open the string editor again and remove your dummy value. When you click OK you'll see that the Value property still contains some XML but examination will reveal that it contains no items.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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