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 4045296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:24:24+00:00 2026-05-20T13:24:24+00:00

VB.NET 2010, .NET 4 Hello, I tried running code analysis the first time and

  • 0

VB.NET 2010, .NET 4

Hello,

I tried running code analysis the first time and found what it appears many others on SO doing the same have found: an abundance of

In method ‘SomeMethod()’, object
‘SomeObject’ is not disposed along all
exception paths. Call
System.IDisposable.Dispose on object
‘SomeObject’ before all references to
it are out of scope.

warnings (CA2000). I’ve read a bunch of other threads on this here on SO, most of which had to do with the object in question possibly throwing an exception outside of a try block etc.

But what about something like this?

Private SomeContainer As New Panel
Private Sub SomePopulatorMethod()
  '..stuff..'
  For i As Integer = 0 To 9
    Dim newLabel as New Label
    newLabel.Text = i.ToString
    SomeContainer.Controls.Add(newLabel)
  Next
  '..other stuff..'
End Sub

In that case, I don’t want newLabel to be disposed since it should remain in the panel. I do things like this when dynamically setting up a helper form where I create a bunch of textbox/label pairs to represent some data set. Is this approach fundamentally wrong? If so, how else should I go about it? I’m actually (sort of) alright with just letting sleeping dogs lie, but warnings make me sad… I’d like to do things right if only I could know how.

Any insight would be appreciated!
Thanks in advance,
Brian

  • 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-20T13:24:24+00:00Added an answer on May 20, 2026 at 1:24 pm

    Are you sure you’re not getting bit by the problem mentioned in the final paragraph of the MSDN article you linked to?

    I can’t say that your approach is incorrect, on the contrary. Generally speaking you shouldn’t even have to worry about whether or not an object is disposable unless it somehow dictates that it needs to be used in a very specific way (as in with a using(x) { ... } block in C#). A stream for example implements IDisposable and can be used that way, or you can just call Close(), which is ultimately what it does when it’s disposed.

    Most Forms controls are components which by definition implement IDisposable… it would be pretty tiring if you had to keep track of all of them with try/catch blocks. If that warning is showing up because the VB compiler is inserting arithmetic overflow checks on everything then just turn off the checks or ignore the warning.

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

Sidebar

Related Questions

Question: Assume a C++ hello world program, non .NET. With Visual Studio 2005/2008/2010, how
I am trying to make my first program in vb.net 2010, and I have
VB.NET 2010, .NET 4 Hello all, I have a System.Timers.Timer object that does some
VB.NET 2010, .NET 4 Hello all, I'm by no means a good programmer and
VB.NET 2010, .NET 4 Hello, I recently read about using SynchronizationContext objects to control
This code is copied directly from Scott Gu's blog (http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx): Hello @name, the year
Is Linq included in .net 2010 i.e. .NET 4.0
With the recent announcement of .NET 4.0 and Visual Studio 2010, it is becoming
VS.net 2010/.net 4.0 RC was just released. For those who have used Beta or
I installed VS.Net 2010 to play around with some of the new C# features

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.