Skip to main content
deleted 8 characters in body
Source Link
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174
         Try
             If inArray Is Nothing Then
                 Return False
             ElseIf inArray.Length <= 0 Then
                 Return False
             ElseIf inArray(0) Is Nothing Then
                 Return False
             End If
  
         Catch ex As Exception
             FlagEx =Return False
         End Try

         Return True
         Try
             If inArray Is Nothing Then Return False
             If inArray.Length <= 0 Then Return False
             If inArray(0) Is Nothing Then Return False
  
         Catch ex As Exception
             FlagEx =Return False
         End Try

         Return True
         Try
             If inArray Is Nothing 
                 OrElse inArray.Length <= 0 
                 OrElse inArray(0) Is Nothing Then 

                 Return False
             End If
  
         Catch ex As Exception
             FlagEx =Return False
         End Try

         Return True
         Try
             Return (inArray Is Not Nothing 
                 AndAlso inArray.Length > 0 
                 AndAlso inArray(0) Is Not Nothing)

         Catch ex As Exception
             FlagEx =Return False
         End Try
         Try
             If inArray Is Nothing Then
                 Return False
             ElseIf inArray.Length <= 0 Then
                 Return False
             ElseIf inArray(0) Is Nothing Then
                 Return False
             End If
  
         Catch ex As Exception
             FlagEx = False
         End Try

         Return True
         Try
             If inArray Is Nothing Then Return False
             If inArray.Length <= 0 Then Return False
             If inArray(0) Is Nothing Then Return False
  
         Catch ex As Exception
             FlagEx = False
         End Try

         Return True
         Try
             If inArray Is Nothing 
                 OrElse inArray.Length <= 0 
                 OrElse inArray(0) Is Nothing Then 

                 Return False
             End If
  
         Catch ex As Exception
             FlagEx = False
         End Try

         Return True
         Try
             Return (inArray Is Not Nothing 
                 AndAlso inArray.Length > 0 
                 AndAlso inArray(0) Is Not Nothing)

         Catch ex As Exception
             FlagEx = False
         End Try
         Try
             If inArray Is Nothing Then
                 Return False
             ElseIf inArray.Length <= 0 Then
                 Return False
             ElseIf inArray(0) Is Nothing Then
                 Return False
             End If
  
         Catch ex As Exception
             Return False
         End Try

         Return True
         Try
             If inArray Is Nothing Then Return False
             If inArray.Length <= 0 Then Return False
             If inArray(0) Is Nothing Then Return False
  
         Catch ex As Exception
             Return False
         End Try

         Return True
         Try
             If inArray Is Nothing 
                 OrElse inArray.Length <= 0 
                 OrElse inArray(0) Is Nothing Then 

                 Return False
             End If
  
         Catch ex As Exception
             Return False
         End Try

         Return True
         Try
             Return (inArray Is Not Nothing 
                 AndAlso inArray.Length > 0 
                 AndAlso inArray(0) Is Not Nothing)

         Catch ex As Exception
             Return False
         End Try
Source Link
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174

It's not bad. An extension method is a nice choice here. There's no state and you're extending a class that you don't own/can't directly modify. Good design decision. I also like that you're using the Try...Catch paradigm. A common mistake for VB6 devs moving to .Net is carrying over the VB6 error handling pattern.

One thing I don't like about what you've done is this.

Catch ex As Exception

This catches every possible exception in the world, including potentially fatal memory errors. Be as specific as possible when catching exceptions. What specific exception would you expect to happen? Catch that instead, or none at all.

The only other thing I want to mention is that FlagEx is a poor name and the variable itself is kind of useless. Take advantage of the Return statement.

         Try
             If inArray Is Nothing Then
                 Return False
             ElseIf inArray.Length <= 0 Then
                 Return False
             ElseIf inArray(0) Is Nothing Then
                 Return False
             End If
  
         Catch ex As Exception
             FlagEx = False
         End Try

         Return True

Of course, you could take this a step further by doing away with the Elifs.

         Try
             If inArray Is Nothing Then Return False
             If inArray.Length <= 0 Then Return False
             If inArray(0) Is Nothing Then Return False
  
         Catch ex As Exception
             FlagEx = False
         End Try

         Return True

Which leads us to using some short circuiting.

         Try
             If inArray Is Nothing 
                 OrElse inArray.Length <= 0 
                 OrElse inArray(0) Is Nothing Then 

                 Return False
             End If
  
         Catch ex As Exception
             FlagEx = False
         End Try

         Return True

Then flip the condition so we can directly return.

         Try
             Return (inArray Is Not Nothing 
                 AndAlso inArray.Length > 0 
                 AndAlso inArray(0) Is Not Nothing)

         Catch ex As Exception
             FlagEx = False
         End Try