Statements

Comments

Comments are defined using single quotes '. The comment starts at the single quote and continues until the end of the line (Wasabi doesn’t have block comments):

' This is a comment.
' It doesn't do anything

... remember that good comments explain the rational for doing something rather than how the code works. If you find yourself writing comments explaining how your code works it’s probably too complicated and/or magical and will be difficult for you (or me) to debug later. You should reconsider your variable and method names and your algorithmic approach so that it’s more obvious to lesser minds (like mine) what’s going on and. Always keep in mind this sage advice from Kernighan:

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian W. Kernighan

Const

The Const keyword allows you to define compile time constants which are restricted to value types and Strings:

Const COLWIDTH_LONG_TEXT = 350

... by convention constants have UNDERSCORE_SEPARATED_UPPER_CASE_NAMES.

Doc Comments

Doc comments are defined using triple single quotes '''. They can be extracted by tools and are very useful for documenting public APIs. Unlike normal comments, and abundance of doc comments is not an indication that your code is too complicated. In fact, all public API should have accompanying doc comments that explain how the API is used

''' This is an abbreviated doc comment
Public Method()

''' <summary>This is a long form doc comment</summar>
''' <param name="param1">A description parameter.</param>
''' <returns>A description of the return value</returns>
Public OtherMethod(param1)

Dim

Note

Identifiers are really important, so important that code complete has an entire chapter on them. While you should totally read it, the short version is that identifiers should:

  • Be descriptive.
  • Not overly long.
  • Avoid unnecessary abbreviation.

The Dim keyword allows you to define variables:

Dim x = 5

... variables may optionally include type annotations:

Dim x = 5 As Int32

... by convention variables have camelCaseNames.

Do While

Note

Do While loops supersede the older While-WEnd style loops and should be used in preference to them.

While loops are defined using the Do While keywords:

Do While iterator.MoveNext()
  ' Do stuff
Loop

Do While loops can exit early using the Exit Do keyword:

Do While iterator.MoveNext()
  ' Do stuff

  If <condition> Then
    Exit Do
  End If
Loop

For

Note

Where they are applicable, For Each loops are preferable to For loops.

C-style for loops are defined using the For keyword with the bounds defined using the <lower> To <upper> syntax. The bounds are inclusive so:

For Dim i = 0 To 5
  %><%= i %> <%
Next

... produces:

0 1 2 3 4 5

The step size is 1 by default, but can be adjusted using the optional Step keyword:

For Dim i = 0 To 5 Step 2
  %><%= i %> <%
Next

... which produces:

0 2 4

For loops can be exited early using the Exit For statement:

For Dim i = 0 To 5 Step 2
        If i > 2 Then
                Exit For
        End If

        %><%= i %><%
Next

... which produces:

0 2

... however, there is not equivalent to a continue statement.

For Each

Python-style for loops are defined using the For Each keywords and lets you iterate over the elements of a collection:

For Each Dim i In Array(0, 1, 2, 3, 4)
  %><%= i %> <%
Next

... produces:

0 1 2 3 4 5

Like the For loop, you can exit early using the Exit For statement.

If

If statements allow you to conditionally execute code. Like so:

If x < 0 Then
  %>Negative<%
ElseIf x = 0 Then
  %>Zero<%
Else
  %>Positive<%
End If

The Else clause is optional and you may have any number, including zero, ElseIf clauses.

On Error Resume Next

Warning

This statement is deprecated. Use Try-Catch blocks instead.

Inherited from VBScript, On Error Resume Next blocks translate exceptions into error codes which can be handled using If statements that check Err.Number:

On Error Resume Next
  ' Do stuff
  If Err.Number <> 0 Then
    ' Handle error
  End If
On Error Goto 0

On Error Resume Next create blocks where when an exception occurs, Err.Number is set to a non-zero value and execution resumes on the following line. On Error Goto 0 ends the corresponding On Error Resume Next block.

On Exit

Warning

This statement is deprecated. Use Try-Finally blocks instead.

The On Exit statement defers execution of the following statement until execution leaves the enclosing scope, similar to a Finally block:

On Exit CloseRS rs

Select Case

Note

Since Wasabi’s Select Case construct doesn’t allow fall through and doesn’t compile to anything special, you’re generally better off using If-ElseIf statements which are both more flexible and more common.

Wasabi supports switch statements using the Select Case statement with the default case being specified using the Case Else construct. For example:

Select Case char
  Case "'"
    Return "&apos;"
  Case """"
    Return "&quot;"
  Case "<"
    Return "&lt;"
  Case ">"
    Return "&gt;"
  Case "&"
    Return "&amp;"
  Case Else
    Return char
End Select

Even though fall through is not permitted and exit is assumed at the end of each case, early exit from an Case can be accomplished using the Exit Select statement:

Select Case adjective
  Case "happy"
    If predicate Then
      Exit Select
    End If

    state = "happy"
  Case Else
    state = "ambivalent"
End Select

Furthermore, each Case can specify multiple matching expressions:

Case "hello", "salutations", "etc."

Set

Warning

This statement is deprecated and unnecessary. Don’t use it.

Inherited from VBScript, the Set keyword was used to distinguish assignment from equality comparisons which both use the = operator:

Dim x
Set x = 1

Wasabi removes the need to distinguish between assignment and equality comparison as such the Set statement should not be used.

Throw

Exceptions can be raised using the Throw statement:

Throw New [System.Exception]("An error occurred")

When an exception is thrown, the runtime will unwind the stack until an appropriate Try-Catch block is found and resume execution in the Catch block.

Try

Try statements are used handling exceptions thrown using the Throw statement:

Try
  ' Do stuff
Catch arg As [System.ArgumentException]
  ' Handle argument exception
Catch io As [System.IOException]
  ' Exceptions can be rethrown maintaining their stack
  Throw
Catch ex As [System.Exception]
  ' Handle exception
Finally
  ' Clean up
End Try

The Finally clause is optional and you may have any number of Catch clauses for handling different types of exceptions.

While

Warning

This statement is deprecated. Use Do While loops instead.

Old style while loops inherited from VBScript:

While itertator.MoveNext()
  ' Do Stuff
WEnd

Use Do While loops instead.

With

The With statement allows abbreviated access to an object’s properties:

With "Hello world!"
  %>
    <%= .Length %>
    <br>
    <%= .ToString() %>
  %>
End With

... produces:

12
Hello world!

Since member access are unambiguously identified by the leading ., Wasabi’s with statement doesn’t suffer from the same ambiguities as JavaScript’s with statement with respect to assignment.

The With statement doesn’t provide any means to access the object so, if you need access you can simply assign the object to a local variable:

Dim obj = <expression>
With obj
  %><% obj.ToString() %><%
  objs.Add(obj)
End With

Table Of Contents

Previous topic

Expressions

Next topic

Functions & Subroutines

This Page