3
I have a Windows Form with the following code:
public partial class frmCadastroPessoaFisica : Form
{
public frmCadastroPessoaFisica()
{
InitializeComponent();
}
}
I would like to create only one instance of this form.
Some answers on the subject in stackoverflow say to use the Singleton standard:
public class Singleton
{
private static Singleton instance;
private Singleton() { }
public static Singleton Instance
{
get
{
if (instance == null)
lock (typeof(Singleton))
if (instance == null) instance = new Singleton();
return instance;
}
}
}
source: linhadecodigo.com.br
Already others say that using Singleton for this would be exaggeration.
What is the correct way to allow only one instance of this form?
My question is in the case of Forms or GUI-type classes, I believe that this question that has been cited is more in the broad and generic sense.
– Robss70
All right, thanks for your attention.
– Robss70
Just see if you really need one
form
or only the data that fills it.– Thiago Lunardi
I consider Singleton an antipattern these days, but I think in certain situations it’s not worth adding a DI and control technique to ensure these problems... so if it’s a functional solution that doesn’t cause so many disorders, here is a great link explaining various ways to implement Singleton and the advantage/disadvantage of each: http://csharpindepth.com/Articles/General/Singleton.aspx
– Gabriel Katakura
@Thiagolunardi is only the form, the application is large and the form can be "called" from several places, but only one instance of it can exist at a time.
– Robss70
Singleton Pattern is considered bad: https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
– Peres