views:

189

answers:

2

I'm writing an application which has two classes that provide basically the same functionality but for different situations. I'd like to have three versions of the software - one where the user can change an ini file to configure the program to use one of the two classes, and then one version that only uses one of the two classes.

Right now I have it working via an ini file, but I'd like to be able to build versions that don't include the code for the unneeded class at all.

What is the best way to go about this? My current line of thinking is that since both classes derive from a common interface I'll just add a compile time conditional that looks at the active build configuration and decides whether to compile that class. What is the syntax to do that?

Thanks in advance for your help and input!

A: 

Better put each derived class in its own assembly. The at runtime, load the assembly which is depoyed with your application, and cast to the base interface or abstract class.

Sunny
So right now I have one interface and two derived classes. You think I should pull the implementation out of the main code and put it in to two assemblies and then check which ones are there are runtime? How would I do this? Thanks for you help!
evan
NM, I think I figured it out, thanks!
evan
+1  A: 

You can use conditional compilation symbols, which are similar to what you would do in C/C++ with precompiler directives.

For instance:

interface IThing {
}

#if PLATFORM_A

class ThingImplementation : IThing {
}

#endif

#if PLATFORM_B

class ThingImplementation : IThing {
}

#endif

You can pick the desider implementation by adding a conditional compilation symbol to the "Build" tab in your project's settings.

For a very basic implementation you do not need an interface at all in fact, just remove the unneeded code based on the platform you are compiling for (even if an interface is useful to enforce consistency).

class Thing {

#if PLATFORM_A

    Platform specific code

#endif

#if PLATFORM_B

    Platform specific code

#endif

}
Lck
how can i use the build type as the conditional compilation symbol. for example with out adding new build types, how would i just do something like if DEBUG or if RELEASE. thanks for your answer!
evan
The DEBUG constant is added by Visual Studio when using the default "Debug" build configuration. Therefore, you can use #if DEBUG without having to define extra symbols (note that the release configuration does not define RELEASE, but instead simply doesn't define DEBUG). For extra symbols, you'll have to manually add them to the build configurations that you create (in the "Build" tab of your VS project). I don't think there is a way to check for the build configuration's name when using the #if directives.
Lck