<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: A Generic Singleton Form Provider for C#</title>
	<atom:link href="http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/</link>
	<description>The blog of David Hammerton</description>
	<lastBuildDate>Wed, 21 Jul 2010 10:12:23 +1000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Alvaro</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-9947</link>
		<dc:creator>Alvaro</dc:creator>
		<pubDate>Wed, 21 Jul 2010 10:12:23 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-9947</guid>
		<description>Hello David!
Thanks for the code above. It&#039;s perfect for me.
In my code I have included two more things:
1. MdiParent
2. BringToFront
The only thing I have changed is the GetInstance method:
public static T GetInstance(Form owner, params object[] args) where T : Form
        {
            if (!mTypeFormLookup.ContainsKey(typeof(T)))
            {
                Form f = (Form)Activator.CreateInstance(typeof(T), args);
                mTypeFormLookup.Add(typeof(T), f);
                f.Owner = owner;
                f.MdiParent = owner;
                f.FormClosed += new FormClosedEventHandler(remover);
            }
            else
            {
                Form f;
                mTypeFormLookup.TryGetValue(typeof(T),out f);
                f.BringToFront();
            }
            return (T)mTypeFormLookup[typeof(T)];
        }</description>
		<content:encoded><![CDATA[<p>Hello David!<br />
Thanks for the code above. It&#8217;s perfect for me.<br />
In my code I have included two more things:<br />
1. MdiParent<br />
2. BringToFront<br />
The only thing I have changed is the GetInstance method:<br />
public static T GetInstance(Form owner, params object[] args) where T : Form<br />
        {<br />
            if (!mTypeFormLookup.ContainsKey(typeof(T)))<br />
            {<br />
                Form f = (Form)Activator.CreateInstance(typeof(T), args);<br />
                mTypeFormLookup.Add(typeof(T), f);<br />
                f.Owner = owner;<br />
                f.MdiParent = owner;<br />
                f.FormClosed += new FormClosedEventHandler(remover);<br />
            }<br />
            else<br />
            {<br />
                Form f;<br />
                mTypeFormLookup.TryGetValue(typeof(T),out f);<br />
                f.BringToFront();<br />
            }<br />
            return (T)mTypeFormLookup[typeof(T)];<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-8686</link>
		<dc:creator>James</dc:creator>
		<pubDate>Wed, 21 Apr 2010 17:04:03 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-8686</guid>
		<description>Thank you so much.
Huge help.</description>
		<content:encoded><![CDATA[<p>Thank you so much.<br />
Huge help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sudhi</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-8330</link>
		<dc:creator>Sudhi</dc:creator>
		<pubDate>Fri, 05 Mar 2010 02:31:44 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-8330</guid>
		<description>Thank you for Nice example</description>
		<content:encoded><![CDATA[<p>Thank you for Nice example</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nuno Coimbra</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-3885</link>
		<dc:creator>Nuno Coimbra</dc:creator>
		<pubDate>Mon, 18 May 2009 09:09:30 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-3885</guid>
		<description>Hello David!

I&#039;m know for, sometimes, miss the tinny picture, i.e., the details... By rushing into things without proper first reading.

My code is exactly the one here. :) I&#039;ve created a new C# project, copy and pasted your code into it and run it. In fact I was surprised it didn&#039;t work, since I&#039;ve seen no reason for it to fail... Until now. Now I see what I&#039;ve missed. :S It&#039;s supposed to be used with regular forms... duh! :P 

Thanks! As I said before, this is truly a cool implementation and it can provide me a way for getting rid of all my Singleton forms... I&#039;ll put it to a good test. :)</description>
		<content:encoded><![CDATA[<p>Hello David!</p>
<p>I&#8217;m know for, sometimes, miss the tinny picture, i.e., the details&#8230; By rushing into things without proper first reading.</p>
<p>My code is exactly the one here. <img src='http://craz.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I&#8217;ve created a new C# project, copy and pasted your code into it and run it. In fact I was surprised it didn&#8217;t work, since I&#8217;ve seen no reason for it to fail&#8230; Until now. Now I see what I&#8217;ve missed. :S It&#8217;s supposed to be used with regular forms&#8230; duh! <img src='http://craz.net/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  </p>
<p>Thanks! As I said before, this is truly a cool implementation and it can provide me a way for getting rid of all my Singleton forms&#8230; I&#8217;ll put it to a good test. <img src='http://craz.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Hammerton</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-3842</link>
		<dc:creator>David Hammerton</dc:creator>
		<pubDate>Sat, 16 May 2009 22:34:40 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-3842</guid>
		<description>Hi Nuno,

There isn&#039;t any reason for it not to work under .NET 3.5, that I can see. What&#039;s the exact problem you&#039;re seeing?

You are correct, you don&#039;t inherit from the Singleton object, your Forms shouldn&#039;t be &#039;Singleton objects&#039;. You simply use the class&#039; GetInstance method to create singleton instances of forms for you.

Feel free to paste your code if you are having trouble.

David</description>
		<content:encoded><![CDATA[<p>Hi Nuno,</p>
<p>There isn&#8217;t any reason for it not to work under .NET 3.5, that I can see. What&#8217;s the exact problem you&#8217;re seeing?</p>
<p>You are correct, you don&#8217;t inherit from the Singleton object, your Forms shouldn&#8217;t be &#8216;Singleton objects&#8217;. You simply use the class&#8217; GetInstance method to create singleton instances of forms for you.</p>
<p>Feel free to paste your code if you are having trouble.</p>
<p>David</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nuno Coimbra</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-3828</link>
		<dc:creator>Nuno Coimbra</dc:creator>
		<pubDate>Sat, 16 May 2009 12:33:36 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-3828</guid>
		<description>oh, and another thing... I&#039;ve tested this an looked at it, and found out that the Forms don&#039;t have to be Singleton objects for the provider to work. :)</description>
		<content:encoded><![CDATA[<p>oh, and another thing&#8230; I&#8217;ve tested this an looked at it, and found out that the Forms don&#8217;t have to be Singleton objects for the provider to work. <img src='http://craz.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nuno Coimbra</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-3825</link>
		<dc:creator>Nuno Coimbra</dc:creator>
		<pubDate>Sat, 16 May 2009 12:21:24 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-3825</guid>
		<description>HEllo!

This is cool! But it doesn&#039;t work in .NET 3.5 (C#). Keeps saying that the constructor of SomeSingletonForm can&#039;t be found :) It only works with a public constructor. Am I doing something worong?</description>
		<content:encoded><![CDATA[<p>HEllo!</p>
<p>This is cool! But it doesn&#8217;t work in .NET 3.5 (C#). Keeps saying that the constructor of SomeSingletonForm can&#8217;t be found <img src='http://craz.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It only works with a public constructor. Am I doing something worong?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pedro</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-2197</link>
		<dc:creator>Pedro</dc:creator>
		<pubDate>Thu, 19 Feb 2009 09:56:15 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-2197</guid>
		<description>Excellent implementation. Here is the version form Windows Mobile 6. It&#039;s actually working on my current project:

 public static class SingletonFormProvider
    {
        #region Singleton
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static SingletonFormProvider()
        {
        }
        #endregion

        static Dictionary mTypeFormLookup = new Dictionary();

        static public T GetInstance()
            where T : Form
        {
            return GetInstance(null, new Type[]{ } /*sin parámetros*/);
        }

        static public T GetInstance(Form owner)
            where T : Form
        {
            return GetInstance(owner, new Type[] { }) /*sin parámetros*/;
        }

        static public T GetInstance(Form owner, params object[] args)
            where T : Form
        {
            // ¿Ya hay una instancia del tipo T?
            if (mTypeFormLookup.ContainsKey(typeof(T))) 
            {
                T f = (T)mTypeFormLookup[typeof(T)];
                
                // ¿Esa instancia está disposed?
                if (f.IsDisposed) 
                {
                    // Borro para la entrada correspondiente al tipo typeof(T) la instancia (formulario) f, puesto que está disposed.
                    mTypeFormLookup.Remove(f.GetType());
                }
            }

            // ¿No hay instancia de tipo T?
            if (!mTypeFormLookup.ContainsKey(typeof(T))) 
            {   
                // Constructor de la clase T con parámetros args.
                ConstructorInfo constructorInfo = 
                    typeof(T).GetConstructor(args.Select(p =&gt; p.GetType()).ToArray());
                
                Debug.Assert(constructorInfo != null, 
                    string.Format(&quot;La clase {0} no tiene ningún constructor disponible.&quot;, 
                    typeof(T)));
                
                // Equivale a: T f = new T(..., [args], ...);
                Form f = constructorInfo.Invoke(args) as T;
#if DEBUG
                if (f == null) 
                {
                    object[] obj = constructorInfo.GetParameters().Select(p =&gt; p.Name).Cast().ToArray();
                    StringBuilder sb = new StringBuilder();
                    int i = 0;
                    foreach (string param in constructorInfo.GetParameters().Select(p =&gt; p.Name))
                    {
                        sb.Append(param);
                        sb.Append(&quot;=&quot;);
                        sb.Append(args[i++]);
                        if (i &lt; args.Length) sb.Append(&quot;, &quot;);
                    }
                
                    Debug.Assert(f != null,
                    string.Format(&quot;No se ha podido invocar el constructor {0} de la clase {1} con parámetros ({2}).&quot;,
                        constructorInfo.Name, typeof(T), sb.ToString()));
                }
#endif
                // Añado para la entrada correspondiente al tipo typeof(T) la instancia (formulario) f.
                mTypeFormLookup.Add(typeof(T), f);
                f.Owner = owner;
            }

            return (T)mTypeFormLookup[typeof(T)];
        }
    }</description>
		<content:encoded><![CDATA[<p>Excellent implementation. Here is the version form Windows Mobile 6. It&#8217;s actually working on my current project:</p>
<p> public static class SingletonFormProvider<br />
    {<br />
        #region Singleton<br />
        // Explicit static constructor to tell C# compiler<br />
        // not to mark type as beforefieldinit<br />
        static SingletonFormProvider()<br />
        {<br />
        }<br />
        #endregion</p>
<p>        static Dictionary mTypeFormLookup = new Dictionary();</p>
<p>        static public T GetInstance()<br />
            where T : Form<br />
        {<br />
            return GetInstance(null, new Type[]{ } /*sin parámetros*/);<br />
        }</p>
<p>        static public T GetInstance(Form owner)<br />
            where T : Form<br />
        {<br />
            return GetInstance(owner, new Type[] { }) /*sin parámetros*/;<br />
        }</p>
<p>        static public T GetInstance(Form owner, params object[] args)<br />
            where T : Form<br />
        {<br />
            // ¿Ya hay una instancia del tipo T?<br />
            if (mTypeFormLookup.ContainsKey(typeof(T)))<br />
            {<br />
                T f = (T)mTypeFormLookup[typeof(T)];</p>
<p>                // ¿Esa instancia está disposed?<br />
                if (f.IsDisposed)<br />
                {<br />
                    // Borro para la entrada correspondiente al tipo typeof(T) la instancia (formulario) f, puesto que está disposed.<br />
                    mTypeFormLookup.Remove(f.GetType());<br />
                }<br />
            }</p>
<p>            // ¿No hay instancia de tipo T?<br />
            if (!mTypeFormLookup.ContainsKey(typeof(T)))<br />
            {<br />
                // Constructor de la clase T con parámetros args.<br />
                ConstructorInfo constructorInfo =<br />
                    typeof(T).GetConstructor(args.Select(p =&gt; p.GetType()).ToArray());</p>
<p>                Debug.Assert(constructorInfo != null,<br />
                    string.Format(&#8221;La clase {0} no tiene ningún constructor disponible.&#8221;,<br />
                    typeof(T)));</p>
<p>                // Equivale a: T f = new T(&#8230;, [args], &#8230;);<br />
                Form f = constructorInfo.Invoke(args) as T;<br />
#if DEBUG<br />
                if (f == null)<br />
                {<br />
                    object[] obj = constructorInfo.GetParameters().Select(p =&gt; p.Name).Cast().ToArray();<br />
                    StringBuilder sb = new StringBuilder();<br />
                    int i = 0;<br />
                    foreach (string param in constructorInfo.GetParameters().Select(p =&gt; p.Name))<br />
                    {<br />
                        sb.Append(param);<br />
                        sb.Append(&#8221;=&#8221;);<br />
                        sb.Append(args[i++]);<br />
                        if (i &lt; args.Length) sb.Append(&#8221;, &#8220;);<br />
                    }</p>
<p>                    Debug.Assert(f != null,<br />
                    string.Format(&#8221;No se ha podido invocar el constructor {0} de la clase {1} con parámetros ({2}).&#8221;,<br />
                        constructorInfo.Name, typeof(T), sb.ToString()));<br />
                }<br />
#endif<br />
                // Añado para la entrada correspondiente al tipo typeof(T) la instancia (formulario) f.<br />
                mTypeFormLookup.Add(typeof(T), f);<br />
                f.Owner = owner;<br />
            }</p>
<p>            return (T)mTypeFormLookup[typeof(T)];<br />
        }<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Albert</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-2072</link>
		<dc:creator>Albert</dc:creator>
		<pubDate>Wed, 04 Feb 2009 09:15:12 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-2072</guid>
		<description>Good implementation.

But, i have a question. I have the following code:


    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(MenuPrincipalObservador.InitForm());
        }
    }


    class MenuPrincipalObservador
    {

        private MenuPrincipalObservador()
        {
        }

        public static Form InitForm()
        {
            MenuPrincipalObservador mpo = new MenuPrincipalObservador();

            MDIParent1 mdi = MenuPrincipalFormSingleton.Instance.createForm();

            mdi.BtnRecepcioMercaderiesClicked += mpo.ClickRecepcioHandler;

            return mdi;
        }


        private void ClickRecepcioHandler(Object sender, EventArgs e)
        {
            frmRecepcioMerc frm = SingletonFormProvider.GetInstance(iDontKnow);
            frm.Show();  
        }
    }


In the last function ClickRecepcioHandler, like him shipment the owner?

Thanks for your attention.</description>
		<content:encoded><![CDATA[<p>Good implementation.</p>
<p>But, i have a question. I have the following code:</p>
<p>    static class Program<br />
    {<br />
        ///<br />
        /// The main entry point for the application.<br />
        ///<br />
        [STAThread]<br />
        static void Main()<br />
        {<br />
            Application.EnableVisualStyles();<br />
            Application.SetCompatibleTextRenderingDefault(false);<br />
            Application.Run(MenuPrincipalObservador.InitForm());<br />
        }<br />
    }</p>
<p>    class MenuPrincipalObservador<br />
    {</p>
<p>        private MenuPrincipalObservador()<br />
        {<br />
        }</p>
<p>        public static Form InitForm()<br />
        {<br />
            MenuPrincipalObservador mpo = new MenuPrincipalObservador();</p>
<p>            MDIParent1 mdi = MenuPrincipalFormSingleton.Instance.createForm();</p>
<p>            mdi.BtnRecepcioMercaderiesClicked += mpo.ClickRecepcioHandler;</p>
<p>            return mdi;<br />
        }</p>
<p>        private void ClickRecepcioHandler(Object sender, EventArgs e)<br />
        {<br />
            frmRecepcioMerc frm = SingletonFormProvider.GetInstance(iDontKnow);<br />
            frm.Show();<br />
        }<br />
    }</p>
<p>In the last function ClickRecepcioHandler, like him shipment the owner?</p>
<p>Thanks for your attention.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://craz.net/blog/2008/06/19/a-generic-singleton-form-provider-for-c/comment-page-1/#comment-2046</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Mon, 26 Jan 2009 04:09:46 +0000</pubDate>
		<guid isPermaLink="false">http://craz.new.eco-shout.org/blog/?p=5#comment-2046</guid>
		<description>This is far amd away the most elegannt, concise and correct implememtation and solution to a very common problem. Thank you for sharing it.</description>
		<content:encoded><![CDATA[<p>This is far amd away the most elegannt, concise and correct implememtation and solution to a very common problem. Thank you for sharing it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
