Singleton Form in Whidbbey

  • Thread starter Thread starter farmboy
  • Start date Start date
F

farmboy

Can someone give me an example of the PROPER way to create a singleton
form in Whidbey? I've done it in the current .NET, but I've been
reading that we can't mess with the constructor in the designer.vb
code.

I've been searching for info, but haven't found much in regards to
doing this in Whidbey.

Any help would be greatly appreciated.

Thanks,
Glen
 
How did i do. In design patterns, we design a singleton from, or somthing
like this.
public class MyForm : System.Windows.Forms.Form
{
int count = 0;
private MyForm() { }
public static MyForm GetForm()
{
if(count <= 2) // the form number, you want to create
{
count++;
return new MyForm();
}
else { return null; } // check this
}
}
 
Hello SharpCoder,

Please explain how this is a singleton... A singleton by definition has one
and *only* one instance. You're creating a new instance up to 2 times.

The *correct* way to expose a class as a singleton is:

public class MyForm : System.Windows.Forms.Form
{
private static MyForm instance = new MyForm();
public static MyForm Instance
{
get { return instance; }
}
}
 
Matt said:
The *correct* way to expose a class as a singleton is:

public class MyForm : System.Windows.Forms.Form
{
private static MyForm instance = new MyForm();
public static MyForm Instance
{
get { return instance; }
}
}

Well, if you're going to bold "correct", you should at least use a
lazy-create pattern - not create the form until it's actually needed.
 
Hello Jon,

The lazy-create pattern is not dependent on the implementation of the singleton
pattern, so for brevity I chose to show the code the way I did.
 
Back
Top