0

I have this simple piece of code that focuses a text field if the user hits yes on a DialogResult, or exits the application if s/he hits no.

DialogResult dialogResult = MessageBox.Show("Client Not Verified\n" + " " + txtUserName.Text + " " + "Already Taken.\n Try Again?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Yes)
{
    txtUserName.Focus();
    return;
}
else
{
    Application.Exit();
}

how I can write the code with the same output by using the ?: operators? so it would look something like this:

dialogResult == DialogResult.Yes ? txtUserName.Focus : Application.Exit();
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • 1
    What happens if you try that? Don't forget the `()` at `Focus()`? – Thomas Weller Jun 02 '21 at 07:54
  • `?:` selects and returns a value. What value is returned by those two methods? Why do you want to use `?:` at all? Just to use a single line, even if it makes the code harder to read and maintain? – Panagiotis Kanavos Jun 02 '21 at 07:56
  • An actual improvement would be to use `String.Format` or string interpolation instead of concatenation to create the message. Each concatenation creates a new temporary string. `$"Client Not Verified\n {txtUserName.Text} Already Taken.\n Try Again?"` is cleaner and only creates a single string. – Panagiotis Kanavos Jun 02 '21 at 07:59
  • @ThomasWeller I am trying to use various ways in my code and thank you :) –  Jun 02 '21 at 08:02
  • @PanagiotisKanavos value returned by this method will be focusing the Username text field if the user hits yes, and closing the application if he hits no mate using these operators would not make the code harder to maintain as it makes it less complex, perform good and more portable. –  Jun 02 '21 at 08:04
  • @PanagiotisKanavos kindly explain a bit more your idea. I *just* woke up –  Jun 02 '21 at 08:05
  • You misunderstand. I'm not asking what's the purpose of that code. I'm asking what is the value returned by `Focus()` or `Exit()`? There isn't one. `?:` is used to pick and return a value, so it can't work with methods that return nothing. `?:` works with expressions, but those calls are statements. `?:` also needs to assign its value somewhere. In this case there's no useful value – Panagiotis Kanavos Jun 02 '21 at 08:05
  • @PanagiotisKanavos Now I get you, thanks, buddy. but what can I do to set, get the values of focus, and exit? –  Jun 02 '21 at 08:11
  • These aren't values, they're method calls. And what you want to do won't improve performance or portability in the slightest. The duplicate question's answers shows how to get this to work but these techniques have slightly worse performance. Are you trying to immitate F# perhaps? C# isn't F# and until all methods return a `unit`, you won't be able to treat methods as functions – Panagiotis Kanavos Jun 02 '21 at 08:13
  • @PanagiotisKanavos I see, then there is no use of using this approach, thanks mate. and it is c# –  Jun 02 '21 at 08:17

0 Answers0