Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

This one has me stumped.

I'm creating a ListViewWebPart, which ultimately inherits from System.Web.UI.Control which implements IDisposable. So ListViewWebPart should also be IDisposable. Right?

But when I code:

using ( ListViewWebPart webPart = new ListViewWebPart()) { ... }

I get a compile error that tells me this type can't be implicitly converted to IDisposable.

I just know I'm doing something wrong here but I just can't see it.

If I use the full class name Microsoft.SharePoint.WebPartPages.ListViewWebPart I still get the same error, and if I use the same code above without the using I can even see the .Dispose() method coming up in the intelli sense.

Could the Dispose() method be shadowed (new'ed) or something?

I'm using VS 2012 on SharePoint 2010 (so .Net 3.5 target).

I'll have a look with ILSPY but has anyone out there seen this before?

I guess my fall back is to explicitly do this with a try/finally.

(I must have been really bad in a previous life to end up as a SharePoint developer.)

Minor Update:

It might be something to do with the SharePoint project or where I'm using it (Feature Event Receiver Activation method). In a separate class library this works fine with no problem.

share|improve this question
1  
So when I finally built, I got an error to tell me I needed to reference System.Web. When I added that I could go back and use the "using" at last. Pity it didn't say that upfront but you learn something everyday. –  Swanny May 23 '13 at 23:55
add comment

1 Answer

If you are getting the error Cannot implicitly convert type YourType to System.IDisposable, than your class needs to implement it. Cite:

You can only use using with classes that implement IDisposable, so that under the covers the compiler knows which function to call on finalisation - yourclass.Dispose().

Like you said, it's probably where you're using it. Have you tried with using System.Web.UI.WebControls;?

share|improve this answer
    
It's not my class and it definately implements it. But your using suggestion was close, not the namespace but the assembly. I needed to add a reference to System.Web. Go figure. –  Swanny May 23 '13 at 23:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.