using statement in C#

  • Thread starter Thread starter J-T
  • Start date Start date
J

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.databaseServer,"","",DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection);
PackageInfos pkgInfos = pkgSQLServer.EnumPackageInfos("", false, "");

return pkgInfos;
}
#region IDisposable Members

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

#endregion
}
}
 
J-T said:
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.
 
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.databaseServer,"","",DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection);

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
 
J-T said:
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>
 
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 said:
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.databaseServer,"","",
DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection);

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.
 
J-T said:
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.)
 
Yes, but they're all local variables, so you can dispose of them in the

You mean by relasing the reference (setting them to null)?
pkgInfos=null;


Thanks Jon
 
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
 
J-T said:
You mean by relasing the reference (setting them to null)?
pkgInfos=null;

No, that doesn't do anything significant. You dispose of them by either
calling Dispose on them explicitly, or using a "using" statement.
 
J-T said:
for instance If you close a dataReader (managed resource) or a connection
(managed resource) ,dosen;t it mean relasaing the resource?

The connection itself isn't a managed resource. It's an unmanaged
resource wrapped in a managed resource.
My second problem is that I cann't underestand the precedence between
Object.Dispose() and Object.Finalize() ? which one is called first?

The finalizer is called, which will typically call Dispose. The CLR
itself doesn't decide to call Dispose - you have to write the finalizer
to do it (or inherit one).
Is this true to say that we can never destroy object in C# ? we can only
relase the references to it ,right?

Yes.
 
I tried to call the dispose on application variable ,but it is not exposed
..There is a wrapper around it but I have no idea why it is not exposed?

I cannot use "Using statement" as it says the ApplicationClass has not
implementd IDisposable:-(



public PackageInfos GetPackageNames(bool returnLastVersion)

{

ApplicationClass application = new ApplicationClass()

PackageSQLServer pkgSQLServer =
application.GetPackageSQLServer(this.databaseServer,"","",DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection);

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

return pkgInfos;


}



Thanks Jon
 
J-T said:
I tried to call the dispose on application variable ,but it is not exposed
.There is a wrapper around it but I have no idea why it is not exposed?

I cannot use "Using statement" as it says the ApplicationClass has not
implementd IDisposable:-(

Well, if it doesn't implement IDisposable, then presumably you don't
need to dispose it.
 

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