Sideway from Sideway
Draft for Information Only


VB.NET Constants and  Literals
  VB.NET Constants
   Types of Constants
   Scope of a Constant
   Compile-time and Run-time Constants
    Conditional Compilation Constants
    Print and Display Constants
  VB.NET Literals
   Literals and Type Coercion

VB.NET Constants and  Literals

VB.NET Constants

A constant is a meaningful name that takes the place of a number or string that does not change. Constants store values that, as the name implies, remain the same throughout the execution of an application.

Types of Constants

Visual Basic contains a number of predefined constants, mainly using for printing and displaying. New constants can also be created with the Const statement, using the same guidelines as for creating a variable name. If Option Strict is On, the constant type must be explicitly declared. Besides, the value of a constants cannot be modified or no new value can be  assigned to a constant.

Scope of a Constant

The scope of a constant scope, which is the set of all code that can refer to it without qualifying its name, is the same as that of a variable declared in the same location. To create a constant that exists within the scope of a particular procedure, declare it inside that procedure. To create a constant that is available throughout an application, declare it using the Public keyword in the declarations section of the class.

Compile-time and Run-time Constants

A compile-time constant is computed at the time the code is compiled, while a run-time constant can only be computed while the application is running. A compile-time constant will have the same value each time an application runs, while a run-time constant may change each time. Compile-time constants are required for cases such as array bounds, case expressions, or enumerator initializers.


Conditional Compilation Constants

The predefined constants available for conditional compilation.

Constant Description
CONFIG A string that corresponds to the current setting of the Active Solution Configuration box in the Configuration Manager.
DEBUG A Boolean value that can be set in the Project Properties dialog box. By default, the Debug configuration for a project defines DEBUG. When DEBUG is defined, Debug class methods generate output to the Output window. When it is not defined, Debug class methods are not compiled and no Debug output is generated.
TARGET A string representing the output type for the project or the setting of the command-line /target option. The possible values of TARGET are:

- "winexe" for a Windows application.
- "exe" for a console application.
- "library" for a class library.
- "module" for a module.
- The /target option may be set in the Visual Studio integrated development environment. For more information, see /target (Visual Basic).
TRACE A Boolean value that can be set in the Project Properties dialog box. By default, all configurations for a project define TRACE. When TRACE is defined, Trace class methods generate output to the Output window. When it is not defined, Trace class methods are not compiled and no Trace output is generated.
VBC_VER A number representing the Visual Basic version, in major.minor format.

Print and Display Constants

The predefined constants available for calling print and display functions in code in place of the actual values.

Constant Description
vbCrLf Carriage return/linefeed character combination.
vbCr Carriage return character.
vbLf Linefeed character.
vbNewLine Newline character.
vbNullChar Null character.
vbNullString Not the same as a zero-length string (""); used for calling external procedures.
vbObjectError Error number. User-defined error numbers should be greater than this value. For example:

Err.Raise(Number) = vbObjectError + 1000
vbTab Tab character.
vbBack Backspace character.
vbFormFeed Not used in Microsoft Windows.
vbVerticalTab Not useful in Microsoft Windows.

VB.NET Literals

A literal is a string that is expressed as itself to represent a value within the text of a program.  A constant is a meaningful name that takes the place of a literal and retains this same value throughout the program, as opposed to a variable, whose value may change.

When Option Infer is Off and Option Strict is On, you must declare all constants explicitly with a data type. When Option Infer is On or Option Strict is Off, you can declare a constant without specifying a data type with an As clause. The compiler determines the type of the constant from the type of the expression. A numeric integer literal is cast by default to the Integer data type. The default data type for floating-point numbers is Double, and the keywords True and False specify a Boolean constant.

Literals and Type Coercion

In some cases, you might want to force a literal to a particular data type; You can coerce a literal to a particular data type in two ways: by appending a type character to it, or by placing it within enclosing characters. A type character or enclosing characters must immediately precede and/or follow the literal, with no intervening space or characters of any kind.

The following table shows the enclosing characters and type characters available in Visual Basic.

Data type Enclosing character Appended type character
Boolean (none) (none)
Byte (none) (none)
Char " C
Date # (none)
Decimal (none) D or @
Double (none) R or #
Integer (none) I or %
Long (none) L or &
Short (none) S
Single (none) F or !
String " (none)



ID: 200700021 Last Updated: 21/7/2020 Revision: 0 Ref:



  1. Active Server Pages,  ,
  2. ASP Overview,  ,
  3. ASP Best Practices,  ,
  4. ASP Built-in Objects,  ,
  5. Response Object,  ,
  6. Request Object,  ,
  7. Server Object (IIS),  ,
  8. Application Object (IIS),  ,
  9. Session Object (IIS),  ,
  10. ASPError Object,  ,
  11. ObjectContext Object (IIS),  ,
  12. Debugging Global.asa Files,  ,
  13. How to: Debug Global.asa files,  ,
  14. Calling COM Components from ASP Pages,  ,
  15. IIS ASP Scripting Reference,  ,
  16. ASP Keywords,  ,
  17. Creating Simple ASP Pages,  ,
  18. Including Files in ASP Applications,  ,
  19. ASP Overview,  ,
  20. FileSystemObject Object,  ,
  21.,  , ADO Object Model
  22. ADO Fundamentals,  ,

Home 5






Hobbies 8


Chinese 1097

English 337

Reference 67


Hardware 151


Application 198

Digitization 25

Latex 9

Manim 121

Numeric 19


Web 283

Unicode 494


CSS 58


OS 389

DeskTop 7

Python 19



Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2


Electric 27

Biology 1

Geography 1

Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019