In visual basic, Keywords are the predefined set of reserved words that have special meaning for the compiler. So the keywords in visual basic cannot be used as identifiers such as variable name, class name, etc., in our applications.
In visual basic, Keywords are differentiated into two types those are
Generally, the reserved keywords will not allow us to use them as names for programming elements such as variables, class, etc. In case, if we want to bypass this restriction, we need to define the variable name in brackets ([]
).
The following table lists the available reserved keywords in the Visual Basic programming language.
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 | #Else |
In visual basic, unreserved keywords are not reserved keywords which means we can use them as names for programming elements such as variables, class, etc. However, it’s not recommended to use the keywords as programming elements because it will make your code hard to read and lead to subtle errors that can be difficult to find.
The following table lists unserved keywords in a visual basic programming language.
Aggregate | Ansi | Assembly | Async | Auto |
Await | Binary | Compare | Custom | Distinct |
Equals | Explicit | 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 |
In case 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 has a special meaning for the compiler.
Following is the example of using the reserve keywords as a variable name by enclosing the variable name with brackets ([]
) in visual basic.
If you observe the above example, we used a Class
keyword as a variable name ([Class]
) by enclosing it with brackets ([]
).
When we execute the above visual basic program, we will get the result below.
This is how we can use the keywords as a variable name in visual basic based on our requirements.