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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:59:58+00:00 2026-05-18T01:59:58+00:00

One of my junior programmers has created a singleton, but it is acting strangely:

  • 0

One of my junior programmers has created a singleton, but it is acting strangely:

I know you aren’t supposed to access object like this, but that’s how they did it and I can’t explain why this is occurring – After calling remove instance, I put a breakpoint on the line after and I can still access the someObject object and it’s properties. This kind of makes sense as your access the reference of the of the mySingleton Object, not the myInstance,… as as you can see I can’t explain it clearly, can anyone help?

e.g.

    Dim x As MySingleton = MySingleton.GetInstance()

    x.someObject.int = 5
    x.someObject.str = "hello"

    Console.Out.WriteLine(x.someObject.int.ToString)
    Console.Out.WriteLine(x.someObject.str.ToString)

    MySingleton.RemoveInstance()

    Console.Out.WriteLine(x.someObject.int.ToString) //still exists!
    Console.Out.WriteLine(x.someObject.str.ToString) //still exists!

Here’s the Psuedo Code for the singleton:

Public Class MySingleton

    Private Shared _myInstance As MySingleton

    Public someObject As New Class1

    Public Shared Function GetInstance() As MySingleton
        If _myInstance Is Nothing Then
            _myInstance = New MySingleton
        End If
        Return _myInstance

    End Function

    Public Shared Sub RemoveInstance()
        _myInstance = Nothing
    End Sub


    End Class

Personally I don’t write my singletons like this – I have the instance object as a separate class. but each to their own.

  • 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-18T01:59:59+00:00Added an answer on May 18, 2026 at 1:59 am

    In general, in .NET you can never explicitly/deterministically set an object to nothing. You can only set its references to Nothing and then allow the garbage collector to clean it up. As already stated, in your original code you actually retained a reference to it by assigning it to x.

    In general, setting an object to Nothing or null is not suggested because an object is eligible for garbage collection after the last reference; the runtime notices this. For example, if you have code like this:

    Dim anObject as new MyObject
    
    anObject.DoAnOperation()
    [1]
    ... 
    ...
    'more code
    ...
    ...
    
    Set anObject = Nothing
    [2]
    

    If you did not set the object to Nothing, it would be eligible for collection anytime after point [1]. If you do, it is not eligible until point [2]. If your method takes a long time to complete, it could become important.

    More info:

    http://msdn.microsoft.com/en-us/library/ee787088.aspx

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

Sidebar

Related Questions

This is very junior, but how does one express a but as a conditional
One has txtDateReceived and second has txtVendorPackDate. Before insert will add record I have
One of my sites has a lot of users. They currently each have one
My apologies once again for asking another very junior question. For one reason or
Parsing is something I come across a lot in development, but as a junior
One of the user of our application has a xml named zean.xml file located
One of the things that I really like in Windows API is overlapped I/O.
One of my entity objects (EF4) has a property that is a decimal. The
one question I always ask myself is how is it possible that javascript has
You know the one I am talking about. We have all been there at

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.