Disposing

R

Rob

Hi all,

I wrote myself a little function to do my own housekeeping, I can call
it by using

Dispose(object)

Within this function there's a select case statement which will then,
based on some flags and the type of the object do things like close
connections, dispose of the object where appropriate and so on.

Here's my question....

I have various functions where I'll do something like this:

create a connection
create a command
create an adapter
create a datatable

fill the datatable via the adapter

Dispose(adapter)
Dispose(command)
Dispose(connection)

return datatable

Now - this is all well and good - but obviously I cant tidy up my
datatable or else I'd have nothing to return....

I'm sure the .net garbage thingy-majig tages care of it - but in an
anal way I like to do all of my own clearing up....so, how can I change
my functions to tidy up but still return objects? Presumably, I'd have
to have passed the datatable in ByRef in the first place, thus not
needing to dispose of it in the function, and then clear it away when
I'm done with it from the calling code...

Any other ideas?

Thanks in advance for your help...

Rob
 
P

Patrice

Not sure what you are looking for ? You obviously can't clean up an object
before being done with it (keep in mind that objects are just "pointers" if
the thing that make you think this, is to see two variable names for the
*same* object). You'll have to call dispose once you are done with the
object.

Sorry if I missed your point...
 
R

Rob

Patrice said:
Not sure what you are looking for ?

Hi Patrice,

I guess I'm looking for the "best practice" etc...ie, in the example I
posted up, is that pretty much what you would all have as well? If so,
would you then clear down the datatable in the calling code afterwards?
Or do you all just leave it to .net to tidy up for you?

Would I be better of passing in the object ByRef so that it removes
what I see as the "untidyness" of the function, ie, I can now clear
down all objects that function created and be happy, because the other
object was passing in byref, and I know that the calling code will deal
with it.
Sorry if I missed your point...

Not sure that you did - I should have probably just reworded it a bit..

Regards

Rob
 
P

Patrice

It's AFAIK considered best to clean up yourself.

For now my understanding is that you feel uncomfortable about not being able
to clean up the datatable "object" inside your function. Passing the object
byref to a sub won't make any technical difference. Object variables are
"pointers" (that is the location of the object, not "the object itself"). So
your function doesn't hold an additional object but just returns a
"reference" to the object it created. This object will be cleaned up by
using the "reference" returned to your main code.

My personal preference woudl be to use a function for a newly created object
and a sub if I need to perform some processing on an object but could do
before something other than just creating it...


Patrice
 
K

Karl Seguin [MVP]

I don't think your custom Dispose() function is very useful.

If you are using VB.NET 2005, why not use the "using" keyword? If you are
using any version of C#, same question.

Disposing objects is 1 line...calling a method is 1 line. All you are really
gaining, in my opinion, is little LESS readability.

Karl
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top