Once a file has been cut into tokens, a few lines that start with # are read before the syntax: they switch lines of code on and off, group them, and say where generated code came from. A directive is alone on its line (white space may come before the #, and a comment may follow). Unlike C there is no #define, no macros and no #include.
Conditional compilation
#Const Tracing = True
#Const Level = 3
#If DEBUG Then
Console.WriteLine("debug build")
#ElseIf Tracing AndAlso Level > 2 Then
Console.WriteLine("tracing")
#Else
Console.WriteLine("release")
#End If
#Const Name = expressiondefines a conditional constant in a space of its own for the file; it can be defined again later in the file, and before it is assigned it isNothing. The expression may use literals, other conditional constants, the operators (+ - * / \ Mod ^ & << >> = <> < > <= >= And Or Xor AndAlso OrElse Not), brackets, casts (CBool,CType,DirectCast,TryCast) andIf(a, b, c). No other names or calls are allowed.#If expr Then, any number of#ElseIf expr Then, an optional#Else, and#End Ifcompile the lines of the first group whose expression is true (theThenis optional). The other lines are skipped without being parsed, so they need not be valid (a disabled group may even contain code from a later language version). Groups nest. A line continuation does not extend a directive.- The compiler (or the project) predefines constants:
DEBUGandTRACEfrom the build settings,TARGET("exe","library","winexe","module"),VBC_VER(the compiler version),_MyType(the project type for the My namespace;"Empty"in the plain SDK console project built for this page), and, in SDK-style projects, the target-framework symbols (NET,NETCOREAPP,NET8_0,NET8_0_OR_GREATER,NETSTANDARD2_0…). Custom constants are set with/defineor<DefineConstants>.
Regions, external sources and warnings
#Region "Startup code"
Public Sub Run()
#Region "inside a method"
Dim unused As Integer
#End Region
#Disable Warning BC42024
Dim x As Integer
#Enable Warning BC42024
End Sub
#End Region
#ExternalSource("page.aspx", 30)
Public Sub FromPage()
Console.WriteLine("generated")
End Sub
#End ExternalSource
| Directive | Meaning |
|---|---|
#Region "text" … #End Region | a named group of lines that an editor can fold; no effect on the program. Regions nest and must respect the block structure; since version 14 they may also start and end inside a method body |
#ExternalSource("file", line) … #End ExternalSource | says that the lines inside came from the given line of another file (generated code, ASP.NET), for the debugger and error messages; no effect on compilation; cannot nest |
#ExternalChecksum("file", "{guid}", "hex checksum") | gives the checksum (an even number of hexadecimal digits) of a file named in an #ExternalSource; repeated directives for one file must agree |
#Disable Warning ids, #Enable Warning ids | turn compiler warnings (comma-separated ids such as BC42024 or CS… names) off and on for the lines between them (version 14) |
In the viewer
A directive is coloured as one unit (#If, #End If, #Region, #Disable Warning and so on) when the # is the first thing on a line; the rest of the line is read as code, so the string after #Region and the symbols in a condition keep their colours, and a string in a directive stops at the end of its line. A # inside a line that holds a date, #1/1/2000#, is a date literal. The viewer cannot know which constants are set, so both sides of an #If are read as code.
Not described
Known, not yet covered
- Reference: the full list of constants that each SDK and compiler version predefines, and the precise rules of
#Disable Warning(which warning identifiers and forms are accepted) were not checked. - Reference: Roslyn’s scanner also recognises
#Rand#Load(for script files) and the marker lines of unresolved version-control conflicts; they are not described.
See also
- File structure and options – where directives sit in a file
- Tooling –
/defineand project properties