Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between Dispose And nothing

Tags:

vb.net

 **Public Sub ExecuteQuery(ByVal pQueryString As String, Optional ByVal pConn As Odbc.OdbcConnection = Nothing)

            Dim Mycmd As New Odbc.OdbcCommand(pQueryString, MyConn)
            Mycmd.ExecuteNonQuery()
            Mycmd.Dispose()

    End Sub**

Here I am Clear the object using Dispose( Mycmd.Dispose()). Can I Use here Nothing ( Mycmd = Nothing? . Which is the Best ?

Please Help Me Sir,

By

Arul.

like image 726
Arul Avatar asked Dec 16 '22 16:12

Arul


1 Answers

Dim Mycmd As New Odbc.OdbcCommand(pQueryString, MyConn)

This command stores the reference of object created by New Odbc.OdbcCommand(pQueryString, MyConn) into Mycmd, i.e Mycmd would basically have the address of the newly created object.

now when you do

Mycmd.Dispose()

then it indicates that the use of that newly created object is over and the space allocated to that object can be freed during garbage collection.

but when you do

Set Mycmd = Nothing

then it just remove the reference of the newly created object from Mycmd, it does not mark it for garbage collection.

like image 148
Sumit Avatar answered Dec 21 '22 22:12

Sumit