ehsan0x
ehsan0x

Reputation: 670

Which way of using Application.Run() is a better practice in code below?

I want to run the login form named frmLogin on my C# application run. I have come up with two different ways to do this, I want to know if any of the two is a more preferred practice? and why? or if they are totally equal?

Also, if there is a better practice than this, what is that?

First way:

Application.Run(new frmLogin());

Second way:

frmLogin _login = new frmLogin();
Application.Run(_login);

Upvotes: 0

Views: 118

Answers (2)

Hans Passant
Hans Passant

Reputation: 942255

Makes no difference. The local variable that stores the form reference exists either way, in the second snippet it just doesn't have a name.

You are however not doing this right. You can't actually tell if the login was successful. And nothing good happens when the user logs in and closes the window, your app stops running. And if you try to work around it by hiding the login window then you'll have a hard time stopping the application.

You should do it like this instead:

using (var dlg = new frmLogin()) {
   if (dlg.ShowDialog() != DialogResult.OK) return;
}
Application.Run(new frmMain());

Upvotes: 1

JaredPar
JaredPar

Reputation: 755357

There is no functional difference between the two.

The only real difference is the second form gives you the ability to access frmLogin after the Run method completes. So unless you actually do something to _login there is no difference here

Upvotes: 3

Related Questions