#define on multiple files

D

David Young

Hello all,

I'm quite new to C# (< 6 months) but really love it and is my language of
choice ..... but I have one question I've not been able to find out .....

In C++ a #define label in one file can be referanced in other *.cpp files,
however in C# if I have say #define SPEED it only refers to the class(es)
in that *.cs file.

Is there anyway to have one #define label work with multiple *.cs files?

Thank you for your time,

David
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Define it in the project level, in the project properties / Configuration
properties / build / Conditional compilation constants

One problem I have faced is that you cannot define a symbol solution wide, I
would like to have that feature implemented sometimes in the future.


Cheers,
 
D

David Young

Cheers Ignacio, that's the trick!!!!

David

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Define it in the project level, in the project properties / Configuration
properties / build / Conditional compilation constants

One problem I have faced is that you cannot define a symbol solution wide, I
would like to have that feature implemented sometimes in the future.


Cheers,
 
J

James Curran

Why do you want a #define at all? #defines cause as many problems as
they solve, which is why their functionality is reduced in C# (and they are
working on reducing it in C++)

My guess is that you have some code like the following:

#if SPEED
// fast way of doing something
#else
// slow/careful way of doing that thing.
#endif

IF so, then you should note that if you have a class like:

class Configuration
{
public const bool Speed = true;
}

then you can write code like this:

if (Configuration.Speed)
{
// fast way of doing something
}
else
{
// slow/careful way of doing that thing.
}


This will be compiled PRECISELY the way the top code way, with code
being generated just the "true" portion of the if(). (This is a requirement
of the C# language)


--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
D

David Young

Fully agree with you, but I need a "global" switch where I can change in one
place rather than going through many *.cs files setting the bool flag on
each class (and possibly missing the odd one!)

David


James Curran said:
Why do you want a #define at all? #defines cause as many problems as
they solve, which is why their functionality is reduced in C# (and they are
working on reducing it in C++)

My guess is that you have some code like the following:

#if SPEED
// fast way of doing something
#else
// slow/careful way of doing that thing.
#endif

IF so, then you should note that if you have a class like:

class Configuration
{
public const bool Speed = true;
}

then you can write code like this:

if (Configuration.Speed)
{
// fast way of doing something
}
else
{
// slow/careful way of doing that thing.
}


This will be compiled PRECISELY the way the top code way, with code
being generated just the "true" portion of the if(). (This is a requirement
of the C# language)


--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

I'm quite new to C# (< 6 months) but really love it and is my language of
choice ..... but I have one question I've not been able to find out ......

In C++ a #define label in one file can be referanced in other *.cpp files,
however in C# if I have say #define SPEED it only refers to the class(es)
in that *.cs file.

Is there anyway to have one #define label work with multiple *.cs files?
 
J

James Curran

Look again at my code.

You change it once (in the Configuration class ), and it is global
throughout your code.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

David Young said:
Fully agree with you, but I need a "global" switch where I can change in one
place rather than going through many *.cs files setting the bool flag on
each class (and possibly missing the odd one!)

David


James Curran said:
Why do you want a #define at all? #defines cause as many problems as
they solve, which is why their functionality is reduced in C# (and they are
working on reducing it in C++)

My guess is that you have some code like the following:

#if SPEED
// fast way of doing something
#else
// slow/careful way of doing that thing.
#endif

IF so, then you should note that if you have a class like:

class Configuration
{
public const bool Speed = true;
}

then you can write code like this:

if (Configuration.Speed)
{
// fast way of doing something
}
else
{
// slow/careful way of doing that thing.
}


This will be compiled PRECISELY the way the top code way, with code
being generated just the "true" portion of the if(). (This is a requirement
of the C# language)


--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

I'm quite new to C# (< 6 months) but really love it and is my language of
choice ..... but I have one question I've not been able to find out .....

In C++ a #define label in one file can be referanced in other *.cpp files,
however in C# if I have say #define SPEED it only refers to the class(es)
in that *.cs file.

Is there anyway to have one #define label work with multiple *.cs
files?
 
J

Jon Skeet [C# MVP]

This will be compiled PRECISELY the way the top code way, with code
being generated just the "true" portion of the if(). (This is a requirement
of the C# language)

It's not quite the same though - the code which doesn't actually get
put in the assembly still needs to be valid C#, whereas with a #if it
doesn't. I agree that most of the time that isn't a problem, but in
some cases it might be (for instance, if you want to be able to
conditionally compile against code which doesn't exist in a certain
configuration).

It also doesn't work for things like #if-ing attributes, which can be
handy sometimes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top