Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys I am using methods of class in other class by making objects like

DO_AddData ObjAddData = new DO_AddData();----Do_AddData other class
this is defined globally in my current class.

i use ObjAddData in many methods of current class .After that i want to dispose this objects..

I try "using" keyword but it's good for local objects which has local scope only...

I also used try finally like


try
{
///////
}

finally
{
((System.Idisposable)).dispose();

}
but it throws exceptions like...

"Unable to cast object of type 'Penmia.Objects.DO_AddData' to type 'System.IDisposable'."

So is there any suitable scenerio for disposing global objects.

Please provide any solution
thank n regards
Rahul
}
Posted
Comments
Sergey Alexandrovich Kryukov 28-Nov-13 4:35am    
There are no "global objects" in .NET, nothing to dispose... :-)
—SA

Invalid question and purely artificial "problem". Please see my comment to the question.

You never ever should cast to IDisposable. If some type does implements this interface, the Dispose call won't need the case, if not, the object don't need to be disposed at all.

I don't know if you understand that this interface is generally unrelated to reclaiming of managed memory. Such reclaiming is done by the Garbage Collector after objects become unreachable. So, just in case, please read about it: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

—SA
 
Share this answer
 
Comments
Rahul_Pandit 28-Nov-13 4:47am    
yeah but there is requirement,we can't believe on garbage collector for disposing...

I am creating a object to access other class method and now i want to dispose this object..And this object is used globally in my whole class.

Now tell me is it clear.
Sergey Alexandrovich Kryukov 28-Nov-13 10:12am    
Please stop it. You just don't understand what dispose does and what should be cleaned up. If you think you need to dispose, please explain what are you going to do in dispose. "Object is used globally in my whole class" is just gibberish, it cannot be clear or unclear. You need to understand types vs objects.
—SA
HI
try like this...



C#
public class OtherClass
      {
          public void somemethod()
          {

              using (DO_AddData obj = new DO_AddData ())
              {

// ur coding here...

              }

          }
      }



      public class DO_AddData : IDisposable
      {
          public void Dispose()
          {
             //
          }
      }




for more info on Using statement pls go through this link

http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx[^]
 
Share this answer
 
Comments
Rahul_Pandit 28-Nov-13 5:36am    
public class OtherClass
{

DO_AddData obj = new DO_AddData ();

protected void page_load()
{
public void somemethod()
{

string str="hello";
obj.insert(str);

}
public void method2()
{
string str="hEY";
obj.insert(str);

}
}

}
protected void button_click()
{
string str="hello all";
obj.insert(str);
}


public class DO_AddData
{
public void insert(STRING STRPARAM)
{
/////insert in database
}
}

it's very related to my code ...now provide my any solution
try this solution..

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace POC
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        DO_AddData obj = new DO_AddData();
        protected void Page_Load(object sender, EventArgs e)
        {
            somemethod();
            method2();
        }
        public void somemethod()
        {
            using (obj)
            {
                string str = "hello";
                obj.insert(str);
            }

        }
        public void method2()
        {
            using (obj)
            {
                string str = "hEY";
                obj.insert(str);
            }

        }
    }

    public class DO_AddData : IDisposable
    {
        public void insert(string STRPARAM)
        {
            /////insert in database
        }

        public void Dispose()
        {
            // dispose all the objects used in this class
        }
    }
}
 
Share this answer
 
Comments
Rahul_Pandit 29-Nov-13 1:01am    
nice solution..but can u tell me how can i check object is disposed or not.
Rahul_Pandit 29-Nov-13 1:19am    
Karthik>>You put method Dispose() in DO_AddData class.So how can i Dispose objAddData in Do_AddData class Dispose method,because i am creating objaddData in webform2..

Please tell me.
Karthik_Mahalingam 29-Nov-13 1:50am    
Hi Rahul

Dispose method will be called automatically when the scope of the using function is completed..

in the dispose method, clear the objects which u r using in the DO_AddData class..
Rahul_Pandit 29-Nov-13 4:03am    
Karthik....DO I not need to clear objAddData??Will it clear automatically??
Karthik_Mahalingam 29-Nov-13 4:36am    
you r declaring it globally for this class.. it wont be disposed unless u r out of that page...

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