Check value of variable

SIMIN

Regular
Joined
Mar 10, 2008
Messages
92
Visual Basic:
Private ReportLog As StringBuilder

Private Sub DoThat()

    'We didn't assign any value to the ReportLog so here we want to check if it's empty or not?
    If ReportLog IsNot Nothing And ReportLog.Length > 0 Then
        '
    End IF

End Sub
The problem is that "If ReportLog IsNot Nothing And ReportLog.Length > 0 Then" itself throws an exception!

A first chance exception of type 'System.NullReferenceException' occurred in MyApp.exe
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

How should I check that variable?
Thank you :)
 
For .Net 2 and higher
Visual Basic:
If ReportLog IsNot Nothing AndAlso ReportLog.Length > 0 Then

otherwise you would need to use a couple of If statements nested
Visual Basic:
If ReportLog IsNot Nothing Then
    If ReportLog.Length > 0 Then
        'do stuff here
 
Back
Top