Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conditional compilation: I want to compile functions on the basis of knowing or not knowing the type of the parameter value (without using /d or /define). Like #ifdef __XYZ_H__ in C++...

Posted on 2004-04-05
5
Medium Priority
?
381 Views
Last Modified: 2011-09-20
I'd like to build a class (Abc) that is shared between C# projects on source code basis. Some of these projects know a particular class (Xyz), some don't.
So when I make a function like Abc.DoSomething(Xyz xyz), it would result in a compile time error in the projects where Xyz is not known. How can I make source code that is only compiled when Xyz is known?
#define works only within the same source file and I'd like to avoid others to need more than including the source file of Abc to their project.

regards, Flea
(Don't beat me, if my English is too poor for you.)

P.S.: At least, there would be the possibility to make a #if ... #warning section...
0
Comment
Question by:Fleasoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 1600 total points
ID: 10755423
In C# you can include #ifs only for terms defined with #define or in the properties of the project. You cannot check the existance of a type declaration. So I would put an #if in the code and I would define the term in the project properties (Project->Properties->Configuration Properties->Build->Conditional Compilation Constants - should be added for both debug and release configuration). Then in the projects you include the type, you will define the term. For the projects where you don't define the type, you will not define the term.
0
 

Author Comment

by:Fleasoft
ID: 10755653
Okay, the solution of TheAvenger (thanks for contribution) was still known to me but I've searched for an easier to apply solution (see the sentence beginning with #define), where the programmer sharing the code file to his/her project has little effort to do. So perhaps there's a way to automatically add the define to project properties?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10755660
I don't think so :-(( Unforutnately the preprocessor of C# is not so powerful as it is in C++....
0
 

Author Comment

by:Fleasoft
ID: 10755951
Unfortunately I'm afraid I'll have to agree you :-(( They should have been thought on something like that.
Thanks anyway...

regards, Flea
0
 
LVL 5

Expert Comment

by:AdrianJMartin
ID: 10756036
As TheAvenger points out all the developer has to do is add the def to the "Conditional Compilation Section" in the projects properties...... it difficult to see how much easier it could be...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question