ChrisO.
ChrisO.

Reputation: 47

VB : Parentheses automatically added in error

My colleagues frown upon me because according to our sourcesafe, I added parentheses to the method-property thus resulting in a stackoverflow. Since I'm sure I did no such thing on purpose, I wonder if somehow I used a obscure shortcut which performed these changes.

This kind of stuff :

Public Function Opslaan() As Boolean
    If something_wrong() Then
        opslaan = False
    End If
    If opslaan = False Then
        Do_Something_Else()
    End If 
    Return opslaan
End Function

and this got changed into :

Public Function Opslaan() As Boolean
    If something_wrong() Then
        opslaan = False
    End If
    If opslaan() = False Then  '' See the parentheses here added automatically
        Do_Something_Else()
    end If 

    Return opslaan
End Function

Any help is appreciated.

Upvotes: 2

Views: 219

Answers (1)

JoelC
JoelC

Reputation: 3754

This looks like bad old VB6 code converted to VB.NET.

The problem is VB6 allowed you to treat the current function name as a variable and update its value all throughout the method. Then, when you exited the method whatever value this variable had would be returned to the caller. This syntax is confusing and should never be used, even in VB6 since there is a better way.

Update all code you find like this to something like:

Public Function Opslaan() As Boolean
    Dim result As Boolean = True

    If something_wrong() Then
        result = False
    end if
    If result = False Then
         Do_Something_Else()
    End If 

    Return result 
End Function

This is much clearer code and will never mistakenly call the same routine.

Also, this is totally personal preference, but I prefer to not check booleans with equals statements. So If result = False Then becomes If Not result Then and If result = true Then becomes If result Then. That almost always feels cleaner to me for Booleans.

Upvotes: 2

Related Questions