Variables, Constant and Calculation
Variables, Constant and Calculation
Variables, Constant and Calculation
Variables
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in
VB.Net has a specific type, which determines the size and layout of the variable's memory; the range of values
that can be stored within that memory; and the set of operations that can be applied to the variable.
A variable is a simple name used to store the value of a specific data type in computer memory.
Understand Scope of Variables
But to have a clear understanding of a global variable you need to understand the scope of the variables.
There are three ways of defining scopes to variables:
1. Procedure-Level: You can only use a variable in the same procedure where you declare it.
2. Module-Level (Private): Makes a variable accessible from all the procedures in a module.
3. Global Level (Public): Makes a variable accessible from all the procedures in all the modules.
Each variable has a particular data type that determines the size, range, and fixed space in computer memory.
We have already discussed various data types. The basic value types provided in VB.Net can be categorized as
–
Type Example
Integral types SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and Char
Floating point types Single and Double
Decimal types Decimal
Boolean types True or False values, as assigned
Date types Date
Some valid variable declarations along with their definition are shown here –
Local Variable
A local variable is one that is declared within a procedure. A member variable is a member of a Visual Basic
type; it is declared at module level, inside a class, structure, or module, but not within any procedure internal
to that class, structure, or module.
Local variables are created when the method, constructor or block is entered and the variable will be
destroyed once it exits the method, constructor, or block.
Global Variable
What Does Global Mean?
If you wanted to use a particular variable over and over again throughout your code, then you declare the
variable as a PUBLIC variable and you would need to declare it in a module. In addition, you would need to use
the Public statement rather than the Dim statement.
There is no way to declare global variables as you're probably imagining them in VB.NET. However, you'll
need to fully-qualify all references to those variables anywhere you want to use them in your code.
Explain Local and Global Declaration
Local
Global
Constants
Constants are fixed values that are not changed during the execution of the program. These fixed
values are also referred to as literals.
Constants can be of any basic data kinds, such as integers, characters, floating points, or string literals.
Enumeration constants are also available.
Constants are processed similarly to regular variables, except that their values cannot be changed after
they are defined.
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.
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 constantlist
Where,
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.
Where, each constant name has the following syntax and parts −
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
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
To declare a constant
Write a declaration that includes an access specifier, the Const keyword, and an expression, as in the following
examples:
Public Const DaysInYear = 365
Private Const WorkDays = 250
To declare multiple constants on a single line
Public Const Four As Integer = 4, Five As Integer = 5, Six As Integer = 44
Coding for Constant Variables
Another Program in Contents (Compute for the Value of Circle)
Calculations
What is arithmetic operators in Visual Basic?
Arithmetic operators are used to perform many of the familiar arithmetic operations that involve the
calculation of numeric values represented by literals, variables, other expressions, function and property calls,
and constants.
Operator Description Example
^ Raises one operand to the power of another B^A will give 49
+ Adds two operands A + B will give 9
- Subtracts second operand from the first A - B will give -5
* Multiplies both operands A * B will give 14
/ Divides one operand by another and returns a floating point B / A will give 3.5
result
\ Divides one operand by another and returns an integer result B \ A will give 3
MOD Modulus Operator and remainder of after an integer division B MOD A will give 1
'Use of - Operator
c=a-b
Console.WriteLine(" Subtraction of a - b is {0}", c)
'Use of * Operator
c=a*b
Console.WriteLine(" Multiplication of a * b is {0}", c)
'Use of / Operator
d=a/b
Console.WriteLine(" Division of a / b is {0}", d)
'Use of \ Operator
c=a\b
Console.WriteLine(" Similar to division Operator (return only integer value) of a - b is {0}", c)
'Use of ^ Operator
c=a^b
Console.WriteLine(" Power of a ^ b is {0}", c)
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Sub
End Module