locked
"End Using" Execution After Return In Function RRS feed

  • Question

  • User1649856639 posted

     I have several functions similar to this:

        Public Shared Function RetrieveRecentContracts(ByVal intUpToMonthsOld As Integer, _
                                                       ByVal strDatabaseName As String) As DataSet
            Dim db As Database = DatabaseFactory.CreateDatabase(strDatabaseName)
            Dim ds As DataSet = Nothing
            Using dbCommand As DbCommand = db.GetStoredProcCommand("Contracts.sp_SelectRecentContracts")
                db.AddInParameter(dbCommand, "MonthsOld", DbType.Int32, intUpToMonthsOld)
                ds = db.ExecuteDataSet(dbCommand)
                Return ds
            End Using
        End Function


    From what I gather here: http://msdn.microsoft.com/en-us/library/htd05whh.aspx -- Using and End Using act like a Try, Catch, Finally, where Finally calls the dispose method.

     

    However, when I step through the code, it does not appear to execute the End Using statement after returning the value, versus in a Try, Finally block, I can see it hit Finally and dispose the object.

     

    Does anyone know if End Using gets executed here? 

    Thursday, May 20, 2010 12:32 PM

Answers

  • User1006193418 posted

    Does anyone know if End Using gets executed here?

    No. The End Using will be missed in this case. You may need to use this kind of  code to make sure the command is disposed.

        Public Shared Function RetrieveRecentContracts(ByVal intUpToMonthsOld As Integer, _
                                                       ByVal strDatabaseName As String) As DataSet
            Dim db As Database = DatabaseFactory.CreateDatabase(strDatabaseName)
            Dim ds As DataSet = Nothing
            Using dbCommand As DbCommand = db.GetStoredProcCommand("Contracts.sp_SelectRecentContracts")
                db.AddInParameter(dbCommand, "MonthsOld", DbType.Int32, intUpToMonthsOld)
                ds = db.ExecuteDataSet(dbCommand)
            End Using
            Return ds
        End Function

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 25, 2010 5:33 AM

All replies

  • User-653327211 posted

    Using I would not put in the category of being like a try catch finally block. try catch finally is for exception handling. Using is for memory management. For instance the way you are using this is for a dbcommand. So when the using statement is called it will allocate the memory for that resource however when it hits the End Using statement it will then clean up the dbcommand statement; what it will do is close the connection for you and deallocate the db variable out of memory.

    Thursday, May 20, 2010 12:56 PM
  • User1649856639 posted

    I understand that.

     

    However, I'm not 100% sure if it's actually hitting the "End Using" statement or not.

     

    In Try, Catch Finally, like this:

    Public Shared Function RetrieveContract(ByVal intContractID As Integer, _
                                                      ByVal strDatabaseName As String) As IDataReader
            Dim db As Database = DatabaseFactory.CreateDatabase(strDatabaseName)
            Dim dbCommand = db.GetStoredProcCommand("Contracts.sp_SelectContract")
    
            Try
                db.AddInParameter(dbCommand, "ContractID", DbType.Int32, intContractID)
                Return db.ExecuteReader(dbCommand)
            Catch
                Throw
            Finally
                If Not dbCommand Is Nothing Then dbCommand.Dispose()
            End Try
        End Function

    I can see, as I step through the code, that Finally gets executed and the dispose method is called.

     

    I know that Using/End Using calls the dispose method, however I want to know if it actually happens in the example I originally posted. I certainly don't "see" it happen when stepping through the code.

    Thursday, May 20, 2010 1:46 PM
  • User1006193418 posted

    Does anyone know if End Using gets executed here?

    No. The End Using will be missed in this case. You may need to use this kind of  code to make sure the command is disposed.

        Public Shared Function RetrieveRecentContracts(ByVal intUpToMonthsOld As Integer, _
                                                       ByVal strDatabaseName As String) As DataSet
            Dim db As Database = DatabaseFactory.CreateDatabase(strDatabaseName)
            Dim ds As DataSet = Nothing
            Using dbCommand As DbCommand = db.GetStoredProcCommand("Contracts.sp_SelectRecentContracts")
                db.AddInParameter(dbCommand, "MonthsOld", DbType.Int32, intUpToMonthsOld)
                ds = db.ExecuteDataSet(dbCommand)
            End Using
            Return ds
        End Function

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 25, 2010 5:33 AM
  • User1649856639 posted

    Awesome. I will change my code. Thank you. 

    Tuesday, May 25, 2010 8:56 AM