-2

I am using the line of code below and getting the error:

Type used in a using statement must be implicitly convertible to System.IDisposable

How do I eradicate this error? This is the line of code:

using (var db = new HealthTrackerContext())
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Rifki
  • 893
  • 1
  • 11
  • 22

4 Answers4

5

If your HealthTrackerContext does not implement IDisposable, you will get this error. I suspect your class does not; so to remedy this situation, you either need to implement IDisposable on your object or remove the using block.

There's nothing wrong with not using a using block, btw =D

Tejs
  • 40,736
  • 10
  • 68
  • 86
0

HealthTrackerContext should implement the IDisposable interface; Once you implement that interface, the error would disappear.

Look at this link http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

edocetirwi
  • 542
  • 5
  • 22
0

It means HealthTrackerContext doesn't implement IDisposable.

A copy-easy and thread safe pattern:

public partial class HealthTrackerContext: IDisposable {
    protected virtual void Dispose(bool disposing) {
        lock(thisLock)
            if(!disposed) {
                if(disposing) {
                    // release your resource 
                    // and set the referencing variables with null 

                    this.disposed=true;
                }
            }
    }

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

    ~HealthTrackerContext() {
        this.Dispose(false);
    }

    object thisLock=new object();
    bool disposed;
}
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
0

you must implement IDisposable Interface for this object like that :

public class Disposable : IDisposable
    {
        private bool isDisposed;

        ~Disposable()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(bool disposing)
        {
            if (!isDisposed && disposing)
            {
                DisposeCore();
            }

            isDisposed = true;
        }


    }   
Joan Caron
  • 1,969
  • 18
  • 21
  • Note that there is absolutely no reason to add this mess of code unless the class has unmanaged objects that need to be destroyed. From the question, it sounds like you're thinking about the problem the wrong way around: the `using` statement exists to make objects that implement `IDisposable` easier to use. You only need `using` if you need `IDisposable`. – Cody Gray - on strike Mar 25 '13 at 22:30