开发者

Difference Between Dispose And nothing

 **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.


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.


Oftentimes, .net objects will ask other entities (which may or may not even be on the same computer) to "do something"(*) on their behalf, and such entities will continue doing so unless or until they are told to stop. Such objects should implement IDisposable, and their IDisposable.Dispose routine should notify any and all entities which had been doing something on their behalf, to stop doing so. If all references to an IDisposable object were to disappear without Dispose being called first, some other entities might continue forever in uselessly doing something on behalf of an object which has long since ceased to exist.

(*) That "doing something" could be anything, including blocking other requests to do something. For example, an object might ask the operating system for exclusive access to a file, and the operating system might relay that request to another computer. If the object doesn't notify the OS when it no longer needs access to the file, the server could leave everyone else in the universe locked out of the file indefinitely.

In an effort to minimize problems from entities' perpetually acting on behalf of abandoned objects, .net provides a means by which objects can ask to be notified when they're abandoned. If an object which overrides Object.Finalize() is abandoned, then .net will usually call that object's override of the Finalize() method. This kinda sorta works, mostly, but it should almost never be relied upon. It's very hard to design a class so that Finalize() will always do the right thing and never do the wrong thing. Among other things, if one isn't careful, it's possible for .net to call Finalize() on an object which it determines is going to be abandoned, even while that object is interacting with an outside entity. This will never happen in code which properly calls Dispose on the object, but may happen in code which relies upon Finalize().


If you mean assigning the value Nothing to the object as below:

Set Mycmd = Nothing

This doesn't actually do anything in terms of signaling as object ready for garbage collection, or freeing an object's used resources.

In VB6 setting an object to equal Nothing was the correct way to free the object's resources, but now calling the Dispose method is correct. Where objects do not implement IDisposable, then you can simply leave them.

Garbage collection will then happen in its own time (and would have even without the call to .Dispose() ).


There is not any .nothing() method to release object resource. You can use dispose and nothing to release object resources. But dispose is used when IDisposable interface is implemented. If you are using your custom classes. The .net built in object already has this functionality but it works if your object is having referenced otherwise it will give an error. You can use

command=nothing

for all objects either .net framework or custom. Both release object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜