Professional Documents
Culture Documents
COSC226 Module3
COSC226 Module3
COSC226 Module3
NET
COSC 226
TOPIC: VB.Net Basic Syntax
Outline:
type of data or value is Decimal 16 bytes Range from 0 to +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9…E+28) without
any decimal point; And 0 to +/-7.92281625142264337593543950335 with 28
assigning to a variable or position to the right of the decimal
function. Double 8 bytes -1.79769313486231570E+308 to -4.94-65645841246544E-324 for negative values;
4.94065645841246544E-324 to 1.79769313486231570E+308, for positive values
• The type of a variable
Integer 4 bytes -2,147,483,648 to 2,147,483,647 (signed)
determines how much space
Long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (9.2…E + 18) (signed)
it occupies in storage and
how the bit pattern stored is Object 4 bytes in 32-bit and 8 bytes in It can store any type of data defined in a variable of type Object
64-bit platform
interpreted.
SByte 1 byte -128 to 127 (signed)
• VB.Net provides a wide range Short 2 bytes -32,768 to 32,767 (signed)
of data types as shown in the Single 4 bytes -3.4028235E + 38 to -1.401298E-45 for negative values;
And for positive value: 1.401298E-45 to 3.4028235E + 38.
table below:
String String Datatype depend on the It accepts Unicode character from 0 to approximately 2 billion characters.
implementing platform
UInteger 4 bytes The range start from 0 to 4,294,967,295 (unsigned)
ULong 8 bytes The range of ULong start from 0 to 18,446,744,073,709,551,615 (1.8…E + 19)
(unsigned)
User-Defined A user-defined data type Each member of the structure has its own data type and limits independent of the
(structure) depends on the implementing other members' ranges.
platform
UShort 2 bytes Range from 0 to 65,535 (unsigned)
The data manipulated by a VB.NET program fall into two categories.
Numeric and String.
Numeric Data must contain only numbers e.g 9,35, 85783,109,95
whereas String Data can contain any symbol. e.g Jane Arbor, 321 Lekki
Road.
Numeric values can be used in arithmetic calculations but string values
cannot be used.
Note: VB uses the term string to represent a string of characters.
Example of datatypes are Boolean (T/F), Byte(1 byte), Currency(8
bytes), Date (8 bytes), Double(8 bytes), Integer(2 bytes), Long(4 bytes),
String(16 bytes), Char, Decimal (12 bytes), Single(4 bytes),object (4
bytes), variant (16 bytes: any value as large as double).
• Variables are the memory locations that hold data
that can be changed during project execution.
• Variables are used in a program to store data items
and to retrieve the data during processing. It may
contain letters, digits or the underscore character. The
first character must be a letter.
• FleetSize, WageRate, AverageAge, MaximumCapacity,
ExtendedPrice, EmployeeNum, EmployeeName,
NumberOfSeminarParticipants.
Rules for naming variables
• All variables must be declared.
• Variable names must begin with an alphabet.
• Variable names should be meaningful and descriptive.
• It must consist of letters, digits and underscores.
• Do not use keywords as a variable name.
• They cannot contain any spaces.
• Variable is an identifier that is declared in a method and stands for a
value within that method.it is used to represent some specified type
of information within a designated portion of the program.
VB.NET Variable and Constant:
• A variable is a simple name used to store the value of a specific data
type in computer memory
• It is a memory location whose contents may changed during program
execution.
• 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
VB.NET Variable and Constant:
Variable Names
• The following are the rules when • Example of Valid names:
naming the variables in Visual Basic
• intFeb_Income
• It must be less than 255 characters
• decSales2014
• No spacing is allowed
It must not begin with a number • dblEastRegion
•
Period is not permitted • StrName
•
• Not case sensitive • Example of invalid names:
• It should not be a Reserved keyword( e.g. • 4thQuarter- The name must begin
Sub, End, for, Case, While, etc.) with a letter or underscore
• No special character is allow (e.g. ? - +! @ • dblWest Region -The name cannot
# % ^ & * ( ) [ ] { } . ; : " ' / \) contain a space
• Begin each variable and constant name • StrFirst.Name - The name cannot
with a letter or underscore. This is a contain punctuation
Visual Basic requirement. Variable and • decSales$East - The name cannot
constant names can contain only letters, contain a special character
underscores, and numbers.
Variable Declaration
• In Visual Basic, one needs to declare the variables before using them
by assigning names and data types.
• The declaration of a variable is simple that requires a variable name
and data type followed by a Dim.
• A Dim is used in Class, Module, structure, Sub, procedure.
Syntax:
Dim [Variable_Name] As [Data Type]
Variable Declaration cont.
• Examples:
• Dim password As String
• Dim yourName As String
• Dim firstnum As Integer
• Dim secondnum As Integer
• Dim total As Integer
• Dim doDate As Date
Variable Declaration cont.
• You may also combine them in one line , separating each variable
with a comma, as follows:
• You can omit the Dim keyword if you specify Public, Protected,
Friend, Protected Friend, Private, Static, or Read Only.
• If you do not specify otherwise, variables you declare using a Dim
statement are Private. The following two statements are equivalent:
• Constants are the memory location that hold data that cannot be
change during execution.
• The format for defining a constant named PI with a value 3.14159
• Const PI= 3.14159
• Constant is a data item whose value is specified at design time
and cannot change at the run time.
• There are 2 literal constant
• Numeric Literal constant e.g 123, 1.23, +123, .1, -6, 8.9E+3, 8.9E-
6
• String Literal constant e.g “John Doe”
• Note: A numeric literal constant can be used in arithmetic
calculations but can contain only certain symbols (digits, decimal
point) while a string literal constant cannot be used in arithmetic.
Keywords
Boolean
AddressOf
ByRef
Alias
Byte
And
ByVal
AndAlso
Call
As
Case
CType
Continue
CUnit
CSByte
CULng
CShort
CUShort
CSng
Date
CStr
Decimal
compiler, whose meaning Declare Default Delegate Dim DirectCast Do
Me
Let
Mod
Lib
Module
Like
MustInherit
Long
MustOverride
Loop
MyBase
name, variable, function, MyClass Namespace Narrowing New Next Not
The following table lists the Operator Option Optional Or OrElse Overloads
Arithmetic operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Floating Division
\ Integer (whole number) division
Mod Remainder division
^ Exponentiation (raising to a power)
& String concatenation (combination)
Operators:
Relational operators
• Combine value(s) to returns Boolean value(true or false)
operator meaning
> Greater than
< Less than
= Equal to
<> Not equal to
>= Greater than or equal to
<= Less than or equal to
• All relational operator are binary
Operators:
Logical operators
• combine multiple Boolean expressions into a compound Boolean expression
operator description
And combine two expression into one. Returns true if both expression are true
Or combine two expression into one. if both expression evaluate to false, Or returns
false, otherwise it returns true
Not negates an expression
Xor logically joins two expressions. If both expression evaluate to true or both
expression evaluate to false, Xor returns false; otherwise it returns true
IsFalse is used to determine whether an expression is False.
IsTrue is used to determine whether an expression is True.
Operators:
Assignment operators
• The Assignment Operators are used to assign the value to variables in VB.NET.
& It is an ampersand symbol that is used to bind two or more operand together.
Assignment: Write short note on VB.Net Bit Shift Operators and Miscellaneous
Operators.
Comments:
• A comment is used to explain the various steps that we have taken in
our programming.
• The compiler ignores these comment statements because the compiler
is not executed or processed in VB.NET.
1. Write a VB.NET console application that allow users to enter the Length and Breadth
of a rectangle, and then computes and display the Area on the screen
2. A Power company wants a program that inputs the voltage and three resistances and
then calculates and displays the current. For three resistors connected in series,
the total resistance is the sum of the individual resistances and by Ohm’s law, the
current in the circuit is given by
amps = voltage / total resistance.
Write a VB console application to computes the current(amps)
• Constants are the memory location that hold data that cannot be
change during execution.
• The format for defining a constant named PI with a value 3.14159
• Const PI= 3.14159
• Constant is a data item whose value is specified at design time
and cannot change at the run time.
• There are 2 literal constant
• Numeric Literal constant e.g 123, 1.23, +123, .1, -6, 8.9E+3, 8.9E-
6
• String Literal constant e.g “John Doe”
• Note: A numeric literal constant can be used in arithmetic
calculations but can contain only certain symbols (digits, decimal
point) while a string literal constant cannot be used in arithmetic.
Keywords
•