A keyword is a reserved word with special meanings in the compiler, whose meaning cannot be changed. Therefore, these keywords cannot be used as an identifier in VB.NET programming such as class name, variable, function, module, etc.
In visual basic, there are two types of Keywords.
Reserved keywords cannot be used as names for programming elements such as variables, procedures, etc. You can bypass this restriction by enclosing the name in brackets ([]
).
Here is the list of the most commonly used reserved keywords.
AddHandler | AddressOf | Alias | And | AndAlso | As |
Boolean | ByRef | Byte | ByVal | Call | Case |
Catch | CBool | CByte | CChar | CDate | CDbl |
CDec | Char | CInt | Class | CLng | CObj |
Const | Continue | CSByte | CShort | CSng | CStr |
CType | CUInt | CULng | CUShort | Date | Decimal |
Declare | Default | Delegate | Dim | DirectCast | Do |
Double | Each | Else | ElseIf | End | EndIf |
Enum | Erase | Error | Event | Exit | False |
Finally | For | For Each…Next | Friend | Function | Get |
GetType | GetXMLNamespace | Global | GoSub | GoTo | Handles |
If | Implements | Imports | In | Inherits | Integer |
Interface | Is | IsNot | Let | Lib | Like |
Long | Loop | Me | Mod | Module | MustInherit |
MustOverride | MyBase | MyClass | Namespace | Narrowing | New |
Next | Not | Nothing | NotInheritable | NotOverridable | Object |
Of | On | Operator | Option | Optional | Or |
OrElse | Out | Overloads | Overridable | Overrides | ParamArray |
Partial | Private | Property | Protected | Public | RaiseEvent |
ReadOnly | ReDim | REM | RemoveHandler | Resume | Return |
SByte | Select | Set | Shadows | Shared | Short |
Single | Static | Step | Stop | String | Structure |
Sub | SyncLock | Then | Throw | To | True |
Try | TryCast | TypeOf…Is | UInteger | ULong | UShort |
Using | Variant | Wend | When | While | Widening |
With | WithEvents | WriteOnly | Xor | #Const | #Else |
#ElseIf | #End | #If |
Unreserved keywords can be used as names for your programming elements. However, it is not recommended because it can make your code hard to read and can lead to subtle errors that can be difficult to find.
Aggregate | Ansi | Assembly | Async | Auto | Await |
Binary | Compare | Custom | Distinct | Equals | Explicit |
From | Group By | Group Join | Into | IsFalse | IsTrue |
Iterator | Join | Key | Mid | Off | Order By |
Preserve | Skip | Skip While | Strict | Take | Take While |
Text | Unicode | Until | Where | Yield | #ExternalSource |
#Region |
If you want to use Keywords as variable names (identifiers), then we need to enclose the variable name in brackets ([]). For example, [Class] is a valid identifier, but Class is not because it’s a keyword and having a special meaning for the compiler.
The following example shows how to use the reserve keywords as a variable name by enclosing the variable name with brackets ([]
).
Imports System
Module Program
Public Class [Class]
Public name As String
End Class
Sub Main()
Dim p1 As [Class] = New [Class]()
p1.name = "John"
Console.WriteLine("Age: " & p1.name)
End Sub
End Module
As you can see a Class
keyword is used as a variable name [Class]
by enclosing it with brackets ([]
).