VB.Net - Constants and Enumerations
The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.
Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.
The constants are treated just like regular variables except that their values cannot be modified after their definition.
An enumeration is a set of named integer constants.
Declaring Constants
In VB.Net, constants are declared using the Const statement. The Const statement is used at module, class, structure, procedure, or block level for use in place of literal values.The syntax for the Const statement is:
[ < attributelist> ] [ accessmodifier ] [ Shadows ] Const constantlistWhere,
- attributelist: specifies the list of attributes applied to the constants; you can provide multiple attributes separated by commas. Optional.
- accessmodifier: specifies which code can access these constants. Optional. Values can be either of the: Public, Protected, Friend, Protected Friend, or Private.
- Shadows: this makes the constant hide a programming element of identical name in a base class. Optional.
- Constantlist: gives the list of names of constants declared. Required.
constantname [ As datatype ] = initializer
- constantname: specifies the name of the constant
- datatype: specifies the data type of the constant
- initializer: specifies the value assigned to the constant
'The following statements declare constants.' Const maxval As Long = 4999 Public Const message As String = "HELLO" Private Const piValue As Double = 3.1415
Example
The following example demonstrates declaration and use of a constant value:Module constantsNenum Sub Main() Const PI = 3.14149 Dim radius, area As Single radius = 7 area = PI * radius * radius Console.WriteLine("Area = " & Str(area)) Console.ReadKey() End Sub End ModuleWhen the above code is compiled and executed, it produces the following result:
Area = 153.933
Print and Display Constants in VB.Net
VB.Net provides the following print and display constants: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. |
Declaring Enumerations
An enumerated type is declared using the Enum statement. The Enum statement declares an enumeration and defines the values of its members. The Enum statement can be used at the module, class, structure, procedure, or block level.The syntax for the Enum statement is as follows:
[ < attributelist > ] [ accessmodifier ] [ Shadows ] Enum enumerationname [ As datatype ] memberlist End EnumWhere,
- attributelist: refers to the list of attributes applied to the variable. Optional.
- asscessmodifier: specifies which code can access these enumerations. Optional. Values can be either of the: Public, Protected, Friend or Private.
- Shadows: this makes the enumeration hide a programming element of identical name in a base class. Optional.
- enumerationname: name of the enumeration. Required
- datatype: specifies the data type of the enumeration and all its members.
- memberlist: specifies the list of member constants being declared in this statement. Required.
[< attribute list>] member name [ = initializer ]Where,
- name: specifies the name of the member. Required.
- initializer: value assigned to the enumeration member. Optional.
Enum Colors red = 1 orange = 2 yellow = 3 green = 4 azure = 5 blue = 6 violet = 7 End Enum
Example
The following example demonstrates declaration and use of the Enum variable Colors:Module constantsNenum Enum Colors red = 1 orange = 2 yellow = 3 green = 4 azure = 5 blue = 6 violet = 7 End Enum Sub Main() Console.WriteLine("The Color Red is : " & Colors.red) Console.WriteLine("The Color Yellow is : " & Colors.yellow) Console.WriteLine("The Color Blue is : " & Colors.blue) Console.WriteLine("The Color Green is : " & Colors.green) Console.ReadKey() End Sub End ModuleWhen the above code is compiled and executed, it produces the following result:
The Color Red is: 1 The Color Yellow is: 3 The Color Blue is: 6 The Color Green is: 4
VB.Net - Statements
A statement is a complete instruction in Visual Basic
programs. It may contain keywords, operators, variables, literal values,
constants and expressions. Statements could be categorized as:
- Declaration statements - these are the statements where you name a variable, constant, or procedure, and can also specify a data type.
- Executable statements - these are the statements, which initiate actions. These statements can call a method or function, loop or branch through blocks of code or assign values or expression to a variable or constant. In the last case, it is called an Assignment statement.
Declaration Statements
The declaration statements are used to name and define procedures, variables, properties, arrays, and constants. When you declare a programming element, you can also define its data type, access level, and scope.The programming elements you may declare include variables, constants, enumerations, classes, structures, modules, interfaces, procedures, procedure parameters, function returns, external procedure references, operators, properties, events, and delegates.
Following are the declaration statements in VB.Net:
S.N | Statements and Description | Example |
---|---|---|
1 |
Dim Statement Declares and allocates storage space for one or more variables. |
Dim number As Integer Dim quantity As Integer = 100 Dim message As String = "Hello!" |
2 |
Const Statement Declares and defines one or more constants. |
Const maximum As Long = 1000 Const naturalLogBase As Object = CDec(2.7182818284) |
3 |
Enum Statement
Declares an enumeration and defines the values of its members. |
Enum CoffeeMugSize Jumbo ExtraLarge Large Medium Small End Enum |
4 |
Class Statement Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises. |
Class Box Public length As Double Public breadth As Double Public height As Double End Class |
5 |
Structure Statement Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises. |
Structure Box Public length As Double Public breadth As Double Public height As Double End Structure |
6 |
Module Statement Declares the name of a module and introduces the definition of the variables, properties, events, and procedures that the module comprises. |
Public Module myModule Sub Main() Dim user As String = InputBox("What is your name?") MsgBox("User name is" & user) End Sub End Module |
7 |
Interface Statement Declares the name of an interface and introduces the definitions of the members that the interface comprises. |
Public Interface MyInterface Sub doSomething() End Interface |
8 |
Function Statement Declares the name, parameters, and code that define a Function procedure. |
Function myFunction (ByVal n As Integer) As Double Return 5.87 * n End Function |
9 |
Sub Statement Declares the name, parameters, and code that define a Sub procedure. |
Sub mySub(ByVal s As String) Return End Sub |
10 |
Declare Statement Declares a reference to a procedure implemented in an external file. |
Declare Function getUserName Lib "advapi32.dll" Alias "GetUserNameA" ( ByVal lpBuffer As String, ByRef nSize As Integer) As Integer |
11 |
Operator Statement Declares the operator symbol, operands, and code that define an operator procedure on a class or structure. |
Public Shared Operator + (ByVal x As obj, ByVal y As obj) As obj Dim r As New obj ' implemention code for r = x + y Return r End Operator |
12 |
Property Statement Declares the name of a property, and the property procedures used to store and retrieve the value of the property. |
ReadOnly Property quote() As String Get Return quoteString End Get End Property |
13 |
Event Statement Declares a user-defined event. |
Public Event Finished() |
14 |
Delegate Statement Used to declare a delegate. |
Delegate Function MathOperator( ByVal x As Double, ByVal y As Double ) As Double |
Executable Statements
An executable statement performs an action. Statements calling a procedure, branching to another place in the code, looping through several statements, or evaluating an expression are executable statements. An assignment statement is a special case of an executable statement.Example The following example demonstrates a decision making statement:
Module decisions Sub Main() 'local variable definition ' Dim a As Integer = 10 ' check the boolean condition using if statement ' If (a < 20) Then ' if condition is true then print the following ' Console.WriteLine("a is less than 20") End If Console.WriteLine("value of a is : {0}", a) Console.ReadLine() End Sub End Module When the above code is compiled and executed, it produces the following result:
a is less than 20; value of a is : 10
No comments:
Post a Comment