0

I am trying to create a disposable ADOX Catalog instance by implementing IDisposable interface but I am getting a Error which is: ADOX.Catalog' does not contain a definition for 'Dispose' and no extension method 'Dispose' accepting a first argument of type 'ADOX.Catalog' could be found (are you missing a using directive or an assembly reference?)

and this my code

namespace Disposable
{
class DBGen : IDisposable
{
    Catalog cat; 
    public DBGen()
    {
        cat = new Catalog();
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\AccessDB\\NewMDB.mdb;" +"Jet OLEDB:Engine Type=5");
        Console.WriteLine("Database Created Successfully");
        cat = null;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool b)
    {
        if (b)
        {
            cat.Dispose();
        }
    }
}

}

I am getting the error at

   cat.Dispose();

can you please let me know why this is happening? Thansk

Suffii
  • 5,694
  • 15
  • 55
  • 92

1 Answers1

0

The field cat seems to be set null right in the constructor so it's null all the time. Try to dispose 'cut' before you set it to null.

If you are going to use cat somwhere else in your class you should remove cat=null; from constructor and rewrite dispose like this:

public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool b)
    {
        if (b)
        {
            if (cat!=null) {
                var disposableCat = cat as IDisposable;
                if (disposableCat != null) {
                   disposableCat.Dispose();
                   cat=null;
                }
            }
        }
    }
George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • well, I already tried that but same issue! I am wondering how come I can't call the Dispose() while I am implementing the : IDisposable interface? – Suffii Jul 21 '12 at 06:25
  • Thanks Achitaka, I tried your code but the problem still remained! I am getting same error – Suffii Jul 21 '12 at 06:36
  • Sometimes classes implement `IDisposable` explicitly. In this cases they must be disposed like this (see my recent edit). Otherwise if class `Catalog` does not implement `IDisposble` neither explicitly nor implicitly - just do not dispose it. – George Mamaladze Jul 21 '12 at 07:46
  • Thanks Achitaka, It works and perfectly now and dispose the object as I would like to.Thanks for your time and perfect explnation – Suffii Jul 21 '12 at 16:38