-5

I am wanting to check if a file exists, if it does do nothing if it does not copy over a file. This is the code I have, but I get compile errors such as

Only assignment, call increment, decrement and new object expressions can be used as a statement
Invalid expression return
Invalid expression term ':'
; Expected

This is the syntax I have

string template = "C:\\Test\\database12.mdb";
string dest = "R:\\Production\\database12.mdb";
if (File.Exists(dest) ? return : File.Copy(template, dest));
Rob
  • 26,989
  • 16
  • 82
  • 98
Bob Goblin
  • 1,251
  • 3
  • 16
  • 33
  • Why are you trying to use a conditional operator here in the first place? Clearly your requirements don't match up to what it does at all, so don't use it. – Servy Apr 07 '15 at 19:51
  • 2
    `if (!File.Exists(dest)) { File.Copy(template, dest) }`. – zzzzBov Apr 07 '15 at 19:52
  • Note how the correct code example by @zzzzBov is basically a direct translation of the first sentence of your question :) – CompuChip Apr 07 '15 at 20:08

3 Answers3

2

The conditional operator (? :) is an operator that returns a value and can't be used to control program flow. Use a standard if-else instead:

if (File.Exists(dest)) 
    return; 
else  
    File.Copy(template, dest);

Which can be simplified to:

if (File.Exists(dest)) 
    return; 

File.Copy(template, dest);

or just

if (!File.Exists(dest)) 
    File.Copy(template, dest);

assuming there's nothing after the file copy.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • The conditional operator is *designed* for control flow, just a different type of control flow than what the OP wants. – Servy Apr 07 '15 at 19:53
1

You're using a Conditional Operator to specify then/else behaviour, which is not valid.

The ?: operator is for getting a value depending on a bool. Thus you should be getting an object frm a pair of options with the same type.

When you want logic to branch, you need to use an if statement, the synatx for which is

if(condition)
{
    // Do true stuff here
}
else
{
     // Do false stuff here
}
David
  • 10,458
  • 1
  • 28
  • 40
-1

The conditional operator

var result = x ? y : z;

can be seen as a shortcut for something like

T result;
if (x)
    result = y;
else
    result = z;

with T being the type of both y and z. This makes clear that y and z must resolve to values (and not statements) of the same type so the entire statement has a consistent type.

This also makes clear that you can not simply use any method call for y or z, but only such method calls that result in values of the same type.

So while it is ok to write

var value = condition ? func1() : func2(someValue);

as long as func1 and func2 are methods returning values of the same type, it is not ok to write

var value = condition ? return : null;

return is not a value and you may not use return as one of the operands in the conditional operator. You may not even do this:

var value = condition ? return true : false;

You could even do something like this:

if ((File.Exists(dest) ? CalcFileSize(dest) : 0) > 0)
{
    // Do something if the file exists and it has content
}

It's far easier (and correct) in this case to simply use the good old if:

if (File.Exists(dest))
    return;
File.Copy(template, dest);
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • The *conditional* operator is *not* limited to just assignments. It can be used anywhere an expression can be resolved to a value. The second operand of an assignment expression is one of many such places. – Servy Apr 07 '15 at 19:54
  • You can mix `if` and the *conditional* opeartor, if you want: `if(Foo() ? CheckA() : CheckB()) DoWork();` The problem with his use of the conditional operator has nothing to do with the `if` block. The problems with his code are expressed right in the errors that he's getting. – Servy Apr 07 '15 at 20:00
  • Your description of what he's doing wrong isn't actually doing anything to explain what he is doing wrong. He knows that the code he has won't work because *he tried it and it didn't work* (and posted the error message to explain why it won't work). Telling him that it won't work is in no way helpful. – Servy Apr 07 '15 at 20:03
  • @Savy Hope this is more what you'd like to read. Sorry, it took me a while to boot my machine first. – Thorsten Dittmar Apr 07 '15 at 20:21
  • Your description of the mapping, while moving in the right direction, is still rather misleading. Your use of `return` in the translation isn't actually an analogous code snippet, and it's all the more confusing given that his *problem* is using `return` inside of the conditional operation (something that's inherently not going to work). Rather, the result of the expression representing the conditional operator resolves to either the second or third operand (based on the value of the first operand) which is noticeably different from the actual syntax you showed. – Servy Apr 07 '15 at 20:25
  • An analogous snippet to that would be: `T temp; if(x)temp = y; else temp = z; var r = temp;` – Servy Apr 07 '15 at 20:30
  • @Servy Yes, I put it differently, but it comes down to the same thing, really. – Thorsten Dittmar Apr 07 '15 at 20:32
  • You know, all the time you spent here dissecting my answer could way better have been spent writing your own answer explaining things the way you deem right ;-) – Thorsten Dittmar Apr 07 '15 at 20:38
  • Your alternative is still flawed; it results in the expression `z` being resolved no matter what, when in reality it's only resolved if `x` is false. – Servy Apr 07 '15 at 20:39
  • Huh? So you say the `if (x)` doesn't matter? – Thorsten Dittmar Apr 08 '15 at 05:24
  • No, I'm saying that you're resolving the expression `z` to its value even if `x` is true. If it causes side effects they will be observed when they are supposed to have never happened. – Servy Apr 08 '15 at 14:00
  • Well, yes, I understand now. I read "return" instead of "resolve". – Thorsten Dittmar Apr 08 '15 at 14:31