Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am porting some VB.NET code to C#.
Here is the VB.NET
VB
#If _MyType = "WindowsForms" Then
     'Some things to do ...
#End If

From what I have read C# will only deal with boolean #IF statements
Apparently it is because the compiler does not like this:
C#
#if _MyType = "WindowsForms"
      //Some things to do ...
#endif

How can I achieve the same results?
Posted
Updated 14-Feb-11 12:10pm
v3
Comments
Manfred Rudolf Bihy 14-Feb-11 18:10pm    
Edit: Added pre tags.

The C# equivalent would be to define WindowsForms and then to do this:

C#
#if WindowsForms

      // do stuff here

#endif


You can define a symbol either via #define or via the project settings.

Example of doing it in code:

C#
#define WindowsForms


[Edit]
~~~~~~~~~~

The #define has to come before any other tokens in the source file. Example:

C#
#define WindowsForms

using System;

namespace Nothing
{
    class Program
    {
        static void Main(string[] args)
        {
#if WindowsForms
            Console.WriteLine("WindowsForms defined");
#else
            Console.WriteLine("WindowsForms not defined");
#endif
        }
    }

}


Basically the #if check checks to see if the symbol is defined. It cannot check for a value. It only checks for definition.
 
Share this answer
 
v7
Comments
Sergey Alexandrovich Kryukov 14-Feb-11 17:21pm    
My 5,
--SA
Nish Nishant 14-Feb-11 17:23pm    
Thank you, SA.
Manfred Rudolf Bihy 14-Feb-11 17:46pm    
Proposed as answer! 5+
See my comment to your comment on my answer.
Nish Nishant 14-Feb-11 17:46pm    
>> See my comment to your comment on my answer. <<

:-)
Espen Harlinn 14-Feb-11 18:06pm    
My 5, and I'm still wishing for a "real" preprocessor :)
The details about preprocessor directives are here: http://msdn.microsoft.com/en-us/library/ed8yd1ha(v=VS.80).aspx[^]
If you read carefully on the site I posted you'll find that this page gives you an interview of all available operators: http://msdn.microsoft.com/en-us/library/4y6tbswk(v=vs.71).aspx[^]

That should fix your problems. ("==" for equality not just "=")

Cheers!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Feb-11 17:21pm    
My 5,
--SA
Nish Nishant 14-Feb-11 17:23pm    
While == is allowed I cannot see how it can be used. VB has a #const which C# does not have.
Manfred Rudolf Bihy 14-Feb-11 17:44pm    
So true! The MSDN page for 2005 had no hint but as I looked at the documentation of 2008 and 2010 they said in C# == can only be used for boolean compares. My bad! :(
Nish Nishant 14-Feb-11 17:48pm    
Yeah, and even the bool comparison doesn't really provide any useful feature in C# as far as I can see.

I guess this is one area where VB.NET has a slight edge over C#.
MacIntyre 14-Feb-11 17:38pm    
Ok - so far so good
The only problem is when I: #define WindowsForms = "WindowsForms";
the #define gets an error saying it can not be used after the first token
What does that mean and where does it go I tried several locations the would still be with in scope.
Thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900