473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using IDisposable

I have Class A that implements IDisposable, within this class i have method A that creates a new Sqlconnection object and execute some stored proc, in Class B, I create an instance of Class A object with the using statement..
my question is when my using statement ends Class A's dipose method should be called i think.. but will my Sqlconnection object within Class A Method A be disposed automatically as well?
Nov 15 '05 #1
6 1942
The Dispose method is not called automatically. You should call it at the
point that you wish to dispose the class's resources.

--

Lynn Harrison
SHAMELESS PLUG - Introduction to 3D Game Engine Design (C# & DX9)
www.forums.Apress.com

"newbie" <an*******@disc ussions.microso ft.com> wrote in message
news:EC******** *************** ***********@mic rosoft.com...
I have Class A that implements IDisposable, within this class i have method A that creates a new Sqlconnection object and execute some stored
proc, in Class B, I create an instance of Class A object with the using
statement... my question is when my using statement ends Class A's dipose method should

be called i think.. but will my Sqlconnection object within Class A Method A
be disposed automatically as well?
Nov 15 '05 #2
If you want the SqlConnection object to be destroyed when class A is
destroyed, two things need to happen.

First you need to code it yourself in the Dispose method. Something like...
public void Dispose()
{
cnn = null;
}

Second, class B must invoke the Dispose method. Something like...
a.Dispose;

Another option - You can also just forget about IDispsable and the Dispose
method, and instead, just destroy class A. That will automatically make the
connection object go out of scope and the CLR will get rid of it. But it is
good style to use Dispose so that consumers know that they can release
expensive resources when they are done with your object.
Nov 15 '05 #3
Marty McDonald <mc******@wsdot .wa.gov> wrote:
If you want the SqlConnection object to be destroyed when class A is
destroyed, two things need to happen.

First you need to code it yourself in the Dispose method. Something like...
public void Dispose()
{
cnn = null;
}


That doesn't destroy the connection - given that something is disposed,
it's usually going out of scope anyway. You should instead call Dispose
on the connection, and follow the MS guidelines for writing Dispose
methods and finalizers:

http://tinyurl.com/2k6e

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Actually, newbie is using a using statement. If you use "using" with a
class that implements IDisposable, Dispose() will be called automatically
when the using block is exited.


"Lynn Harrison" <lh*******@dvc4 00.com.NOSPAM> wrote in message
news:uF******** ******@TK2MSFTN GP11.phx.gbl...
The Dispose method is not called automatically. You should call it at the
point that you wish to dispose the class's resources.

--

Lynn Harrison
SHAMELESS PLUG - Introduction to 3D Game Engine Design (C# & DX9)
www.forums.Apress.com

"newbie" <an*******@disc ussions.microso ft.com> wrote in message
news:EC******** *************** ***********@mic rosoft.com...
I have Class A that implements IDisposable, within this class i have method A that creates a new Sqlconnection object and execute some stored
proc, in Class B, I create an instance of Class A object with the using
statement...
my question is when my using statement ends Class A's dipose method

should be called i think.. but will my Sqlconnection object within Class A Method A be disposed automatically as well?

Nov 15 '05 #5
A little more to it than this: You can't deterministical ly destroy an
object in managed code, that's handled by the garbage collector, so this:
public void Dispose()
{
cnn = null;
}
Does not ensure that cnn is destroyed immediately, it merely becomes
available for collection. Further, it's not really necessary to manually
set the reference to null unless the object that owns cnn is going to be
referenced longer than cnn.

Once all references go away, the garbage collector will destroy the
connection when it gets around to it -- and that can be quite a while,
depending upon what generation cnn is in; I've seen gen2 objects stick
around for an hour after all references go away. You use Dispose to ensure
that any expensive resources that the object is holding on to are released
immediately (usually these are unmanaged resources), rather than waiting for
the object to be collected.


"Marty McDonald" <mc******@wsdot .wa.gov> wrote in message
news:OU******** ******@TK2MSFTN GP09.phx.gbl... If you want the SqlConnection object to be destroyed when class A is
destroyed, two things need to happen.

First you need to code it yourself in the Dispose method. Something like... public void Dispose()
{
cnn = null;
}

Second, class B must invoke the Dispose method. Something like...
a.Dispose;

Another option - You can also just forget about IDispsable and the Dispose
method, and instead, just destroy class A. That will automatically make the connection object go out of scope and the CLR will get rid of it. But it is good style to use Dispose so that consumers know that they can release
expensive resources when they are done with your object.

Nov 15 '05 #6
If you want to have Class A dispose of the connection when it is disposed:
(and you want to make the dispose kind of deterministic for your using
clause):

public class A : IDisposable
{
private Connection cnn = new Connection();
public void Dispose()
{
// bear in mind that once you do this, cnn is useless, so it's
probably
// a bad thing if your consumer tries to use class A after Dispose
has been called
cnn.Dispose();
}
}

Now if Dispose is called, you can clean up early, otherwise the GC will take
care of things in the normal course.

One thing NOT to do: Don't use a Finalizer (Destructor in C# syntax)!

These are bad. Really bad. You only need to use one of these things if you
are directly holding an unmanaged resource (like a window handle, or a win32
file handle or something like that).
"newbie" <an*******@disc ussions.microso ft.com> wrote in message
news:EC******** *************** ***********@mic rosoft.com...
I have Class A that implements IDisposable, within this class i have method A that creates a new Sqlconnection object and execute some stored
proc, in Class B, I create an instance of Class A object with the using
statement... my question is when my using statement ends Class A's dipose method should

be called i think.. but will my Sqlconnection object within Class A Method A
be disposed automatically as well?
Nov 15 '05 #7

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

Similar topics

26
448
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of scope, the dtor could be immediately be called (no GC needed). For that, you don't have to declare the whole File class as a struct (which would be not good for performance when File has a lot of data-members). Instead creating a thin wrapper...
7
3016
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the "Close()" methods reared their ugly (at least it would seem...)heads. I was happily delving away in the .NET framework, investigating the stream classes with the msdn and Lutz Roeder's .NET reflector, when I stumbled upon the following:
3
2143
by: Razzie | last post by:
Hi, I know that as a general rule, whenever your class contains members that implicitly or explicitly implement IDisposable, your class should too. However, does it count when my class uses a using claus? class A { private void doSomething() {
2
1119
by: Per Rollvang | last post by:
Hi all! I have created a few classes that inherits the IDisposable-interface, and if I don't use them in this fashion using(bla bla bla) { // do stuff }
8
3707
by: J-T | last post by:
I have a class like below I have a couple of questions about that: 1) I like to use "Using statement" when creating an object of this class,so I had to implement IDisposable.Am I doing this right here? 2) Do I have to be worried about those objects I have created in GetPackageNames() ?? for instance making them null afterward or something? 3) This class is in my business layer As you can see I'm using Microsoft.SQLServer.DTSPkg80...
5
5608
by: Andreas Müller | last post by:
Hi, I was wondering, if there is something similar in VB.NET like the using statement in C#. What it does is to automatically call Dispose on the object decrared with in the statement when the block exits. using (MyBoj) { }// MyObj.Dispose is called
3
3492
by: Dave | last post by:
I trying to determine the best pattern for designing my business and data layers... Can the instance of the business object eventually cause memory leaks in Example 1? If your business class doesn't implement IDisposable and falls out scope, does it eventually get cleaned up by the GC or should it be set to NULL? If it can cause leaks, should the IDisposable always be implemented at a base class level as a best practice?
11
2322
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or not as a general rule, or only under certain circumstances... Since it's an easy enough thing to do (http://msdn2.microsoft.com/en-us/library/system.idisposable.aspx), is there any good reason not to make every custom class IDisposable, the same...
5
7147
by: Hillbilly | last post by:
MSDN Remarks "as a rule" the using statement should be used when instantiating objects which inherit IDisposable. Other than the obvious unmanaged objects like the file system example, fonts and the database how else may it be determined which classes and their objects inherit IDisposable? And the remarks imply the using statement is a different and perhaps more efficient way to use objects than try-catch-finally? ...
3
1344
by: Paul | last post by:
Hi all, I currenty have a datalayer and have decided to impliment IDisposable with it. The data layer contains a number of objects which I can call dispose on, but i'm not too sure if you need to worry about strings. In the dispose method, is it best to set these to "", or set them to null? Also, I have an List<Tobject, should this be cleared and set to null? I usually clean things up myself, but figured for a datalayer, IDisposable...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.