How can I Dispose of a previously referenced Instance in

In, there is a class (class "Login") with a private variable that references an instance of another class (class "User").  If Login.User already references an instance of a User and now I want it to reference a new instance of a User and dispose of the old instance, do I need to explicitly dispose of the old instance (Login.User.dispose) before creating and assigning a reference to the new user (Login. User = New User) or will assigning a reference to the new instance automatically dispose of the old instance?  If so, how would I do this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
It depends somewhat on whether the User class implements the IDisposable interface or not.

.NET generally obfuscates memory management issues from the developer by performing its own garbage collection. Occasionally though you just have to handle it yourself. That's what IDisposable is for.

If the class implements IDisposable then the answer to your question is yes. If not then just let the .NET runtime do its job.

This article might help you understand further.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
Occasionally though you just have to handle it yourself. That's what IDisposable is for.
But generally speaking, you only do this when a class you create is working with unmanaged resources, like file handles, device contexts, graphics handles, etc. If you're not using these things, then you generally don't have to dispose of anything.
Jacques Bourgeois (James Burger)PresidentCommented:
When a class from the framework has a Dispose method, you should consider it as an indication that it is better to call Dispose.

When a class from outside of the framework has a Dispose method, you can usually assume that the programmer who wrote the method knew what he was doing, and it might be better to call Dispose.

But there is also a catch there. A programmer who does not know what he is doing might have created a method that is called Dispose and it might not do what you expect it to do.

There are 2 ways to know... without ever being sure.

The first one is documentation... if you have it.

The second one is to try to create a User object with Using.

    Using yourUser as new User

    End Using

If the compiler accepts this, it means that the underlying class implements IDisposable. Programmers who do not know what they are doing sometimes create a Dispose method that has nothing to do with the mechanism of releasing resources. A programmer who takes care to implement that interface usually knows what he is doing.
Declan_BasileITAuthor Commented:
Thanks everyone.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.