tags:

views:

90

answers:

4

I know, I know, I could have used a for loop, dont tell me anything about that. Please, help!

Private Function LoadSaved() ''//Loads saved clippings if the user wants us to
        Dim ZomgSavedClips As StringCollection
        If IsDBNull(My.Settings.SavedClips) = False Then ''//If it is null this would return a rather ugly error. Dont want that do we?
            ZomgSavedClips = My.Settings.SavedClips ''//ZomgSavedClips name was a joke, I just felt like it.
            ZomgSavedClips.Add(" ") ''//This line ought to fix the error, but doesnt
            i = 0
            While i < ZomgSavedClips.Count ''//This is where the error occurs
                ClipListings.Rows.Add(ZomgSavedClips(i))
                i = i + 1 ''//First time I wrote this function I forgot this line. Crashed mah comp. Fail.
            End While
        End If
    End Function

The line While i < ZomgSavedClips.Count is bugging, I know that the .count should return null but I even added a blank piece of text just to stop that. Whats up with this? Should I add actual text?

+2  A: 

Obviously, My.Settings.SavedClips is still set to Nothing.

John Saunders
Makes sense, but then the error would be on the "`.Add(" ")`" line (assuming OP left it in).
Thorarin
+3  A: 

SavedClips is null no? If it is null it could pass the test IsDBNull beacuse the both are not the same

Gregoire
How can I check if it is null then?
Cyclone
If SavedClips is nothing ThenElseEnd If
Gregoire
Got it. Thanks!
Cyclone
+1  A: 

SavedClips is regular 'ole null (nothing in VB). Include a check for "My.Settings.SavedClips is nothing". If that evaluates to true then just leave the function.

Guy
A: 

I even added a blank piece of text just to stop that.

All you did was move where the error happens. You can't call .Add() on a null/Nothing object.

'''<summary>Loads saved clippings if the user wants us to</summary>'
Private Sub LoadSaved() ''//Loads saved clippings if the user wants us to
    ''//Load saved clips into memory
    Dim ZomgSavedClips As StringCollection = My.Settings.SavedClips
    If ZomgSavedClips Is Nothing Then ZomgSavedClips = New StringCollection()

    ''//Apply loaded clips to visible listings
    Dim i As Integer
    While i < ZomgSavedClips.Count ''
        ClipListings.Rows.Add(ZomgSavedClips(i))
        i += 1 
    End While
End Sub

Some notes on this code:

  • Don't use Function when you mean Sub
  • Since you'll be selling this code to others, you want to use xml comments at the top so that Visual Studio can give better intellisense helps.
  • IsDBNull() doesn't do what you think it does.
  • Yes, you should use a for loop, but since you already commented on that I left the while loop alone with the assumption that there's more code you didn't show us.
Joel Coehoorn
I actually meant function, I call it several times in the remaining code.Ill use XML comments, fine, not a problem.i was already declared in another bit of code. Thanks for the help!
Cyclone
If you don't return a value, you want 'Sub'
Joel Coehoorn
Alright, ill give that a shot. Could that prevent the code from working if it doesn't return a value?
Cyclone