?
Solved

Considerations when having IDisposable private member variables

Posted on 2016-09-24
4
Medium Priority
?
99 Views
Last Modified: 2016-09-25
I have a class similar to below which has a private member variable whose type is from a 3rd party and implements IDisposable.  My question is are there any considerations I should be making when using a type this way?

    public class PaypalService
    {
        //PayPalAPIInterfaceClient implements IDispose
        private readonly PayPalAPIInterfaceClient _paypalClient = new PayPalAPIInterfaceClient();

        public List<PaypalTransaction> GetTransactions(DateTime startDate, DateTime endDate)
        {
            //do stuff using _paypalClient
        }
        public List<PaypalTransaction> GetTransactions(string payerEmail)
        {
            //do stuff using _paypalClient
        }
        public List<PaypalTransaction> GetTransaction(string transactionId)
        {
            //do stuff using _paypalClient
        }
    }

Open in new window



In all the code examples the class is wrapped in a using() similar to below which is what made me think I might be causing problems using it as a member variable.

            using (var paypalClient = new PayPalAPIInterfaceClient())
            {
                //do stuff using paypalClient
            }

Open in new window

0
Comment
Question by:canuckconsulting
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41814051
If the disposable thing is at the class level, then your class needs to implement IDisposable so that other developers have the chance to dispose of the object. You will dispose of the class-level object in the Dispose method you create. Otherwise, if you're only using something disposable within a method, then wrap that usage in a using statement.
0
 

Author Comment

by:canuckconsulting
ID: 41814052
Hi käµfm³d.

It's used by multiple members within the class which is why I have it set as a member variable.

What would the IDisposable method look like?  Would it simply call _paypalClient .Dispose()?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 41814063
Yes, but you should follow the disposable pattern when you implement it, as recommended by MS.

By creating a public Dispose method, you tell other developers that they should expect to clean up this class (via that method) when they are done with it.
0
 

Author Closing Comment

by:canuckconsulting
ID: 41814936
Great link.  Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question