-2

I am trying to run this inline if statement

pRun.StartInfo.FileName = File.Exists("C:\\Test\\Data\\TestingPurposes\\" + UserName 
+ "DailyInfo") ? pRun.StartInfo.UseShellExecute = true : MessageBox.Show("Please 
verify that this file exists");

But I am getting a compile error of:

Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and 'System.Windows.Forms.DialogResult'

How can I remove this error and run the below statement? What I want to do is check if the file exists, if it does, open it. If it does not then produce the messagebox with said message.

crthompson
  • 15,653
  • 6
  • 58
  • 80
  • 1
    You have some syntactical errors, but what you want to achieve by this code? – Hamlet Hakobyan Mar 31 '14 at 20:10
  • @HamletHakobyan - I want to check if the file exists. If the file does exist open the file, if it does not produce said MessageBox. – MasterOfStupidQuestions Mar 31 '14 at 20:11
  • Why on earth would you not want to use an ordinary `if` statement here? –  Mar 31 '14 at 20:11
  • 3
    If the file exists, you are trying to assign `pRun.StartInfo.UseShellExecute = true` to `pRun.StartInfo.FileName`. – 48klocs Mar 31 '14 at 20:11
  • This just doesn't make any sense... you're trying to set the filename to the result of this ternary operation... but the result is either a statement (...UseShellExecute = true) or a MessageBox. That's just... odd... – Tim Mar 31 '14 at 20:13
  • 1
    [`MessageBox.Show()`](http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.show(v=vs.110).aspx) doesn't return a bool; it returns a [`DialogResult`](http://msdn.microsoft.com/en-us/library/system.windows.forms.dialogresult(v=vs.110).aspx). So, you need to make it a boolean expression by comparing its value `MessageBox.Show(...) == DialogResult.OK`. That said, I wouldn't do that with a ternary operator. It's just a mess. – canon Mar 31 '14 at 20:13

1 Answers1

4

In order to use ternary operator, both statement should return the same type or one type should be convertible to other.See documentation:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

You need to use simple if statements intead, it is also more readable:

if(File.Exists("C:\\Test\\Data\\TestingPurposes\\" + UserName + "DailyInfo"))
{ 
   pRun.StartInfo.UseShellExecute = true   
}
else MessageBox.Show("Please verify that this file exists");
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Thanks -- I'll just use a simple if statement. – MasterOfStupidQuestions Mar 31 '14 at 20:14
  • _"Your first expression [...] doesn't return anything, it's just an assignment."_ I'm afraid that's not right, i.e.: `bool x; Console.Write(x = true); // true`. [See here](http://stackoverflow.com/a/3807583/621962). – canon Mar 31 '14 at 20:55
  • @canon you are right, I didn't think that carefully.that is completely valid,thanks for pointing it out, the only problem is expressions returns different values and both are not assignable or convertible to `FileName` which is probably a string. – Selman Genç Mar 31 '14 at 21:09