views:

128

answers:

3

VB.NET: What is the best way to ensure that a particular object may be instantiated only once during program execution?

+2  A: 

You need a Singleton.

There a great article (unfortunately in C#) here: http://www.yoda.arachsys.com/csharp/singleton.html

But you'll be able to translate to VB quite easily.

You want the 2 last implementations.

further explanations here: http://en.wikipedia.org/wiki/Singleton%5Fpattern

Mike

Mike Gleason jr Couturier
A: 
  1. Use a module.
  2. Use this code

_

Class MySpecialClass
    Private Sub New()
    End Sub

    Public ReadOnly SingleInstance As New MySpecialClass
End Class

Edit: WTF? Why is the code formatting broken?

Jonathan Allen
Adding in a dummy character as above seems to fix it; but it's not clear why one can't go straight from a numbered list into a code section.
Kyralessa
Thanks......................................
Jonathan Allen