Solved

Considerations when having IDisposable private member variables

Posted on 2016-09-24
4
70 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nested forach loop to linq 3 42
Visual Studio Start / Browser Emulator menu different 2 38
Convert a string to date 4 40
.NET universe documentation poster 2 22
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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