Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I needed to define a preprocessor directive, that abstracts some conditions.

I want to detect platform File I/O support.

So i want to define a preprocessor directive like FILE_IO to detect does this platforms supports or not.

I needed a preprocessor directive because i can't use File api on unsupported platforms, so i need to stop using them at compile time.

Take a look the below script.

How can i achieve this?

Any help is appreciated.

What I have tried:

C#
// It won't work
#define FILE_IO = !SAMSUNG_TV && !WEBGL

// OR

#if !SAMSUNG_TV && !WEBGL

// It get defined anyway
#define FILE_IO

#endif
Posted
Updated 24-Jul-17 3:02am
v2
Comments
[no name] 24-Jul-17 8:57am    
a.) The first one is wrong. See e.g. #if (C# Reference) | Microsoft Docs[^], 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

b.) The second one Looks pretty ok. Maybe you should first make sure that it is undefined:
#undef FILE_IO
#if !SAMSUNG_TC && !WEBGL ....
EmpireWorld 24-Jul-17 8:59am    
Can i assign boolean value to #define?

Can i use #define inside of #if?
Dave Kreskowiak 24-Jul-17 9:07am    
#define directives have no value at all. You can only check if they are defined or not.
[no name] 24-Jul-17 9:10am    
In c# you control it whether a Symbol exists or not. There is no Need to assign a bool or any other value.

What I know you can use #define inside an #if.

In your case I guess, that FILE_IO is allready defined, maybe not by you. Therefore make sure it is undefined before you make your tests:

#undef FILE_IO
#if !SAMSUNG_TV && !WEBGL
#define FILE_IO
#endif

... should do it
EmpireWorld 24-Jul-17 9:13am    
FILE_IO isn't defined, i want to define it in my own code.

Let me try the #define inside #if.

1 solution

#define SAMSUNG_TC
#define WEBGL
#if !SAMSUNG_TC && !WEBGL
#define FILE_IO
FILE_IO = !SAMSUNG_TC && !WEBGL;
#endif
 
Share this answer
 
Comments
EmpireWorld 24-Jul-17 9:15am    
The line FILE_IO = !SAMSUNG_TC && !WEBGL; does not works.
[no name] 24-Jul-17 9:27am    
Yes it does not work. Again a link to the documentation of MS which is worth to read it: #if (C# Reference) | Microsoft Docs[^]

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