Ali Ersöz
Ali Ersöz

Reputation: 16086

Is there a way to set the value of #define on runtime?

I wonder if there is a way to set the value of #define in run time.

I assume that there is a query for Oracle specific and Sql Server specific at the code below.

#define oracle

// ...    

#if oracle
// some code
#else
// some different code.
#endif

Upvotes: 4

Views: 7771

Answers (4)

user7427029
user7427029

Reputation: 255

To be complete: These optimization slides for OpenCL imply at page 11 that it is possible, but note that the author seems to interpret "runtime" as the hull program's runtime. In this sense, the kernels (small programs for each GPU thread) are "compiled at runtime".

Upvotes: 0

Louis Gerbarg
Louis Gerbarg

Reputation: 43452

No, the preprocessor runs before compile and can alter code at that time, that is its purpose, if you want to switch behavior based on something at runtime use a variable and normal conditional logic.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062780

#if is compile-time. You could specify this in your build process (via switches to msbuild/csc), but not really at runtime. The excluded code doesn't exist. You might be better advised to (1 of):

  • Have separate DAL stacks for each back-end, using Dependency Injection / IoC
  • Use an ORM tool that supports either
  • Branch the code based n the provider (in a single DAL)

Upvotes: 2

Ana Betts
Ana Betts

Reputation: 74654

Absolutely not, #defines are compiled out by the preprocessor before the compiler even sees it - so the token 'oracle' isn't even in your code, just '1' or '0'. Change the #define to a global variable or (better) a function that returns the correct value.

Upvotes: 15

Related Questions