Implementing IDisposible for my Class

S

Sylvie

Hello,

I have a class, when I use this class like this, using (StockManager StMan =
new StockManager()) { .......... }

it returns "error type used in a using statement must be implicitly
convertible to 'System.IDisposable' "

How can I implement Dispose parts ?

My Class is;
class StockManager

{

public StockDataInterface StockData = null;

public StockManager()

{

StockData = new StockDataInterface();

}


public int CreateStock() { ..... }

}
 
J

Jon Skeet [C# MVP]

I have a class, when I use this class like this, using (StockManager StMan =
new StockManager()) { .......... }
it returns "error type used in a using statement must be implicitly
convertible to 'System.IDisposable' "

How can I implement Dispose parts ?

Let's step back a moment - why do you *want* to use a using statement?
Does your class really *need* to be disposed? Is it meant to release
StockData afterwards?

Jon
 
S

Sylvie

StockData was a sample class,

Any class that created whether using "using" statement or not must be
diposed or equalize to null ?

These two implementation is same ?

1.
StockManager StMan = new StockManager()
try
....
finally
StMan = null;
end

2.
using (StockManager StMan = new StockManager()) { .. }
 
J

Jon Skeet [C# MVP]

StockData was a sample class,

Any class that created whether using "using" statement or not must be
diposed or equalize to null ?

What do you expect setting the variable to null to do? It's not going
to dispose of the object.

The "using" statement is *solely* there to call IDisposable.Dispose in
a finally block.

If you don't need to call Dispose, then don't use a "using" statement.

Jon
 
J

John Duval

StockData was a sample class,

Any class that created whether using "using" statement or not must be
diposed or equalize to null ?

These two implementation is same ?

1.
StockManager StMan = new StockManager()
try
....
finally
StMan = null;
end

2.
using (StockManager StMan = new StockManager()) { .. }





- Show quoted text -

Hi Sylvie,
Actually the equivalent is more along the lines of:

StockManager StMan = new StockManager();
try
{ .... }
finally
{
StMan.Dispose();
}

Which is why StockManager needs to implement IDisposible.

Having said that, make sure you need to implement IDisposible before
you run off and do this. In most cases, you don't need to implement
IDisposible at all. Consider using IDisposible if you need to clean
up unmanaged resources (e.g. window handles) ,or if you need to
guarantee the timing of your object's cleanup (because the garbage
collector doesn't clean up objects right away).

John
 
S

Sylvie

Thanks but,

What s Garbage Collector ?

Thanks again

iletide sunu yazdi said:
StockData was a sample class,

Any class that created whether using "using" statement or not must be
diposed or equalize to null ?

These two implementation is same ?

1.
StockManager StMan = new StockManager()
try
....
finally
StMan = null;
end

2.
using (StockManager StMan = new StockManager()) { .. }

"Jon Skeet [C# MVP]" <[email protected]>, iletide sunu
yazdi

I have a class, when I use this class like this, using (StockManager
StMan =
new StockManager()) { .......... }
it returns "error type used in a using statement must be implicitly
convertible to 'System.IDisposable' "
How can I implement Dispose parts ?
Let's step back a moment - why do you *want* to use a using statement?
Does your class really *need* to be disposed? Is it meant to release
StockData afterwards?
Jon- Hide quoted text -

- Show quoted text -

Hi Sylvie,
Actually the equivalent is more along the lines of:

StockManager StMan = new StockManager();
try
{ .... }
finally
{
StMan.Dispose();
}

Which is why StockManager needs to implement IDisposible.

Having said that, make sure you need to implement IDisposible before
you run off and do this. In most cases, you don't need to implement
IDisposible at all. Consider using IDisposible if you need to clean
up unmanaged resources (e.g. window handles) ,or if you need to
guarantee the timing of your object's cleanup (because the garbage
collector doesn't clean up objects right away).

John
 
J

John Duval

Thanks but,

What s Garbage Collector ?

Thanks again

iletide sunu yazdinews:[email protected]... said:
StockData was a sample class,
Any class that created whether using "using" statement or not must be
diposed or equalize to null ?
These two implementation is same ?
1.
StockManager StMan = new StockManager()
try
....
finally
StMan = null;
end
2.
using (StockManager StMan = new StockManager()) { .. }
"Jon Skeet [C# MVP]" <[email protected]>, iletide sunu
yazdi
I have a class, when I use this class like this, using (StockManager
StMan =
new StockManager()) { .......... }
it returns "error type used in a using statement must be implicitly
convertible to 'System.IDisposable' "
How can I implement Dispose parts ?
Let's step back a moment - why do you *want* to use a using statement?
Does your class really *need* to be disposed? Is it meant to release
StockData afterwards?
Jon- Hide quoted text -
- Show quoted text -
Hi Sylvie,
Actually the equivalent is more along the lines of:
StockManager StMan = new StockManager();
try
{ .... }
finally
{
StMan.Dispose();
}
Which is why StockManager needs to implement IDisposible.
Having said that, make sure you need to implement IDisposible before
you run off and do this. In most cases, you don't need to implement
IDisposible at all. Consider using IDisposible if you need to clean
up unmanaged resources (e.g. window handles) ,or if you need to
guarantee the timing of your object's cleanup (because the garbage
collector doesn't clean up objects right away).
John- Hide quoted text -

- Show quoted text -

Hi Sylvie,
You can probably find a lot of articles that give you an overview of
the .NET garbage collector. But briefly, the garbage collector is the
part of the .NET framework that automatically reclaims the memory for
objects that are no longer in use. The benefit of the garbage
collector is that you don't have to remember to release the memory
every object manually... the framework will see that your object is no
longer in use, and it will do it for you.

I'd recommend that you visit MSDN to get a better overview. Here's
one link:
http://msdn2.microsoft.com/en-us/library/0xy59wtx.aspx

John
 
J

Jon Skeet [C# MVP]

What s Garbage Collector ?

I really, *really* don't want to cause any offense - but now is a good
time to get a book on C# and .NET. If you're not aware of garbage
collection and disposal, then a newsgroup isn't a particularly good
place to get that background information. Newsgroups are great for
very specific questions, but not for picking up basic knowledge. Books
and tutorials are a far better source for that - it's less likely that
you'll miss something important.

I'd put the code down at this point, and start learning about .NET as
a platform and C# as a language.

Jon
 
A

Andrew Faust

The others brought up some good points about you needing to read a book
that covers .Net. These are fundamental issues and if you don't have a good
understanding of them you're going to be running in to constant problems.
However, I'm going to answer your root question so you'll recognize the tie
back to your code while you're doing your reading.

System.IDisposable is an interface. An interface is basically a contract
that your class makes to everything that uses your class. It defines a set
of functionality that your class guarantees it supports. In this case the
IDisposable interface tells everything else that it has a method called
Dispose() that they can call. This Dispose method is typically used to
clean up resources, however, it doesn't actually have to do anything.

You do this by adding it to your class

class StockManager : IDisposable
{
...Your Code...

public override void Dispose()
{
....Your Dispose Code....
}
}

Let me stress. Don't just use this and move on. Go get a book, read it and
make sure you understand class inheritance, interfaces, garbage collection,
etc.
 

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

Top