Click here to Skip to main content
15,867,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using (UserProfile objProf = new UserProfile())
           {

               objProf.UserName = txtYourName.Text;
               objProf.UserEmail = txtEmail.Text;
               objProf.UserPhone = txtPhone.Text;

               if (rbtnMale.Checked == true)
               {
                   objProf.UserGender = "M";
               }
               else if (rbtnFemale.Checked == true)
               {
                   objProf.UserGender = "F";
               }
               int Result = new UserProfileBAL().InsertBasicUserProfile(objProf);
           }



i am using the following code to dispose the object.. but i am getting the following error near keyword :Using


error msg:
VB
Error   3   'UserProfile': type used in a using statement must be implicitly convertible to 'System.IDisposable'
Posted
Comments
Rajesh Yerramsetti 11-May-12 0:41am    
Plz help me in solving this error...

type used in a using statement must be implicitly convertible to 'System.IDisposable'
Error in itself is self explanatory.

The using Statement is intended to be used to ensure the disposal of an object at the end of the using block, thus, only types which are disposable may be used in such a statement.
Details here: Compiler Error CS1674 [^]

You need to do implement IDisposable interface for your UserProfile class, something like:
C#
// OK
class D : IDisposable {
   void IDisposable.Dispose() {}
   public void Dispose() {}

   public static void Main() {
      using (D d = new D()) {}
   }
}
 
Share this answer
 
C#
public class UserProfile : System.IDisposable
{
   public UserProfile()
   {
  
   }
   public void Close()
   {

   }
}


Now you can call the UserProfile class from a using Statement!:)
 
Share this answer
 
Comments
Member 12068634 2-Feb-16 22:24pm    
It solved my problem, thanks. But, it then gives me Override dispose(bool) error-method no found to override since i am trying to get rid of Form. Pls kindly Help.
charles henington 26-Aug-16 0:51am    
what do you mean get rid of Form?
The using statement will look at the final type of the expression i.e. whatever is returned from UserProfile(); if this returns something that is IDisposable, then you're OK. The compiler will tell you if you get it wrong.

As a simple step, make sure that you added the proper library reference to fix it. As an example, in LInQ code, if you miss to add a reference to System.Data.Entity thro similar 'using' model, then you will get the same error.
 
Share this answer
 
You should probably watch out for where you are creating two UserProfile instances.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900