Sideway
output.to from Sideway
Draft for Information Only

Content

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

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.

Constants

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)

Source/Reference


©sideway

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

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 151

Software

Application 198

Digitization 25

Latex 9

Manim 121

Numeric 19

Programming

Web 283

Unicode 494

HTML 65

CSS 58

ASP.NET 174

OS 389

DeskTop 7

Python 19

Knowledge

Mathematics

Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Physics

Electric 27

Biology 1

Geography 1


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