PC Review


Reply
Thread Tools Rate Thread

#define in C#

 
 
Alain Dekker
Guest
Posts: n/a
 
      9th May 2011
In C++ you can do this:
#define MY_MAGIC_NUMBER 5
....
if (nTheUserID == 5)
{
DoSomething();
}

but I can't find how to do that in C#. The MSDN documentation appears to
suggest that this is not possible like it was in C/C++. Is that right? Is
there any way to use the preprocessor to define macros in C#?

Thanks,
Alain


 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      9th May 2011
Alain Dekker wrote:
> In C++ you can do this:
> #define MY_MAGIC_NUMBER 5
> ...
> if (nTheUserID == 5)
> {
> DoSomething();
> }
>
> but I can't find how to do that in C#. The MSDN documentation appears to
> suggest that this is not possible like it was in C/C++. Is that right?


Yes that's right. And the use of #define for this purpose in C++ is an
abuse of the preprocessor anyway - with various side effects in case of
trivial names.

In C# as well as in C++ this should read:

const int MY_MAGIC_NUMBER = 5;


> Is
> there any way to use the preprocessor to define macros in C#?


No.


Marcel
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      9th May 2011
"Alain Dekker" <(E-Mail Removed)> wrote in message
news:iq8q2p$au4$(E-Mail Removed)...

> In C++ you can do this:
> #define MY_MAGIC_NUMBER 5
> ...
> if (nTheUserID == 5)
> {
> DoSomething();
> }
>
> but I can't find how to do that in C#. The MSDN documentation appears to
> suggest that this is not possible like it was in C/C++. Is that right? Is
> there any way to use the preprocessor to define macros in C#?


No. #define in C# is basically a Boolean. Either a symbol is defined or it
isn't; it holds no value. And there are no preprocessor macros whatsoever.


 
Reply With Quote
 
Alain Dekker
Guest
Posts: n/a
 
      9th May 2011
OK, thanks to all. The preprocessor macro feature in C++ was powerful...but
also could be very hard to debug. Its ok that C# doesn't do it, but I guess
its ok to mourn this feature's departure for a few hours...

Alain

"Jeff Johnson" <(E-Mail Removed)> wrote in message
news:iq8t18$k06$(E-Mail Removed)...
> "Alain Dekker" <(E-Mail Removed)> wrote in message
> news:iq8q2p$au4$(E-Mail Removed)...
>
>> In C++ you can do this:
>> #define MY_MAGIC_NUMBER 5
>> ...
>> if (nTheUserID == 5)
>> {
>> DoSomething();
>> }
>>
>> but I can't find how to do that in C#. The MSDN documentation appears to
>> suggest that this is not possible like it was in C/C++. Is that right? Is
>> there any way to use the preprocessor to define macros in C#?

>
> No. #define in C# is basically a Boolean. Either a symbol is defined or it
> isn't; it holds no value. And there are no preprocessor macros whatsoever.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Define name David Microsoft Excel Programming 4 9th Mar 2006 09:19 PM
define name =?Utf-8?B?c3Vuc2hpbmU=?= Microsoft Excel Misc 1 29th Mar 2005 01:37 AM
Define Value Fernando Peixito Microsoft Access Macros 1 3rd Feb 2004 11:50 AM
#define A.M Microsoft C# .NET 3 13th Jan 2004 08:31 PM
How to define this.... Alex Sanchez. Microsoft C# .NET 1 21st Nov 2003 04:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:57 PM.