-1

All, I have an application I want to lanch from another application or as a stand alone utility. To facilitate the start up of appA from appB, I use the following code in Main()/Program.cs

[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new SqlEditorForm(args));
} 

Now, in SqlEditorForm I have two constructors

public SqlEditorForm(string[] args)
    : this()
{
    InitializeComponent();

    // Test if called from appB...
    if (args != null && args.Count() > 0)
    {
        // Do stuff here...
    }
}

and the deafult

public SqlEditorForm()
{
    // Always do lots of stuff here...
}

This to me looks fine, but when run as stand alone (args.Length = 0) the SqlEditorForm(string[] args) constructor is getting called, and before it steps into the constructor to perform InitializeComponent();, it goes and initialises all of the global variables for the class then steps directly into the default constructor.

Question, The chaining of the constructors seems to be happending in the wrong order. I want to know why?

Thanks for your time.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • What really is the question here? – Lews Therin Dec 11 '12 at 12:53
  • The chaining of the constructors seems to be happending in the wrong order. I want to know why? Thanks. – MoonKnight Dec 11 '12 at 12:56
  • Why down vote. It is a legitimate question - well formatted. spell checked with love. Not even so much as a comment to explain - well thats just bad manners... – MoonKnight Dec 11 '12 at 13:01
  • 1
    http://stackoverflow.com/questions/4609774/constructor-chaining-order – Phil Gan Dec 11 '12 at 13:02
  • @Killercam I didn't downvote anything. – Lews Therin Dec 11 '12 at 13:04
  • Not an issue. Sometimes it is difficult locate questions that have been asked before - I appreciate this may have duplication elsewhere, but I can find it. The above post talks about constructor chaining, which is _not_ complex. This is working differently to the way chaining of constructors _should_ work. [this answer](http://stackoverflow.com/a/1785105/626442) seems to go some way to highlighting what is going on - but not all the way... – MoonKnight Dec 11 '12 at 13:16

1 Answers1

1

Move all logic to constructor with parameter and call that constructor from parameterless one, passing default parameter value:

public SqlEditorForm()
    :this(null)
{        
}

public SqlEditorForm(string[] args)
{   
    InitializeComponent();
    // Always do lots of stuff here...

    if (args != null && args.Count() > 0)
    {
        // Do stuff here...
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • But why should the parameterless constructor be called first; `: this()` says call the default _after_ the overridden constructor. It should not matter where I put `InitializeComponent();` should it!? The way I have set `Main(string[] args)` mean the overridden constructor should always be called... – MoonKnight Dec 11 '12 at 12:59
  • @Killercam sorry, fixed (for some reason initially I thought about `base` call..) - move all logic to constructor with parameter. And call that constructor from your parameterless one, passing default parameter value. – Sergey Berezovskiy Dec 11 '12 at 13:05
  • Thanks very much. This is news to me! Clearly the ordering of the chaining for constructors in WinForms is somewhat different. Am I right in saying this? Again, thanks for your time... – MoonKnight Dec 11 '12 at 13:17
  • @Killercam here is useful link about [Chanin Constructors refactoring](http://www.industriallogic.com/xp/refactoring/chainConstructors.html) – Sergey Berezovskiy Dec 11 '12 at 13:22
  • Thanks you. However, I want to note that this is not just about chaining. WinForms treats this chaining differently including the InitialisComponent() method... – MoonKnight Dec 11 '12 at 14:42
  • @Killercam what do you mean by 'differently'? `Form` as an ordinal C# class, which behaves as other C# classes. – Sergey Berezovskiy Dec 11 '12 at 15:10
  • The way I had the constructors set up befor meant that the custom constructor for the form `SqlEditorForm(args[])` should have been called, _then_ `this()`, invoking the default constructor. The presence of `InitilizeComponent()` method changes this... See [this post](http://stackoverflow.com/a/1784683/626442) – MoonKnight Dec 11 '12 at 15:26