I have a Winforms app with the following constructors:
public Form1()
{
InitializeComponent();
//Code that enables/disables buttons etc
}
public Form1(int ID)
{
searchByID = ID;
InitializeComponent();
//Code that enables/disables buttons etc
}
Which one gets choosen? That depends if the program is started by CMD with an added parameter. This is the main that checks that:
static void Main(string[] args)
{
//Args will be the ID passed by a CMD-startprocess (if it's started by cmd of course
if (args.Length == 0)
{
Application.Run(new Form1());
}
else if(args.Length>0)
{
string resultString = Regex.Match(args[0], @"\d+").Value;
incidentID = Int32.Parse(resultString);
try
{
Application.Run(new Form1(incidentID));
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
My question was:
How can I optimize the constructors? They both contain about 30 lines of as good as the same code and I wanted to fix this by doing:
public Form1()
{
Form1(0)
}
public Form1(int ID)
{
if (ID>0)
{
//it has an ID
}else
{
doesn't have an ID
}
}
but this gives me the error:
Non-invocable member cannot be used like a method.
How can I optimize this?