Will expressions or expressions be executed after return statement in VB.net?

Well, I have taken root in one of our enterprise applications, which is made on VB.net. I am not familiar with VB.net (I am doing stuff in C #), so I ask this question: Does the code execute after the cleanup comment?

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 

This is my understanding, as soon as you return, you will again give call control. Is this a VB.Net oddity that I have to accept or a giant WTF?

+4
source share
6 answers

Statements after the cleanup comment will not be executed. This is a candidate for the app in Try / Catch / finally.

+8
source

If you did not specify any control logic in your example

+1
source

The code should be (cleanup) wrapped inside the finally statement using a try catch exception:

pseudo:

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

Is it possible that it will work anyway ... maybe ... I used VB.net to record, and quite a while has passed, but I remember such oddities. I can’t give you the right answer, as it was a very bad practice. What you can do is clear it and set some breakpoints in our code and debug it. See if the code returns to it ...

+1
source

Short answer: the code below the return will never be executed.

This is like translated code. Like someone took a C # fragment from the Internet and tried to write it to VB for VS 2003 (before VB supported the USING statement, but while C # did).

where MySqlConnection and MySqlCommand are new, should be placed in USE blocks, and Dispose () lines converted to END USING.

If possible, use USING over TRY / FINALLY to ensure that IDisposable objects are cleaned.

 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 also use this template for SqlTransactions. (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.

+1
source

This is a giant WTF for me and a very complicated thing that you can skip, as a rule, viewing peer code can catch this, I don’t understand why the return from the function is placed earlier before cleaning. This could be done during code testing, where the programmer wanted to check the function first, and decided to ignore the cleaning code, quickly returning from it, I suspect that the cleaning code may be long, i.e. perhaps it throws an exception that does not get properly and wants to ignore it, so you need to return immediately ... This is (un) a deliberate side effect of introducing a leak, since the resource is not being cleaned properly, thereby masking the real problem ... that I I take it upon myself.

Hope this helps, Regards, Tom.

0
source

The code presented will do nothing after the return statement. VB.NET and C # are similar in this order.

Perhaps this code was written by the VB6 programmer, trusting the old paradigms, or perhaps it was the work of the update tool, porting the code from VB6 to VB.NET.

0
source

Source: https://habr.com/ru/post/1299760/


All Articles