Information Technology Reference
In-Depth Information
condition and stops the program if that condition is false. The remaining
parameters define messages that will be printed if the condition is false.
Tr a c e . Wr i t e L i n e w r i t e s d i a g n o s t i c m e s s a g e s t o t h e d e b u g c o n s o l e . S o , t h i s
method writes messages and stops the program if a person object is invalid.
Yo u w o u l d c a l l t h i s m e t h o d i n a l l y o u r p u b l i c m e t h o d s a n d p r o p e r t i e s a s
a precondition and a post-condition:
public string LastName
{
get
{
CheckState();
return lastName;
}
set
{
CheckState();
lastName = value ;
CheckState();
}
}
CheckState fires an assert the first time someone tries to set the last name
to the empty string, or null. Then you fix your set accessor to check the
parameter used for LastName. It's doing just what you want.
But this extra checking in each public routine takes time. You'll want to
include this extra checking only when creating debug builds. That's where
the Conditional attribute comes in:
[ Conditional ( "DEBUG" )]
private void CheckState()
{
// same code as above
}
The Conditional attribute tells the C# compiler that this method should be
called only when the compiler detects the DEBUG environment variable.
The Conditional attribute does not affect the code generated for the
CheckState() function; it modifies the calls to the function. If the DEBUG
symbol is defined, you get this:
 
Search WWH ::




Custom Search