Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 738469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:50:24+00:00 2026-05-14T07:50:24+00:00

I receive a warning when I run some code through Visual Studio’s Code Analysis

  • 0

I receive a warning when I run some code through Visual Studio’s Code Analysis utility which I’m not sure how to resolve. Perhaps someone here has come across a similar issue, resolved it, and is willing to share their insight.

I’m programming a custom-painted cell used in a DataGridView control. The code resembles:

public class DataGridViewMyCustomColumn : DataGridViewColumn
{
    public DataGridViewMyCustomColumn() : base(new DataGridViewMyCustomCell())
    {
    }

It generates the following warning:

CA2000 : Microsoft.Reliability : In method ‘DataGridViewMyCustomColumn.DataGridViewMyCustomColumn()’ call System.IDisposable.Dispose on object ‘new DataGridViewMyCustomCell()’ before all references to it are out of scope.

I understand it is warning me DataGridViewMyCustomCell (or a class that it inherits from) implements the IDisposable interface and the Dispose() method should be called to clean up any resources claimed by DataGridViewMyCustomCell when it is no longer.

The examples I’ve seen on the internet suggest a using block to scope the lifetime of the object and have the system automatically dispose it, but base isn’t recognized when moved into the body of the constructor so I can’t write a using block around it… which I’m not sure I’d want to do anyway, since wouldn’t that instruct the run time to free the object which could still be used later inside the base class?

My question then, is the code okay as is? Or, how could it be refactored to resolve the warning? I don’t want to suppress the warning unless it is truly appropriate to do so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:50:24+00:00Added an answer on May 14, 2026 at 7:50 am

    If you’re using Visual Studio 2010 then CA2000 is completely broken. It may also be broken in other versions of FxCop (a.k.a. Code Analysis), but VS2010 is the only one I can vouch for. Our codebase is giving CA2000 warnings for code like this…

    internal static class ConnectionManager 
    {
        public static SqlConnection CreateConnection()
        {
             return new SqlConnection("our connection string");
        }
    }
    

    …indicating that the connection is not being disposed before it goes out of scope in the method. Well, yeah, that’s true, but it isn’t out of scope for the application as it’s returned to a caller – that’s the whole point of the method! In the same way, your constructor argument isn’t going out of scope but is being passed to the base class, so it’s a false positive from the rule rather than an actual problem.

    This used to be a useful rule, but now all you can really do is turn it off until they fix it. Which is unfortunate, because the (very few) actual positives are things that should be fixed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You might have a look at another (nice one) implementation… May 14, 2026 at 10:02 pm
  • Editorial Team
    Editorial Team added an answer Just project your local list into a list of the… May 14, 2026 at 10:02 pm
  • Editorial Team
    Editorial Team added an answer The above is probably an Objective-C category that extends the… May 14, 2026 at 10:02 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.