Unit 1
Unit 1
String Variables
The String data type stores only text, and string variables are declared as follows:
Dim someText As String
Character Variables
Character variables store a single Unicode character in two bytes. To declare a
Character variable, use the Char data type:
Dim char1, char2 As Char
Date Variables
Date variables store date values that may include a time part (or not), and they are
declared with the Date data type:
Dim expiration As Date
DATA TYPE CONVERSION
ToBoolean Boolean
ToByte Byte
ToChar Unicode character
ToDate TimeDate
ToDecimalDecimal
ToDouble Double
ToInt16 Short Integer (2-byte integer, Int16)
ToInt32 Integer (4-byte integer, Int32)
ToInt64 Long (8-byte integer, Int64)
ToSByte Signed Byte
CShort Short (2-byte integer, Int16)
ToSingle Single
ToString String
ToUInt16 Unsigned Integer (2-byte integer, Int16)
ToUInt32 Unsigned Integer (4-byte integer, Int32)
ToUInt64 Unsigned Long (8-byte integer, Int64)
Type Characters
Type Character Description Example
C Converts value to a Char type Dim ch As String = "A"c
D or @ Converts value to a Dim price As Decimal = 12.99D
Decimal type
R or # Converts value to a Double type Dim pi As Double = 3.14R
I or % Converts value to an Integer type Dim count As Integer = 99I
L or & Converts value to a Long type Dim distance As Long = 1999L
S Converts value to a Short type Dim age As Short = 1S
F or ! Converts value to a Single type Dim velocity As Single = 74.99F
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:
Const constantname [ As datatype ] = initializer
Const : keyword for const
constantname: specifies the name of the constant
datatype: specifies the data type of the constant
initializer: specifies the value assigned to the constant
For example,
'The following statements declare constants.'
Const maxval As Long = 4999
Public Const message As String = "HELLO"
Private Const piValue As Double = 3.1415
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.
vbNullStrin Not the same as a zero-length string (""); used for calling
g external procedures.
vbObjectEr Error number. User-defined error numbers should be greater
ror than this value.
vbTab Tab character.
vbBack Backspace character.
ENUMERATIONS
Enumerations provide a convenient way to work with sets of
related constants and to associate constant values with names.
For example, you can declare an enumeration for a set of integer
constants associated with the days of the week, and then use the
names of the days rather than their integer values in your code.
An enumeration has a name, an underlying data type, and a set
of members. Each member represents a constant.
And Bitwise AND Operator copies a bit to the (A AND B) will give 12, which is 0000
result if it exists in both operands. 1100
Or Binary OR Operator copies a bit if it exists (A Or B) will give 61, which is 0011
in either operand. 1101
Xor Binary XOR Operator copies the bit if it is (A Xor B) will give 49, which is 0011
set in one operand but not both. 0001
Not Binary Ones Complement Operator is (Not A ) will give -61, which is 1100
unary and has the effect of 'flipping' bits. 0011 in 2's complement form
<< Binary Left Shift Operator. The left A << 2 will give 240, which is 1111
operands value is moved left by the 0000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left A >> 2 will give 15, which is 0000
operands value is moved right by the 1111
number of bits specified by the right
operand.
Assignment Operators
Operator Description Example