This project has moved and is read-only. For the latest updates, please go here.

":" - Expression Helper - Invalid literal character



I noticed that when I try to parse the following expression:

boolValue ? "C:/SOMEPATH" : "D:/SOMEPATH"

I get an Invalid literal character exception for the ':' char. The exception is being thrown in the "Unescape" method.

I have also tried:

boolValue ? @"C:/SOMEPATH" : @"D:/SOMEPATH"

but this did not improve things.

Any chance of a fix?

Many thanks,



RupertAvery wrote Oct 15, 2016 at 7:16 PM

Note: You need to wrap code in a code block (CTRL-K), otherwise the backslashes seem to get escaped.

From the code you gave above, the forward slash works fine. The expression you gave is using double-quotes without escape characters, you I assume it's not being stored in a .cs source code as a string literal, instead as plain text:
boolValue ? "C:/SOMEPATH" : "D:/SOMEPATH" 
To embed the expression you gave into a C# program you would have to escape the double quotes so it would look like this:
var expr = new CompiledExpression("boolValue ? \"C:/SOMEPATH\" : \"D:/SOMEPATH\"");
However, I think you are trying to use a backslash. The backslash will not work as you intended. If your expression was in a plain text file (not in .cs) it might look like this:
boolValue ? "C:\SOMEPATH" : "D:\SOMEPATH" 
This will throw an exception, and even the IDE will refuse to compile this is you placed it in a .cs file as an actual piece of code. This is because backslash in a string is used to escape the next character, so it would appear to the compiler that you are trying to enter \S, which is an invalid escape character.

If you are storing it outside code, in plain text the expression you need is:
boolValue ? "C:\\SOMEPATH" : "D:\\SOMEPATH" 
So that the compiler escapes the double backslash to a single backslash.

If you embedded the expression as a string literal in C# code, you would need to escape your backslashes twice:
var expr = new CompiledExpression("boolValue ? \"C:\\\\SOMEPATH\" : \"D:\\\\SOMEPATH\"");
Hope this resolves your issue.