开发者

Will statements or expressions execute after a return statement in VB.net?

Ok, I was rooting around in one of our company apps which is done in VB.net. I'm not familiar with VB.net (I do 开发者_StackOverflow社区stuff in C#) so I'm asking this question: Does the code after the clean up comment execute?

Public Function DoesUserHavePermission(ByVal UserID As Integer, ByVal ActionID As Integer) As Boolean
    ' some extra code re: getting data


     Return UserHasPermission

     '-Clean Up-
     MySqlCommand.Dispose()
     MySqlConnection.Dispose()
     RowCount = Nothing


End Function

It is my understanding once you say return, you give the calling function control again. Is this a VB.Net oddity which I have to accept or a giant WTF?


The statements after the Clean up comment will not execute. This is a candidate for enclosure within Try/Catch/Finally.


Not unless there's some control logic you omitted in your example


The code should be (the clean up that is) wrapped inside of a finally statement by using a try catch exception:

pseudo:

try
  'make conn
catch exception
finally
mysqlCmd.Dispose
....
end try

Is it possible it will still run..possibly...I used to write VB.net and it has been quite some time but I do remember oddities like that. I can't give you a sure answer as this is / was very bad practice. What you can do is clean it up and set some break points in y our code and debug. See if the code comes back to it...


Short answer: The code below the return will never execute.

That looks like translated code. Like someone took a C# snippet from the web and tried to write it in VB for VS 2003 (before VB supported the USING statement, but while C# did).

where the MySqlConnection and MySqlCommand are new'd up should be put in USING blocks and the Dispose() lines turned into END USING.

Where possible, use USING over TRY/FINALLY to ensure cleanup of IDisposable objects.

using mySqlConnection as New SqlConnection(connectionString)
    mySqlConnection.Open
    using mySqlCommand as New SqlCommand(commandString, mySqlConnection)
       'do something that may fail'

       return UserHasPermission

    end using 'disposes mySqlCommand'
end using 'closes/disposes mySqlConnection'

You can use this pattern for SqlTransactions too. (Place after mySqlConnection.Open)

using myTerribleVariableName as SqlTransaction = mySqlConnection.BeginTransaction
    'do something that may fail - multiple SqlCommands maybe'
    'be sure to reference the transaction context in the SqlCommand'

    myTerribleVariableName.Commit
end using 'disposes. rollsback if not commited'

Oh, and you can delete the RowCount = Nothing statement.


That to me is a giant WTF and a very difficult thing to miss, normally peer code review can catch that, I do not understand why a return from the function is placed earlier on prior to the cleanup. That could have been done during the testing of the code where the programmer was wanting to test the function first and decided to ignore the cleanup code by returning from it quickly enough, I suspect the cleanup code could be lengthy, ie. maybe it is throwing an exception that is not properly caught and wanted to ignore it hence a need to return immediately...That is an (un) intentional side effect of introducing a leak as the resource is not cleaned up properly thereby masking the real problem...that's my take on it.

Hope this helps, Best regards, Tom.


The code as presented will not do anything after the return statement. VB.NET and C# are similar in that fashion.

Its possible that this code was written by a VB6 programmer, trusting in old paradigms, or possibly this was the work of the upgrade tool, porting code from VB6 to VB.NET.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜