KEMBAR78
Fundamentals of .Net Programming concepts | PPTX
Fundamentals
of
VB.Net
Dr.A.Bharathi Lakshmi,
Head and Assistant Professor of IT,
V.V.Vanniaperumal College for Women,
Virudhunagar.
Variables & Constants
 Variables
• Container that stores one or more values
• Name of the variable is unchanged.
• The value might change during execution.
 Constants
• Stores a constant value
• Name as well as the value cannot be changed during
execution.
Data Types
Data Types Description
Integer 32-bit number – numeric data
Long 64-bit number – numeric data
Short 16-bit number – numeric data
Byte ASCII characters value in a numeric format – binary data
Double Huge floating point numbers
Single Single precision floating point numbers
Decimal Very large floating point numbers
Boolean 16-bit number – Boolean values
Char Single character
DateTime Date and time information
String Alphanumeric data
Object Data of any type
Rules for Naming variables
 It must begin with a letter
 It cannot contain a period or an
identifier-type character
 It must not exceed 255 characters
 It must be unique within the scope.
Declaring Variables
 Dim statement is used to declare a
variable
Syntax
Dim VariableName [As type]
VariableName – name of the variable
type – data type
Declaration
Dim ivar
Dim svar=“Hello”
Dim ivar1,ivar2 as integer
Initialization
Dim ivar as Integer
ivar=25
New Keyword
 New keyword initialize the variables before using
them in the code
Examples
Dim Inta as Object
Inta= new Integer
dim Ints as new Integer()
Nothing Keyword
 Represents default value of any data type
 Re-initialize to its default value.
Examples
Dim Inta as Integer=20
Inta= inta+4
Inta=Nothing
Explicit declaration
 Can be declared implicitly as well as
explicitly
 Without declaring a variable ,if it is
used it is implicitly declared
Example
InCo=20*5
Option Explicit [on|off]
 On – explicit declaration
 Off – implicit declaration
 Default - on
Scope of a variables
 Defines it visiblity
 The part of the program that can
access the variable
 Based on scope, variables can be
divided into two types
• Procedure-level variables
• Module-level variables
Procedure-Level Variables
 Only accessed within the procedure
 Reffered as local variables
Sub MyPro()
Dim str as String
str=“Hello “
MsgBox(str)
End Sub
In this code the variable str can be used only within the
procedure MyPro()
Module-Level Variables
 Declared in the declaration section of the
Module.
 Module level variables are classified into
• Private – can be used within the modules
Private str as String
Sub MyPro()
str=“Hello “
End Sub
Sub display()
MsgBox(str)
End Sub
• Public – can be used across modules
Public str as String
Sub MyPro()
str=“Hello “
End Sub
Sub display()
MsgBox(str)
Declaring Constants
 Const Keyword is used to declare a
constant
Syntax
Const ConstantName [As type]
ConstantName – name of the constant
type – data type
Example
Const cd as integer =25
Dim ivar as Integer
if ivar =1000 then
MessageBox.show(“Discount offered is “ & cd &
“%”)
End if
Operators
 Perform actions on data.
 Actions can be classified as
• Mathematical Calculations
• Assigning values to variables
• Comparing data
• Binary calculations
Example
a=15+30
(+) sign is the mathematical operator
the added value will be stored in a using
assignment operator
Types of Operators
 Arithmetic
 Comparison
 Logical/bitwise
Arithmetic Operators
 Performs Mathematical calculations.
 The arithmetic Operators are
• + operator – Add two numebers
• - operator – Subtract two numbers
• * operator – Multiply two numbers
• / operator – divide two numbers returns
floating
point number
•  operator – divide two numbers returns
Integer
point number
• ^ operator – power for a specified number
x^y
Comparison Operators
 Used to compare the expression
 The operators are
• Relational Operators
 Compares two numerical values
 <.>,<=,>=,<> and =
 Returns a Boolean value
• Is Operators
 Compares two object references
 Check if they are identical
 Returns a Boolean value
• Like Operators
 Compares two strings
 Case sensitive
 Returns a Boolean value
Logical/Bitwise Operators
 Perform logical Operations
 Logical operators are
• And operator
• Or operator
• Not operator
• Xor operator
• AndAlso operator
• OrElse operator
Logical/Bitwise Operators
 And operator
• Performs AND logical operations
• Returns true only when all expressions return true.
Example
Dim res as Boolean
Dim x as Integer = 40
Dim y as Integer = 30
Dim z as Integer = 20
res=x>y and y>z ‘Returns true
res=y>x and y>z ‘Returns false
Logical/Bitwise Operators
 Or operator
• Performs OR logical operations
• Returns true if any one of the expressions
return true.
Example
Dim res as Boolean
Dim x as Integer = 40
Dim y as Integer = 30
Dim z as Integer = 20
res=x>y Or y>z ‘Returns true
res=y>x Or y>z ‘Returns true
Logical/Bitwise Operators
 Not operator
• Performs NOT logical operations.
• Negates the value of expression and returns the opposite
value.
• If true result will be false and vise versa.
Example
Dim res as Boolean
Dim x as Integer = 40
Dim y as Integer = 30
Dim z as Integer = 20
res=Not(x>y) ‘Returns False
res=Not(y>x) ‘Returns True
Logical/Bitwise Operators
 Xor operator
• Performs XOR logical operations
• Performs logical exclusion on two
expressions.
• Returns true if any one of the expression
returns true value
• Returns false if all the expression returns
same value
Example
Dim res as Boolean
Dim x as Integer = 40
Dim y as Integer = 30
Dim z as Integer = 20
res=x>y Xor y>z ‘Returns false
res=y>x Xor y>z ‘Returns true
Logical/Bitwise Operators
 AndAlso operator
• Performs AND logical operations
• If first expression returns false then the second expression
wont be evaluated
Example
Dim res as Boolean
Dim x as Integer = 40
Dim y as Integer = 30
Dim z as Integer = 20
res=x>y AndAlso y>z ‘Returns true
res=y>x AndAlso y>z ‘Returns false
Logical/Bitwise Operators
 OrElse Operator
• Performs OR logical operations
• If first expression returns true then the second expression
wont be evaluated.
Example
Dim res as Boolean
Dim x as Integer = 40
Dim y as Integer = 30
Dim z as Integer = 20
res=x>y OrElse y>z ‘Returns true
res=y>x OrElse y>z ‘Returns true
Operator Precedence
 Order of evaluation of expressions
 Order of precedence
• Arithmetic Operators
 ^
 * & /
 
 + & -
• Comparison Operators
 =
 <>
 < & >
 >=
 <=
 Like
 Is
• Logical/Bitwise Operators
 Not
 And & AndAlso
 Or & OrElse
Operator Precedence
Example
2+3*4+5
a)20 b)45 c) 17 d) none
7-8+5
a) 6 b) -5 c) -4 d) 4
Expressions
 Combination of operators and operands.
 The value of the expressions are calculated at the
time of execution.
 Three types of expressions
• Numeric
• Value Comparison
• Boolean
Expressions
 Numeric Expressions
• Returns numeric values
• Is calculated using operator precedence rules
Examples
Dim cel as Integer
Dim far as Integer = 90
cal= (5/9)* (far-32)
Expressions
 Value comparison expressions
• Compare values
• Comparison operators are used
Examples
if x>250 then
MessageBox.Show(“>250”)
else
MessageBox.Show(“<=250”)
end if
Expressions
 Boolean Expressions
• Evaluates a Boolean value
Examples
if pass=true then
MessageBox.Show(“pass”)
else
MessageBox.Show(“fail”)
end if
Conversions
 Assigns the value of a variable of a data type to a
variable of another data type.
 If the value of the integer variable is assign to a long
variable it is broadening
 If the value of the long variable is assign to an integer
variable it is narrowing
 Types
• Implicit – broadening
• Explicit – narrowing (type.parse())
Controlling Program Flow
• Decision statements
 Execute stmts. based on the condition
 If…Then
 If…Then…Else
 Select…Case
• Looping Statement
 While…End While
 Do…Loop
 For…Next
 For…Each…Next
Decision Statements
 If…Then
• To execute a block of code based on a certain
condition
Syntax
If <<condition>> Then
‘Statements to be executed
End If
Decision Statements
 If…Then…Else
• Same as If…Then
• Two blocks of code can be executed for true case as
well as for false case
Syntax
If <<condition>> Then
‘Execute Statements if condition is true
Else
‘Execute Statements if condition is false
End If
Decision Statements
 Select…Case Statement
• Execute a group of statements based on the
test expression
• Can be a single variable or complex
expression
Syntax
Select <<testvalue>>
‘Execute a set of statements
Case <<value1>>
‘Execute a set of statements
Case <<value2>>
‘Execute a set of statements
Case <<value3>>
‘Execute a set of statements
Case Else
‘Execute a set of statements
End Select
Looping Statements
 While…End While
• Set of statements will be executed until the condition
becomes false.
Syntax
While <<condition>>
‘Executes statements
End While
Looping Statements
 Do…Loop
• Same as while loop
• Executes the block of code first without evaluating the
condition
• Then the condition is evaluated
Syntax
do
‘Executes statements
Loop While <<condition>>
Looping Statements
 For…Next Statement
• Same as while…end while
• Block of code will be executed for a fixed number of
times.
Syntax
For <<varnam>>=<<start>> to <<end>> Step <<value>>
‘Executes statements
Next
Looping Statements
 For…Each…Next Statement
• Similar to For...Next statement
• Enables to traverse through a collection
such as an array or a row of a table.
Syntax
For each <<varnam>> In <<array name or collection>>
‘Executes statements
Next
Examples
Dim I as integer
For Each I in Arrnam
Console.WriteLine(arrnam(i))
Next

Fundamentals of .Net Programming concepts

  • 1.
    Fundamentals of VB.Net Dr.A.Bharathi Lakshmi, Head andAssistant Professor of IT, V.V.Vanniaperumal College for Women, Virudhunagar.
  • 2.
    Variables & Constants Variables • Container that stores one or more values • Name of the variable is unchanged. • The value might change during execution.  Constants • Stores a constant value • Name as well as the value cannot be changed during execution.
  • 3.
    Data Types Data TypesDescription Integer 32-bit number – numeric data Long 64-bit number – numeric data Short 16-bit number – numeric data Byte ASCII characters value in a numeric format – binary data Double Huge floating point numbers Single Single precision floating point numbers Decimal Very large floating point numbers Boolean 16-bit number – Boolean values Char Single character DateTime Date and time information String Alphanumeric data Object Data of any type
  • 4.
    Rules for Namingvariables  It must begin with a letter  It cannot contain a period or an identifier-type character  It must not exceed 255 characters  It must be unique within the scope.
  • 5.
    Declaring Variables  Dimstatement is used to declare a variable Syntax Dim VariableName [As type] VariableName – name of the variable type – data type Declaration Dim ivar Dim svar=“Hello” Dim ivar1,ivar2 as integer Initialization Dim ivar as Integer ivar=25
  • 6.
    New Keyword  Newkeyword initialize the variables before using them in the code Examples Dim Inta as Object Inta= new Integer dim Ints as new Integer()
  • 7.
    Nothing Keyword  Representsdefault value of any data type  Re-initialize to its default value. Examples Dim Inta as Integer=20 Inta= inta+4 Inta=Nothing
  • 8.
    Explicit declaration  Canbe declared implicitly as well as explicitly  Without declaring a variable ,if it is used it is implicitly declared Example InCo=20*5 Option Explicit [on|off]  On – explicit declaration  Off – implicit declaration  Default - on
  • 9.
    Scope of avariables  Defines it visiblity  The part of the program that can access the variable  Based on scope, variables can be divided into two types • Procedure-level variables • Module-level variables
  • 10.
    Procedure-Level Variables  Onlyaccessed within the procedure  Reffered as local variables Sub MyPro() Dim str as String str=“Hello “ MsgBox(str) End Sub In this code the variable str can be used only within the procedure MyPro()
  • 11.
    Module-Level Variables  Declaredin the declaration section of the Module.  Module level variables are classified into • Private – can be used within the modules Private str as String Sub MyPro() str=“Hello “ End Sub Sub display() MsgBox(str) End Sub • Public – can be used across modules Public str as String Sub MyPro() str=“Hello “ End Sub Sub display() MsgBox(str)
  • 12.
    Declaring Constants  ConstKeyword is used to declare a constant Syntax Const ConstantName [As type] ConstantName – name of the constant type – data type Example Const cd as integer =25 Dim ivar as Integer if ivar =1000 then MessageBox.show(“Discount offered is “ & cd & “%”) End if
  • 13.
    Operators  Perform actionson data.  Actions can be classified as • Mathematical Calculations • Assigning values to variables • Comparing data • Binary calculations Example a=15+30 (+) sign is the mathematical operator the added value will be stored in a using assignment operator
  • 14.
    Types of Operators Arithmetic  Comparison  Logical/bitwise
  • 15.
    Arithmetic Operators  PerformsMathematical calculations.  The arithmetic Operators are • + operator – Add two numebers • - operator – Subtract two numbers • * operator – Multiply two numbers • / operator – divide two numbers returns floating point number • operator – divide two numbers returns Integer point number • ^ operator – power for a specified number x^y
  • 16.
    Comparison Operators  Usedto compare the expression  The operators are • Relational Operators  Compares two numerical values  <.>,<=,>=,<> and =  Returns a Boolean value • Is Operators  Compares two object references  Check if they are identical  Returns a Boolean value • Like Operators  Compares two strings  Case sensitive  Returns a Boolean value
  • 17.
    Logical/Bitwise Operators  Performlogical Operations  Logical operators are • And operator • Or operator • Not operator • Xor operator • AndAlso operator • OrElse operator
  • 18.
    Logical/Bitwise Operators  Andoperator • Performs AND logical operations • Returns true only when all expressions return true. Example Dim res as Boolean Dim x as Integer = 40 Dim y as Integer = 30 Dim z as Integer = 20 res=x>y and y>z ‘Returns true res=y>x and y>z ‘Returns false
  • 19.
    Logical/Bitwise Operators  Oroperator • Performs OR logical operations • Returns true if any one of the expressions return true. Example Dim res as Boolean Dim x as Integer = 40 Dim y as Integer = 30 Dim z as Integer = 20 res=x>y Or y>z ‘Returns true res=y>x Or y>z ‘Returns true
  • 20.
    Logical/Bitwise Operators  Notoperator • Performs NOT logical operations. • Negates the value of expression and returns the opposite value. • If true result will be false and vise versa. Example Dim res as Boolean Dim x as Integer = 40 Dim y as Integer = 30 Dim z as Integer = 20 res=Not(x>y) ‘Returns False res=Not(y>x) ‘Returns True
  • 21.
    Logical/Bitwise Operators  Xoroperator • Performs XOR logical operations • Performs logical exclusion on two expressions. • Returns true if any one of the expression returns true value • Returns false if all the expression returns same value Example Dim res as Boolean Dim x as Integer = 40 Dim y as Integer = 30 Dim z as Integer = 20 res=x>y Xor y>z ‘Returns false res=y>x Xor y>z ‘Returns true
  • 22.
    Logical/Bitwise Operators  AndAlsooperator • Performs AND logical operations • If first expression returns false then the second expression wont be evaluated Example Dim res as Boolean Dim x as Integer = 40 Dim y as Integer = 30 Dim z as Integer = 20 res=x>y AndAlso y>z ‘Returns true res=y>x AndAlso y>z ‘Returns false
  • 23.
    Logical/Bitwise Operators  OrElseOperator • Performs OR logical operations • If first expression returns true then the second expression wont be evaluated. Example Dim res as Boolean Dim x as Integer = 40 Dim y as Integer = 30 Dim z as Integer = 20 res=x>y OrElse y>z ‘Returns true res=y>x OrElse y>z ‘Returns true
  • 24.
    Operator Precedence  Orderof evaluation of expressions  Order of precedence • Arithmetic Operators  ^  * & /   + & - • Comparison Operators  =  <>  < & >  >=  <=  Like  Is • Logical/Bitwise Operators  Not  And & AndAlso  Or & OrElse
  • 25.
    Operator Precedence Example 2+3*4+5 a)20 b)45c) 17 d) none 7-8+5 a) 6 b) -5 c) -4 d) 4
  • 26.
    Expressions  Combination ofoperators and operands.  The value of the expressions are calculated at the time of execution.  Three types of expressions • Numeric • Value Comparison • Boolean
  • 27.
    Expressions  Numeric Expressions •Returns numeric values • Is calculated using operator precedence rules Examples Dim cel as Integer Dim far as Integer = 90 cal= (5/9)* (far-32)
  • 28.
    Expressions  Value comparisonexpressions • Compare values • Comparison operators are used Examples if x>250 then MessageBox.Show(“>250”) else MessageBox.Show(“<=250”) end if
  • 29.
    Expressions  Boolean Expressions •Evaluates a Boolean value Examples if pass=true then MessageBox.Show(“pass”) else MessageBox.Show(“fail”) end if
  • 30.
    Conversions  Assigns thevalue of a variable of a data type to a variable of another data type.  If the value of the integer variable is assign to a long variable it is broadening  If the value of the long variable is assign to an integer variable it is narrowing  Types • Implicit – broadening • Explicit – narrowing (type.parse())
  • 31.
    Controlling Program Flow •Decision statements  Execute stmts. based on the condition  If…Then  If…Then…Else  Select…Case • Looping Statement  While…End While  Do…Loop  For…Next  For…Each…Next
  • 32.
    Decision Statements  If…Then •To execute a block of code based on a certain condition Syntax If <<condition>> Then ‘Statements to be executed End If
  • 33.
    Decision Statements  If…Then…Else •Same as If…Then • Two blocks of code can be executed for true case as well as for false case Syntax If <<condition>> Then ‘Execute Statements if condition is true Else ‘Execute Statements if condition is false End If
  • 34.
    Decision Statements  Select…CaseStatement • Execute a group of statements based on the test expression • Can be a single variable or complex expression Syntax Select <<testvalue>> ‘Execute a set of statements Case <<value1>> ‘Execute a set of statements Case <<value2>> ‘Execute a set of statements Case <<value3>> ‘Execute a set of statements Case Else ‘Execute a set of statements End Select
  • 35.
    Looping Statements  While…EndWhile • Set of statements will be executed until the condition becomes false. Syntax While <<condition>> ‘Executes statements End While
  • 36.
    Looping Statements  Do…Loop •Same as while loop • Executes the block of code first without evaluating the condition • Then the condition is evaluated Syntax do ‘Executes statements Loop While <<condition>>
  • 37.
    Looping Statements  For…NextStatement • Same as while…end while • Block of code will be executed for a fixed number of times. Syntax For <<varnam>>=<<start>> to <<end>> Step <<value>> ‘Executes statements Next
  • 38.
    Looping Statements  For…Each…NextStatement • Similar to For...Next statement • Enables to traverse through a collection such as an array or a row of a table. Syntax For each <<varnam>> In <<array name or collection>> ‘Executes statements Next Examples Dim I as integer For Each I in Arrnam Console.WriteLine(arrnam(i)) Next