?
Solved

Considerations when having IDisposable private member variables

Posted on 2016-09-24
4
Medium Priority
?
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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