Export (0) Print
Expand All

Special Characters in Code

At times, you need to use special characters in your code, that is, characters that are not on the standard alphanumeric list. The punctuation and special characters in the Visual Basic .NET character set have various uses, from organizing program text to defining the tasks that the compiler or the compiled program carries out. They do not specify an operation to be performed.

Parentheses

Use parentheses when defining a procedure, such as a Sub or Function. You must enclose all procedure arguments in parentheses. You also use parentheses for putting variables or arguments into logical groups.

Separators

Separators do what their name suggests: they separate sections of code. In Visual Basic, the separator character is the colon (:). Use separators when you want to place multiple statements on a single line instead of separate lines, saving space and improving the readability of your code. The following example shows three statements separated by colons (:):

a = 5: b = 10: c = 15

Concatenation

Use the "&" operator for concatenation, or linking strings together. Do not confuse it with the + operator, which adds together numeric values. Using the "+" operator to concatenate may cause incorrect results when operating on two numeric values. The following code provides an example:

Var1 = "10.01"
Var2 = 11
Result = Var1 + Var2   ' Result = 21.01
Result = Var1 & Var2   ' Result = 10.0111

Member Access Operators

To access a member of a type, you use the dot (.) or exclamation point (!) operator between the type name and the member name. The type can be an enumeration, structure, interface, or class, and the member can be a field, property, event, or method.

Dot (.) operator

Use the . operator as a member access operator for properties, events, fields, and methods, as in the following example:

Dim MyForm As New System.Windows.Forms.Form
MyForm.Text = "This is my form"   ' Accesses Text member (property) of Form class (on MyForm object).
MyForm.CenterToScreen()   ' Accesses CenterToScreen member (method) on MyForm.

Exclamation point (!) operator

Use the ! operator (in a class or interface only) as a dictionary access operator. The class or interface must have a default property that accepts a single String argument. The identifier immediately following the ! operator becomes the string argument to the default property, as in the following example:

Public Class HasDefault   ' Exposes a default property.
   Default Public ReadOnly Property Index(ByVal S As String) As Integer
      Get
         Return 1000 + CInt(S)
      End Get
   End Property   ' Index
End Class   ' HasDefault
' ...
Public Class TestHasDefault
   Public Sub CompareAccess()
      Dim HD As HasDefault = New HasDefault()
      Dim IndexStr As String = "5"
      MsgBox("Traditional access returns " & HD.Index(IndexStr) & vbCrLf & _
             "Default property access returns " & HD(IndexStr) & vbCrLf & _
             "Dictionary access returns " & HD!IndexStr)
   End Sub
End Class   ' TestHasDefault

The three output lines of MsgBox all display the value 1005. The first line uses the traditional access to property Index, the second makes use of the fact that Index is the default property of class HasDefault, and the third uses dictionary access to the class.

Note that the second operand of the ! operator must be a valid identifier or keyword. Therefore, the following change to the last line of the MsgBox call generates an error because "5" is not an identifier or keyword:

             "Dictionary access returns " & HD!"5")
Note   References to default collections must be explicit. In particular, you cannot use the ! operator on a late-bound variable.

The ! character is also used as the Single type character.

See Also

Program Structure and Code Conventions | Type Characters

Show:
© 2015 Microsoft