I have a vb.net project I'm trying to convert into C#. I have a file named MyWebExtension.vb and in it there is this line of code #If _MyType <> "Empty" Then
I attempted to convert into C# #if (_MyType != "Empty")
When I run the application I get the following error:
Invalid preprocessor expression.
What did I do wrong with my conversion?
#define
in C# can only specify 'flags', not constant values as VB allows you to do.
In C#, you can have:
#define _MyType
#If _MyType
or:
#if ! _MyType
but in VB, you can have:
#Const _MyType = "SomeType"
#If _MyType = "SomeType"
There is no good work-around for this - you can use constants, but of course these can only be referenced from within a class.
The following sample shows some valid and invalid preprocessor expressions:
#if symbol // OK
#endif
#if !symbol // OK
#endif
#if (symbol) // OK
#endif
#if true // OK
#endif
#if false // OK
#endif
#if 1 // CS1517
#endif
#if ~symbol // CS1517
#endif
#if * // CS1517
#endif
class x
{
public static void Main()
{
}
}
So I think you can use like this;
#define _MyType
#If _MyType
From #if
When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it will compile the code between the directives only if the specified symbol is defined . Unlike C and C++, you cannot assign a numeric value to a symbol; the #if statement in C# is Boolean and only tests whether the symbol has been defined or not.
#define DEBUG
// ...
#if DEBUG
Console.WriteLine("Debug version");
#endif
You can use the operators == (equality), != (inequality) only to test for true or false . True means the symbol is defined . The statement -#if DEBUG has the same meaning as #if (DEBUG == true). The operators && (and), and || > (or) can be used to evaluate whether multiple symbols have been defined. You can also group symbols and operators with parentheses.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.