Link to home
Start Free TrialLog in
Avatar of bluedragon99
bluedragon99Flag for United States of America

asked on

Visual Basic IsNull problem, how to determine if var is null

Hello,

Here's the problem. if this (If Form2.SetAlertsViewer.ListItems(H).ListSubItems(1) = "Source Interface" Then) isn't true SourceIntCol collection never gets built and this (If SourceIntCol(H) = list(Counter) Then) compare sets true due to the fact that sourceintcol(h) is null.  At least I think it is.  Haven't had any luck with isnull.  I need a way to make sure if it is null than it doesn't set             If SourceIntCol(H) = list(Counter) Then        equal to true.  Help!


If A = 1 Then
    FirstItem = list(Counter)

    For H = 1 To Form2.SetAlertsViewer.ListItems.Count
       
        If Form2.SetAlertsViewer.ListItems(H).ListSubItems(1) = "Source Interface" Then
              SourceIntCol.Add Form2.SetAlertsViewer.ListItems(H).ListSubItems(3)
        End If
       
       
   

        If SourceIntCol(H) = list(Counter) Then
       
            Set itmx = Alerter.AlerterStatus.ListItems.Add(, , "")
            itmx.SubItems(1) = "Source Interface"

                If DecimalPorts.Checked = True Then
                    SixthField = CLng("&H" & (SixthField))
                    SeventhField = CLng("&H" & (SeventhField))
                End If

   itmx.SubItems(2) = FirstField
   itmx.SubItems(3) = SecondField
   itmx.SubItems(4) = ThirdField
   itmx.SubItems(5) = FourthField

       If ProtoDec.Checked = True Then
           Call ProtoConvert(FifthItem)
           FifthField = FifthItem
       End If

   itmx.SubItems(6) = FifthField
   itmx.SubItems(7) = SixthField
   itmx.SubItems(8) = SeventhField
   itmx.SubItems(9) = EighthField

   Alerter.AlerterCount.Caption = Alerter.AlerterCount.Caption + 1
    Else
    End If

Next
End If


Avatar of bluedragon99
bluedragon99
Flag of United States of America image

ASKER

This works but it's ghetto, any better ways other than goto??

If A = 1 Then
    FirstItem = list(Counter)

    For H = 1 To Form2.SetAlertsViewer.ListItems.Count
       
        If Form2.SetAlertsViewer.ListItems(H).ListSubItems(1) = "Source Interface" Then
              SourceIntCol.Add Form2.SetAlertsViewer.ListItems(H).ListSubItems(3)
        Else
            GoTo NotThisone:
        End If
       
       
   

        If SourceIntCol(H) = list(Counter) Then
       
            Set itmx = Alerter.AlerterStatus.ListItems.Add(, , "")
            itmx.SubItems(1) = "Source Interface"

                If DecimalPorts.Checked = True Then
                    SixthField = CLng("&H" & (SixthField))
                    SeventhField = CLng("&H" & (SeventhField))
                End If

   itmx.SubItems(2) = FirstField
   itmx.SubItems(3) = SecondField
   itmx.SubItems(4) = ThirdField
   itmx.SubItems(5) = FourthField

       If ProtoDec.Checked = True Then
           Call ProtoConvert(FifthItem)
           FifthField = FifthItem
       End If

   itmx.SubItems(6) = FifthField
   itmx.SubItems(7) = SixthField
   itmx.SubItems(8) = SeventhField
   itmx.SubItems(9) = EighthField

   Alerter.AlerterCount.Caption = Alerter.AlerterCount.Caption + 1
    Else
    End If
NotThisone:
Next
End If
Never mind that does not work it sets true if Source Interface is found in the listview which means it displays all lines.
ASKER CERTIFIED SOLUTION
Avatar of Dabas
Dabas
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
bluedragon99,
What about this:

        If A = 1 Then
            FirstItem = list(Counter)

            For Each li In Form2.SetAlertsViewer.ListItems.Count

                If li Is Nothing Then
                    'Do Nothing
                Else
                    If Form2.SetAlertsViewer.ListItems(H).ListSubItems(1) = "Source Interface" Then
                        SourceIntCol.Add(Form2.SetAlertsViewer.ListItems(H).ListSubItems(3))




                        If SourceIntCol(H) = list(Counter) Then

                            itmx = Alerter.AlerterStatus.ListItems.Add(, , "")
                            itmx.SubItems(1) = "Source Interface"

                            If DecimalPorts.Checked = True Then
                                SixthField = CLng("&H" & (SixthField))
                                SeventhField = CLng("&H" & (SeventhField))
                            End If

                            itmx.SubItems(2) = FirstField
                            itmx.SubItems(3) = SecondField
                            itmx.SubItems(4) = ThirdField
                            itmx.SubItems(5) = FourthField

                            If ProtoDec.Checked = True Then
                                Call ProtoConvert(FifthItem)
                                FifthField = FifthItem
                            End If

                            itmx.SubItems(6) = FifthField
                            itmx.SubItems(7) = SixthField
                            itmx.SubItems(8) = SeventhField
                            itmx.SubItems(9) = EighthField

                            Alerter.AlerterCount.Caption = Alerter.AlerterCount.Caption + 1
                        Else
                        End If
                    End If
                End If
            Next
        End If

Dabas
Oops

Rewrite:


            For Each li In Form2.SetAlertsViewer.ListItems.Count

                If li Is Nothing Then
                    'Do Nothing
                Else
                    If li.SubItems(1) = "Source Interface" Then
                        SourceIntCol.Add(li.SubItems(3))
etc




Dabas
No luck and when it writes to the alert window the fields are completly blank.  I really think SourceIntCol(H) is null and the compare is failing somehow.  I just don't get it.
bluedragon99,
How and where did you declare SourceIntCol?

Dabas
on form load

Dim SourceIntCol As New Collection
bluedragon99,
You are adding the contents of SubItem(3) to the collection.
Is nothing being added at all, or sometimes yes, sometimes no?
What is SubItems(3) holding?

Dabas
This program monitors flows on cisco routers.  users can set alerts like Source Interface = AT1/0.100  

SubItem(1) = "Source Interface"
SubItem(2) = "="
SubItem(3) = "AT1/0.100"

This function I am writing searches the list box for a set alert of source interface and if it finds it compares what it found from the router list(Counter) and the Subitems(3) var.  If the are equal it prints it to the alert window.  Basicly whats happing is the if's are setting true when the should be false. if list(counter) and subitems(3) don't equal, possibly because of it reading the list in (subitem 3 or subitem 1) as null thus screwing up the if statements with nulls... hope this helps.
Simplified it.  This msgbox "MsgBox SourceIntCol(H)" never comes up at all even when source interface is in the 1,1 position

How do I check if it's null all the ways I know are not working..
     


If A = 1 Then
    FirstItem = list(Counter)

    For H = 1 To Form2.SetAlertsViewer.ListItems.Count

          MsgBox "list count is " & Form2.SetAlertsViewer.ListItems.Count
       
       
        If Form2.SetAlertsViewer.ListItems(H).ListSubItems(1) = "Source Interface" Then
            SourceIntCol.Add Form2.SetAlertsViewer.ListItems(H).ListSubItems(3)
            MsgBox SourceIntCol(H)
        End If
       
       
        If SourceIntCol(H) <> list(Counter) Then
            MsgBox "the two vars are NOT equal"
        End If


Next
End If
and this one comes up whether I do <> or =.......?!??!

 If SourceIntCol(H) <> list(Counter) Then
            MsgBox "the two vars are NOT equal"
        End If
bluedragon99,
Set a breakpoint on the IF .... = "Source Interface" line by placing the cursor on it and pressing F9
Run the program and when it stops at the breakpoint:
Highlight Form2.SetAlertsViewer.ListItems(H).ListSubItems(1), right click  and Add Watch
I have the feeling the contents are not exactly "Source Interface"

Dabas
never been able to get the watches working it says

Watch :   : SetAlertsViewer : <Expression not defined in context> : Empty : Form1.Timer2_Timer
got the watch hang on..
Ok set three watches.  It appears that the problem is SourceIntCol(P) is not defined because of this if failing:

If Form2.SetAlertsViewer.ListItems(P).ListSubItems(1) = "Source Interface" Then
        SourceIntCol.Add Form2.SetAlertsViewer.ListItems(P).ListSubItems(3)
End If

sooo...this SourceIntCol(P) doesn't exist.  How do I get around this?


Change: + : Form2.SetAlertsViewer.ListItems(P).ListSubItems(1) : "Source IP" : Object/IListSubItem : Form1.Timer2_Timer
Watch :   : List(Counter) : "************" : Variant/String : Form1.Timer2_Timer
Watch :   : SourceIntCol(P) : <Invalid procedure call or argument> : Variant/Integer : Form1.Timer2_Timer

If I do this it fixes the problem completly but this won't work because it only compares the alert values no the types...if someone set a source ip alert and this finds a destination ip that matches it could alert

'   If Form2.SetAlertsViewer.ListItems(P).ListSubItems(1) = "Source Interface" Then
        SourceIntCol.Add Form2.SetAlertsViewer.ListItems(P).ListSubItems(3)
'   End If
This also works but will it cause problems with other alerts?  If it doesn't get defined by finding source int it defines it as a space and then it fails the next compare (what the router says and what val the alert is set for)

If Form2.SetAlertsViewer.ListItems(P).ListSubItems(1) = "Source Interface" Then
        SourceIntCol.Add Form2.SetAlertsViewer.ListItems(P).ListSubItems(3)
   End If
        SourceIntCol.Add " "
This is my final code which seems to work great.  This seemed to be the key:  

   If Form2.SetAlertsViewer.ListItems(P).ListSubItems(1) = "Source Interface" Then
        SourceIntCol.Add Form2.SetAlertsViewer.ListItems(P).ListSubItems(3)
   End If
        SourceIntCol.Add " "


Any better ideas??




If A = 1 Then
Dim SourceIntCol As New Collection
   
    FirstItem = list(Counter)

For P = 1 To Form2.SetAlertsViewer.ListItems.Count

   If Form2.SetAlertsViewer.ListItems(P).ListSubItems(1) = "Source Interface" Then
        SourceIntCol.Add Form2.SetAlertsViewer.ListItems(P).ListSubItems(3)
   End If
        SourceIntCol.Add " "


If SourceIntCol(P) = list(Counter) Then
        Set itmx = Alerter.AlerterStatus.ListItems.Add(, , "")
        itmx.SubItems(1) = "Source Interface"

        If DecimalPorts.Checked = True Then
            SixthField = CLng("&H" & (SixthField))
            SeventhField = CLng("&H" & (SeventhField))
        End If

        itmx.SubItems(2) = FirstField
        itmx.SubItems(3) = SecondField
        itmx.SubItems(4) = ThirdField
        itmx.SubItems(5) = FourthField

        If ProtoDec.Checked = True Then
            Call ProtoConvert(FifthItem)
            FifthField = FifthItem
        End If

        itmx.SubItems(6) = FifthField
        itmx.SubItems(7) = SixthField
        itmx.SubItems(8) = SeventhField
        itmx.SubItems(9) = EighthField

        Alerter.AlerterCount.Caption = Alerter.AlerterCount.Caption + 1
        Else
        End If



Next
End If
bluedragon99,
> This is my final code which seems to work great.

Glad it is working.
An allocation of the points will be greatly appreciated!

Dabas
Thanks Dabas!!! as usual you guys are great (thanks for the watch lesson :)