473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Disposing

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

Jan 11 '07 #1
4 1374
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...
"Rob" <ba*********@googlemail.coma écrit dans le message de news:
11**********************@77g2000hsv.googlegroups.c om...
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

Jan 11 '07 #2
Rob
"Patrice" wrote:
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

Jan 11 '07 #3
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

"Rob" <ba*********@googlemail.coma écrit dans le message de news:
11**********************@i39g2000hsf.googlegroups. com...
"Patrice" wrote:
>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

Jan 11 '07 #4
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Rob" <ba*********@googlemail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
"Patrice" wrote:
>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
Jan 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
4
by: Dakkar | last post by:
I have a program with windows forms and after execution of my program im making it invisible for working background progress and i have a dispose function like this protected override void...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
0
by: Gman | last post by:
Hi, Objective: Draw a grid on a bitmap and set this as a panel's image. (Rather than draw the grid directly on the panel and redraw repeatedly in the paint event.) Problem: It works fine....
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
4
by: Peter Webb | last post by:
I am supposed to manually dispose of some instances, such as Brushes, right? I have a couple of questions: 1. I have the following code, and it works just fine: ...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.