Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
#If bTrace Then
Trace.WriteLine("Begun db query at " & now())
#End If


In this code What's "#" means ? and why we enclosed the block by "#"?
Posted

"#" introduces a compiler instruction, in this case "#If" which if true, allows the compiler to process the code up to "#End If". If false, that code is ignored.

In your case, if bTrace is declared and true, then the "Trace.WriteLine" instruction is included in your code.

See here[^] or here[^]for more info.
 
Share this answer
 
v2
#if is know as a preprocessor directive. Your code means: if b.Trace is true, Compile the Trace line compiled, othewise don't. For example if your code had been:

if bTrace Then
  Trace.WriteLine("Begun db query at " & now())
End If


Then the above code will always be compiled, and the if condition evaluated. This causes a loss of performance.

Often #if is used to add code you only want to execute under a debug build, without a performance hit when live code is compiled. I avoid preprocessor directives, the performance gain isn't normally enough to justify the WTF factor and messy code, but I do use them on occaision.

See
#if C# Rerence MSDN
 
Share this answer
 
Comments
Layth Rafea 13-Nov-10 15:10pm    
Thank you so much, now i get it...
so I've a little question ? do you advice me to use "#" with every "if" ?
Keith Barrow 13-Nov-10 15:23pm    
No, you should only use #if when you *need* to prevent the compiler from compiling code *and* you know the condition (bTrace in your case) at compile time. You should *avoid* using #if otherwise, as it makes the code harder to understand.
Layth Rafea 13-Nov-10 17:03pm    
thank you again Kieth
if bTrace is equal true this line
Trace.WriteLine("Begun db query at " & now())

will compile if not will not compile
 
Share this answer
 
Any preprocesser directive starts with this character.
See here[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900