Information Technology Reference
In-Depth Information
variables control whether they get called when callers are compiled. Using
the Conditional directive enables you to create libraries with debugging
features embedded. Those features can be enabled or disabled at runtime.
Yo u c a n a l s o c r e a t e m e t h o d s t h a t d e p e n d o n m o r e t h a n o n e e n v i r o n m e n t
variable. When you apply multiple conditional attributes, they are com-
bined with OR. For example, this version of CheckState would be called
when either DEBUG or TRACE is true:
[ Conditional ( "DEBUG" ),
Conditional ( "TRACE" )]
private void CheckState()
To c r e a t e a c o n s t r u c t u s i n g A N D, y o u n e e d t o d e fi n e t h e p r e p r o c e s s o r s y m -
bol yourself using preprocessor directives in your source code:
#if ( VAR1 && VAR2 )
#define BOTH
#endif
Ye s , t o c r e a t e a c o n d i t i o n a l r o u t i n e t h a t r e l i e s o n t h e p r e s e n c e o f m o r e t h a n
one environment variable, you must fall back on your old practice of #if.
All #if does is create a new symbol for you. But avoid putting any exe-
cutable code inside that pragma.
Then, you could write the old version of CheckState this way:
private void CheckStateBad()
{
// The Old way:
#if BOTH
Trace .WriteLine( "Entering CheckState for Person" );
// Grab the name of the calling routine:
string methodName =
new StackTrace ().GetFrame( 1 ).GetMethod().Name;
Debug .Assert(lastName != null ,
methodName,
"Last Name cannot be null" );
Debug .Assert(lastName.Length > 0 ,
methodName,
"Last Name cannot be blank" );
 
Search WWH ::




Custom Search