By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

using statement in C#

P: n/a
J-T
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 namespace and in my only methodI am returning
an object of Type "PackageInfos".This means that in my presenation layer I
need to have this name space to otherwise the output object of this method
won;t be recognized. Is it a right thing to do? or I should change something
in my Code?
I so appreciate youe help.

Thanks

using System;
using Microsoft.SQLServer.DTSPkg80;

namespace AIMS.Business.ClassFiles.DTS
{

public class DTSIntrop : IDisposable
{

protected string databaseServer;

public DTSIntrop(string databaseServer)
{
this.databaseServer=databaseServer;

}

public PackageInfos GetPackageNames()
{
ApplicationClass application = new ApplicationClass();
PackageSQLServer pkgSQLServer =
application.GetPackageSQLServer(this.databaseServe r,"","",DTSSQLServerStorageFlags.DTSSQLStgFlag_Use TrustedConnection);
PackageInfos pkgInfos = pkgSQLServer.EnumPackageInfos("", false, "");

return pkgInfos;
}
#region IDisposable Members

public void Dispose()
{
this.Dispose();
}

#endregion
}
}
Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
J-T <J-*@nospam.com> wrote:
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?
Yes, but only if you really have anything to dispose of. In this case,
your implementation of Dispose would just cause a recursive call,
blowing up with a stack overflow. Why do you feel you need to implement
IDisposable in the first place?
2) Do I have to be worried about those objects I have created in
GetPackageNames() ?? for instance making them null afterward or something?
Setting local variables to be null is generally useless. However, you
should look at whether any of the objects you use there need disposing.
3) This class is in my business layer As you can see I'm using
Microsoft.SQLServer.DTSPkg80 namespace and in my only methodI am returning
an object of Type "PackageInfos".This means that in my presenation layer I
need to have this name space to otherwise the output object of this method
won;t be recognized. Is it a right thing to do? or I should change something
in my Code?


Yes, that should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
J-T
Hey Jon,
Nice to have your reply:-)
Why do you feel you need to implement
IDisposable in the first place?
If you look at the code I am using a RCW around a com interface
(Microsoft.SQLServer.DTSPkg80).In one of the methods as you see I am using
ApplicationClass application = new ApplicationClass();

PackageSQLServer pkgSQLServer =
application.GetPackageSQLServer(this.databaseServe r,"","",DTSSQLServerStorageFlags.DTSSQLStgFlag_Use TrustedConnection);

PackageInfos pkgInfos = pkgSQLServer.EnumPackageInfos("", false, "");

so I thought it is not a bad idea to dispose them some how and then I
realized that none of them expose Dispose method so I can call it in my
class's dispose method (probably because they are all unmanaged).
In the mean time in my UI code I wanted to use "Using statement" to create
an instance of my object and I had to implement IDisposable in my class,well
implementing IDisposable needs to have a stub ,right? so that was the
feeling.
Thanks Jon for your nice attention to everything as awlays;-)

Ray

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... J-T <J-*@nospam.com> wrote:
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?


Yes, but only if you really have anything to dispose of. In this case,
your implementation of Dispose would just cause a recursive call,
blowing up with a stack overflow. Why do you feel you need to implement
IDisposable in the first place?
2) Do I have to be worried about those objects I have created in
GetPackageNames() ?? for instance making them null afterward or
something?


Setting local variables to be null is generally useless. However, you
should look at whether any of the objects you use there need disposing.
3) This class is in my business layer As you can see I'm using
Microsoft.SQLServer.DTSPkg80 namespace and in my only methodI am
returning
an object of Type "PackageInfos".This means that in my presenation layer
I
need to have this name space to otherwise the output object of this
method
won;t be recognized. Is it a right thing to do? or I should change
something
in my Code?


Yes, that should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3

P: n/a
J-T wrote:
I have a class like below I have a couple of questions about that:

As Jon has already said and also..
IDisposable is usually only really necessary when you need to clean up
external connections. Close filestreams, close database connections,
whatever else.

HTH
JB

<snip>
Nov 17 '05 #4

P: n/a
J-T
Am I right in this statement?

Calling Dispose() method of a managed object dosen't mean that the object is
destroyed (reclaimed by Garbage collector),but this method is used to relase
manager and unmanaged resources as soon as you want .If we don;t do this ,it
would be done through "object finalization" and right before the object is
recliamed by Garbage collector.

I hope I'm right in the above senetence:-)

"J-T" <J-*@nospam.com> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...
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 namespace and in my only methodI am returning
an object of Type "PackageInfos".This means that in my presenation layer I
need to have this name space to otherwise the output object of this method
won;t be recognized. Is it a right thing to do? or I should change
something in my Code?
I so appreciate youe help.

Thanks

using System;
using Microsoft.SQLServer.DTSPkg80;

namespace AIMS.Business.ClassFiles.DTS
{

public class DTSIntrop : IDisposable
{

protected string databaseServer;

public DTSIntrop(string databaseServer)
{
this.databaseServer=databaseServer;

}

public PackageInfos GetPackageNames()
{
ApplicationClass application = new ApplicationClass();
PackageSQLServer pkgSQLServer =
application.GetPackageSQLServer(this.databaseServe r,"","",DTSSQLServerStorageFlags.DTSSQLStgFlag_Use TrustedConnection);
PackageInfos pkgInfos = pkgSQLServer.EnumPackageInfos("", false, "");

return pkgInfos;
}
#region IDisposable Members

public void Dispose()
{
this.Dispose();
}

#endregion
}
}

Nov 17 '05 #5

P: n/a
J-T <J-*@nospam.com> wrote:
Why do you feel you need to implement
IDisposable in the first place?
If you look at the code I am using a RCW around a com interface
(Microsoft.SQLServer.DTSPkg80).In one of the methods as you see I am using
ApplicationClass application = new ApplicationClass();

PackageSQLServer pkgSQLServer =
application.GetPackageSQLServer(this.databaseServe r,"","",
DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedC onnection);

PackageInfos pkgInfos = pkgSQLServer.EnumPackageInfos("", false, "");


Yes, but they're all local variables, so you can dispose of them in the
medthod.
so I thought it is not a bad idea to dispose them some how and then I
realized that none of them expose Dispose method so I can call it in my
class's dispose method (probably because they are all unmanaged).
In the mean time in my UI code I wanted to use "Using statement" to create
an instance of my object and I had to implement IDisposable in my class,well
implementing IDisposable needs to have a stub ,right? so that was the
feeling.


The using statement is a way of making it easy to automatically call
Dispose - in other words, unless something implements IDisposable,
there's no reason why you'd *want* to use it. You shouldn't be
implementing IDisposable just to be able to have a using statement.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

P: n/a
J-T <J-*@nospam.com> wrote:
Am I right in this statement?

Calling Dispose() method of a managed object dosen't mean that the object is
destroyed (reclaimed by Garbage collector),but this method is used to relase
manager and unmanaged resources as soon as you want .If we don;t do this ,it
would be done through "object finalization" and right before the object is
recliamed by Garbage collector.

I hope I'm right in the above senetence:-)


Well, you rarely need to release managed resource that way (and indeed
you can't actually free managed resources like that - you can just
release a reference that your instance might have), but that's pretty
accurate for unmanaged resources. (The unmanaged resource will only be
released by finalization if a finalizer releases it though - it doesn't
automatically happen. Fortunately, you'll probably find you very rarely
need to *directly* have unmanaged resources - usually there's a wrapper
with a finalizer in the way anyway.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

P: n/a
J-T
> Yes, but they're all local variables, so you can dispose of them in the
medthod.
You mean by relasing the reference (setting them to null)?
pkgInfos=null;
Thanks Jon
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... J-T <J-*@nospam.com> wrote:
>Why do you feel you need to implement
> IDisposable in the first place?


If you look at the code I am using a RCW around a com interface
(Microsoft.SQLServer.DTSPkg80).In one of the methods as you see I am
using
ApplicationClass application = new ApplicationClass();

PackageSQLServer pkgSQLServer =
application.GetPackageSQLServer(this.databaseServe r,"","",
DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedC onnection);

PackageInfos pkgInfos = pkgSQLServer.EnumPackageInfos("", false, "");


Yes, but they're all local variables, so you can dispose of them in the
medthod.
so I thought it is not a bad idea to dispose them some how and then I
realized that none of them expose Dispose method so I can call it in my
class's dispose method (probably because they are all unmanaged).
In the mean time in my UI code I wanted to use "Using statement" to
create
an instance of my object and I had to implement IDisposable in my
class,well
implementing IDisposable needs to have a stub ,right? so that was the
feeling.


The using statement is a way of making it easy to automatically call
Dispose - in other words, unless something implements IDisposable,
there's no reason why you'd *want* to use it. You shouldn't be
implementing IDisposable just to be able to have a using statement.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #8

P: n/a
J-T
>indeed you can't actually free managed resources like that - you can just
release a reference that your instance might have
for instance If you close a dataReader (managed resource) or a connection
(managed resource) ,dosen;t it mean relasaing the resource?

My second problem is that I cann't underestand the precedence between
Object.Dispose() and Object.Finalize() ? which one is called first?
Is this true to say that we can never destroy object in C# ? we can only
relase the references to it ,right?

Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... J-T <J-*@nospam.com> wrote:
Am I right in this statement?

Calling Dispose() method of a managed object dosen't mean that the object
is
destroyed (reclaimed by Garbage collector),but this method is used to
relase
manager and unmanaged resources as soon as you want .If we don;t do this
,it
would be done through "object finalization" and right before the object
is
recliamed by Garbage collector.

I hope I'm right in the above senetence:-)


Well, you rarely need to release managed resource that way (and indeed
you can't actually free managed resources like that - you can just
release a reference that your instance might have), but that's pretty
accurate for unmanaged resources. (The unmanaged resource will only be
released by finalization if a finalizer releases it though - it doesn't
automatically happen. Fortunately, you'll probably find you very rarely
need to *directly* have unmanaged resources - usually there's a wrapper
with a finalizer in the way anyway.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.