Re: Implementing IDisposible for my Class

Tech-Archive recommends: Speed Up your PC by fixing your registry



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.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


"Sylvie" <sss@xxxxxx> wrote in message news:Ossc%23MTHIHA.3600@xxxxxxxxxxxxxxxxxxxxxxx
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() { ..... }

}



.



Relevant Pages

  • Re: to dispose or not ?
    ... If someone calls a custom method Dispose and even bether is filling the ... As said before by me and others the Idisposable interface is a contract, ... Dim intCount As Integer ... Dim entry As New DirectoryServices.DirectoryEntry ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How can I call base interface method?
    ... it means you can only inherit one implementation. ... By definition, it is an interface, not an implementation. ... Hence, in your derived class, you can have the Dispose() method. ... public virtual void Dispose() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: to dispose or not ?
    ... Dispose is not unlike any other property or method in that regard. ... Well i guess you both forget that the idisposable interface only has one ... As said before by me and others the Idisposable interface is a contract ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Memory Leak Experts!!!!
    ... dispose would get rid of the object but the reference would still exist. ... interface implementation has interfered with the normal function of the GC. ... Deterministic disposing or implementing the IDispose pattern is only a ... >I think that you are confusing Dispose with Finalise. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Issue: Queued Component and Marshal.ReleaseComObject
    ... Originally I have my interface inherit the IDisposable and I got ... up the ideal to explicitly define the Dispose() method in the interface, ... > void DisplayMessage; ... > public void DisplayMessage(string msg) ...
    (microsoft.public.dotnet.framework.interop)