Problems with the Formdesigner when using Singleton-classes.

Hi.

I have just migrated from Java to C# and i ran in to some problems using Visual C# 2005 Express edition.

I have managed to create singleton-classes and they are functioning well, but when i add them to my form, the designer stops working.and I get the Warning message:

"The variable 'myCalendar1' is either undeclared or was never assigned."

myCalendar1 is the singleton-class instanciated trough MyCalendar.getInstance-method.

The project compiles and publishes and works seemingly fine, but the designer produces the same message no matter what I do.

I had to edit the InitializeComponent-method beacuse the designer insists on using the "new"-operator for all new instances, even though i dragged and dropped "MyCalendar" from the toolbox.

I would appreciate any help i can get here, because I've run out of ideas. No luck in searching the web, so I hope you can provide some useful tips.

Tanks!



Answer this question

Problems with the Formdesigner when using Singleton-classes.

  • Chandra B

    By design this is not a good idea to use a singleton on forms or controls. I think you should avoid this and try to redesign your application architecture.

  • Eddie Deyo

    Hi
     
    Since a Windows Forms control can be in only one container, I wonder if it's a good design decision to have a singleton control. Why are you doing that

    Hi.

    I have just migrated from Java to C# and i ran in to some problems using Visual C# 2005 Express edition.

    I have managed to create singleton-classes and they are functioning well, but when i add them to my form, the designer stops working.and I get the Warning message:

    "The variable 'myCalendar1' is either undeclared or was never assigned."

    myCalendar1 is the singleton-class instanciated trough MyCalendar.getInstance-method.

    The project compiles and publishes and works seemingly fine, but the designer produces the same message no matter what I do.

    I had to edit the InitializeComponent-method beacuse the designer insists on using the "new"-operator for all new instances, even though i dragged and dropped "MyCalendar" from the toolbox.

    I would appreciate any help i can get here, because I've run out of ideas. No luck in searching the web, so I hope you can provide some useful tips.

    Tanks!


  • THHNO

    Thanks for all your replies. The have been very helpful!


  • bill perlman

    I've found one way to implement my singleton-classes, and i think It's a good solution: I added all the components in the constructor after InitializeComponent, and it builds and publishes fine.

    The only problem now is that all components added after InitializeComponent don't show in the forms designer, so now i have to set all the properties, like location and size, manually. Are there any methods that "repaint" the form after InitializeComponent

    Any thoughts or ideas regarding this problem wil be much appreciated


  • RonDesta

    this works for me

    public sealed class MainForm : System.Windows.Forms.Form

    {

    private static MainForm m_instance;

    // public property that can only get the single instance

    // of this class.

    public static MainForm Instance

    {

    get

    {

    // Uses "Lazy initialization"

    if( m_instance == null )

    m_instance = new MainForm();

    return m_instance;

    }

    }

    ...

    static void Main()

    {

    /*

    original - replace to implement singleton design

    Application.Run(new MainForm());

    */

    Application.Run(MainForm.Instance);

    }

    }


  • Suketu

    Thanks for reply.

    Firts of all, I think Singletons are easier to work with, and i want to make sure that only one object of these classes are instanciated. Second, it's easier to call these objects with, for instance, "MyClass.Instance"-method. That way I don't have to worry about attach the objectinstance in other constuctors or methods everytime I want other objects to access it..

    It may not be a good design desicion, It's just that I'm used to, and comfortable working with singletons. So I'm open for any other suggestions!

    Thanks again!


  • Problems with the Formdesigner when using Singleton-classes.