Skip to main content

VB Option Explicit and Strict in Biml

The Verbose Default​

Visual Basic in BimlScript historically required explicit type declarations everywhere, even on values whose type the compiler could trivially infer. A simple loop counter still needed an As Integer clause:

<#@ template Language="VB" #>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<# for n as integer = 1 to 25 #>
<Package Name="GeneratedPkg<#= n #>" />
<# next #>
</Packages>
</Biml>

That was workable but added clutter, and it forced beginners to memorize Biml type names like AstTableNode before they could write basic loops.

Two New Template Attributes​

The template directive accepts two attributes that relax these requirements: optionexplicit and optionstrict. Setting either to false lets BimlScript infer types where it can.

<#@ template Language="VB" optionexplicit="false" optionstrict="false" #>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<# for n = 1 to 25 #>
<Package Name="GeneratedPkg<#= n #>" />
<# next #>
</Packages>
</Biml>

Both attributes map to the standard Visual Basic Option Explicit and Option Strict statements. The Microsoft documentation for those statements covers the exact compiler effects in detail.

Practical Wins​

The benefit shows up in two places. First, readability: short loops and assignments stop being dominated by type annotations. Second, ramp up: a new author can write the following without needing to know that the value coming back from RootNode.Tables(0) is an AstTableNode.

<# tbl = RootNode.Tables(0) #>

Compared to the older form below, the result is the same and the author can stay focused on the Biml shape rather than the SDK type names.

<# Dim tbl As AstTableNode = RootNode.Tables(0) #>

When stricter compile time checks are wanted, leave the defaults in place or set the attributes to true explicitly. Mixing both modes across files is fine: the attributes apply per template.