Is this .Dispose worth doing?

  • Thread starter Thread starter Dean Slindee
  • Start date Start date
D

Dean Slindee

After finishing with an access of a database table, I have been setting the
data adapter, command, and connection objects to Nothing. Is it worth
following the Nothing commands with Dispose commands as well, like this:

Finally

cn.Close()

da = Nothing

cmd = Nothing

cn = Nothing

da.Dispose()

cmd.Dispose()

cn.Dispose()

End Try

Thanks,

Dean Slindee
 
Dean Slindee said:
After finishing with an access of a database table, I have been setting
the
data adapter, command, and connection objects to Nothing. Is it worth
following the Nothing commands with Dispose commands as well, like this:

Finally

cn.Close()

da = Nothing

cmd = Nothing

cn = Nothing

da.Dispose()

cmd.Dispose()

cn.Dispose()

End Try

Are 'cn' etc. local variables or class-level variables? If they are local
variables, setting them to 'Nothing' doesn't make much sense and can be
counter-productive. In addition to that, you'll have to dispose the objects
before setting them to 'Nothing', otherwise a 'NullReferenceException' will
be thrown when attempting to access the 'Dispose' method. Calling a
connection's 'Dispose' method will implicitly close it.
 
Herfried just curious in wich way might setting object pointers to Nothing
be counter productive ? ( just curious )


As i am from origin a clean VB6 COM programmer i still cleanup my own mess
( as i also still program with VB6 this is something i do automaticly )
i even found once a article on MSDN wich stated that setting object pointers
to Nothing should help the GC ( honestly need to say that i can not
remember them mentioning anything about the scope )

And yes i do this with class level and local vars

I think Dean is on the right track if he works by the CDN rule ( Close
Dispose Nothing )

regards

Michel Posseth [MCP]
 
Micheal,

I never make a command or things like that global. Therefore the command
goes out of scope at the end of the method. Why would I do an instruction
extra to set it to nothing.

When you have set it global and declare it in your routine as new, it can
have a function. However than you can for the same sake declare it
completely inside the method when you want to set it to nothing at the end
of that.

The dispose is inherited from component. From that most classes inherit as
they forever inherit from object. In both are methods that you never or
seldom use.

Except of course when they are overridden with an extra function, by
instance the connection.dispose which (can be) a special kind of close
function.

I hope this gives some idea's,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top