0% found this document useful (0 votes)
48 views54 pages

Unit 1

The document provides an overview of the .NET framework. It discusses key concepts like the Common Language Runtime (CLR), Base Class Library, Common Type System, Common Language Specification, .NET design principles, Microsoft Intermediate Language (MSIL), and object-oriented programming in .NET. It also covers Visual Basic 2010 and introduces concepts such as variables, data types, and event-driven programming.

Uploaded by

dawngliani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views54 pages

Unit 1

The document provides an overview of the .NET framework. It discusses key concepts like the Common Language Runtime (CLR), Base Class Library, Common Type System, Common Language Specification, .NET design principles, Microsoft Intermediate Language (MSIL), and object-oriented programming in .NET. It also covers Visual Basic 2010 and introduces concepts such as variables, data types, and event-driven programming.

Uploaded by

dawngliani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

UNIT 1

OVERVIEW OF .NET FRAMEWORK


.NET Framework is a software framework developed
by Microsoft that runs primarily on Microsoft
Windows.
It includes a large class library named Base Class
Library and provide language interoperability across
several programming languages.
The first version of the .Net framework was released
in the year 2002.The version was called .Net
framework 1.0. The .Net framework has come a long
way since then, and the current version is 4.7.1.
Unlike the Win32 API, .NET is totally object-
oriented.
Common Language Runtime
 The CLR has the following key features:
 Exception Handling - Exceptions are errors which occur when
the application is executed.
 Garbage Collection - Garbage collection is the process of
removing unwanted resources when they are no longer required.
Examples of garbage collection are
 Working with Various programming languages –
◦ Language - The first level is the programming language itself, the most
common ones are VB.Net and C#.
◦ Compiler – There is a compiler which will be separate for each
programming language. So underlying the VB.Net language, there will
be a separate VB.Net compiler. Similarly, for C#, you will have another
compiler.
◦ Common Language Interpreter – This is the final layer in .Net which
would be used to run a .net program developed in any programming
language. So the subsequent compiler will send the program to the CLI
layer to run the .Net application.
Base Class Library
The .NET Framework includes a set of standard
class libraries.
A class library is a collection of methods and
functions that can be used for the core purpose.
Most of the methods are split into either the
System.* or Microsoft.* namespaces. (The asterisk
* just means a reference to all of the methods that
fall under the System or Microsoft namespace).
A namespace is a logical separation of methods.
We will learn these namespaces more in detail in
the subsequent chapters.
The Common Types System and
Common Language Specification
 The Common Type System (CTS) enables software
written in different languages to work together.
 With the CTS in place, all .NET languages use
strings, integers, and so on in the same way, so no
conversion needs to take place.
 The Common Language Specification (CLS) was
introduced by Microsoft to make it easier for
language developers to adapt their languages to be
compatible with .NET. This means that C, C++, C#
and Java can be easily made to be compatible
with .NET environment.
.Net Framework Design Principle
Interoperability
Portability
Security
Memory management
Simplified deployment
MSIL
 Microsoft Intermediate Language (MSIL) is a language
used as the output of a number of compilers (C#,
VB, .NET, and so forth). It is a set of independent
instructions that are generated by language compiler when
the project is compiled.
 MSIL code is not executable but can be processed by CLR
before it becomes executable. The advantages of MSIL are
as follows:
◦ 1. MSIL provides language interoperability, as code in any .NET
language is compiled on MSIL.
◦ 2. Same performance in all .NET languages.
◦ 3. It support different runtime environments.
◦ 4. JIT compiler in CLR converts MSIL code into native machine
code which is executed by OS.
INTRODUCTION TO .NET

Visual Basic 2010 is a language you can use to


tell your computer how to do things; but, like a
child, the computer will understand only if you
explain things very clearly.
Visual Basic 2010 offers an easy-to-use language
to explain some complex tasks.
Visual Basic 2010 frees the programmer from
having to deal with the mundane complexities of
writing Windows applications.
You are free to concentrate on solving real
problems
EVENT-DRIVEN PROGRAMMING
Event-driven programming is a
programming paradigm in which the flow of
program execution is determined by events -
for example a user action such as a mouse
click, key press, or a message from the
operating system or another program.
An event-driven application is designed to
detect events as they occur, and then deal
with them using an appropriate event-
handling procedure.
THE VISUAL STUDIO 2010 IDE
The Menu Editor
The Toolbars
THE TOOLBOX
SOLUTION EXPLORER
PROPERTIES WINDOW
THE DESIGN VIEW/ CODE EDITOR
USING THE HELP SYSTEM
INTRODUCTION TO VB.NET
PROGRAMMMING
In Object-Oriented Programming methodology, a program
consists of various objects that interact with each other by
means of actions.
1. Object − Objects have states and behaviors. Example: A dog
has states - color, name, breed as well as behaviors - wagging,
barking, eating, etc.
2. Class − A class can be defined as a template/blueprint that
describes the behaviors/states that objects of its type support.
3. Methods − A method is basically a behavior. A class can
contain many methods. It is in methods where the logics are
written, data is manipulated and all the actions are executed.
4. Instance Variables − Each object has its unique set of
instance variables. An object's state is created by the values
assigned to these instance variables.
VARIABLES

In visual basic, as in any other


programming language, variables store
values during a program’s language
execution.
A variable has a name and a value.
The variable’s username, for example,
might have the value Joe, and the variable
Discount might have the value
DECLARING VARIABLES
 When programming in VB 2010, you should declare
your variables because this is the default mode. To
declare variables use the Dim statement and the As
keyword,
Dim meters As integer
Dim greetings As string

 The first variable, meters, will store integers, such as 3


or 1,002; the second variable, greetings will store text.
You can declare multiple variables of the same or
different type in the same line, as follows:
Dim Qty As Integer , Amount As Decimal, Volume As String
Dim Length,Width, Height As Integer, Volume, Area As Double
VARIABLE INITIALIZATION
Visual basic allows you to initialize variables
in the same lines that declare them. The
following statement declares an integer
variable and immediately places in the value
3,045 in it:
Dim distance As integer = 3045
This statement is equivalent to the following
two:
Dim Distance As integer
Distance = 3045
IDENTIFIERS
 Asthe name defines, an identifier is used to identify the
name of variable, function, class, or any other user-defined
elements in the program. An identifier should be the
combination of letter, digit, and underscore. There are
various rules for identifier in VB.NET, as follows:
◦ The first character of an identifier must start with an alphabet or
underscore, that could be followed by any sequence of digits (0-
9), letter or underscore.
◦ An identifier should not contain any reserved keyword.
◦ It should not start with any digit.
◦ It should not more than 51 characters.
◦ An identifier can contain two underscores, but should not be
consecutive.
◦ It should not include any commas or white spaces in-between
characters.
KEYWORDS
A keyword is a reserved word with special meanings in
the compiler, whose meaning cannot be changed.
Therefore, these keywords cannot be used as an
identifier in VB.NET programming such as class name,
variable, function, module, etc.
Some of the reserved keyword available in the VB.NET
language are:

And Boolean ByVal Char


Date Dim Else End
Integer If Loop New
Public Then True End if
Enum Next Private
DATA TYPE (Types of Variables)
Variables have a data type, which
determines what kind of values you can
store to a variable. Visual basic organizes
the following categories of variables:
◦ Numeric
◦ String
◦ Boolean
◦ Date
◦ Object
Numeric Variables
Data Type Size Stores
Byte (Byte) 1 byte Integers in the range 0 to 255.
Signed Byte(SByte) 1 byte Integers in the range −128 to 127.
Short (Int16) 2 bytes Integer values in the range −32,768 to 32,767.
Integer (Int32) 4 bytes Integer values in the range −2,147,483,648 to 2,147,483,647.
Long (Int64) 8 bytes Integer values in the range −9,223,372,036,854,755,808
to9,223,372,036,854,755,807.
Unsigned Short 2 bytes Positive integer values in the range 0 to 65,535.
Unsigned Integer 4 bytes Positive integers in the range 0 to 4,294,967,295.
Unsigned Long 8 bytes Positive integers in the range 0 to
(ULong) 18,446,744,073,709,551,615.
Single Precision 4 bytes Single-precision floating-point numbers.
(Single)
precision variable
Double Precision 8 bytes Double-precision floating-point numbers.
(Double)
 Boolean Variables
The Boolean data type stores True/False values. Boolean variables are, in essence,
integers that take the value –1 (for True) and 0 (for False). Actually, any nonzero
value is considered True. Boolean variables are declared as
Dim failure As Boolean

 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.

Enum Statement declares an enumeration and defines the


values of its members.
Enum enumerationname [ As datatype ] memberlist
End Enum
Enum background
None = 0
green = 1
yellow = 2
red = 3
blue = 4
End Enum
Sub Main()
Dim value As background = background.green
If value=0 then
Console.WriteLine("None")
Elseif value=1 then
Console.WriteLine("green")
Elseif value=2 then
Console.WriteLine("yellow")
Elseif value=3 then
Console.WriteLine("red")
Elseif value=4 then
Console.WriteLine("blue")
End if
The benefits of using enumerations include the
following:
Reduces errors caused by transposing or mistyping
numbers.
Makes it easy to change values in the future.
Makes code easier to read, which means it is less
likely that errors will be introduced.
Ensures forward compatibility. If you use
enumerations, your code is less likely to fail if in
the future someone changes the values
corresponding to the member names.
OPERATORS IN VB.NET
An operator is a symbol that tells the compiler
to perform specific mathematical or logical
manipulations. VB.Net is rich in built-in
operators and provides following types of
commonly used operators:
◦ Arithmetic Operators
◦ Comparison Operators
◦ Logical/Bitwise Operators
◦ Bit Shift Operators
◦ Assignment Operators
◦ Miscellaneous Operators
Arithmetic Operators
Operator Description Example
^ Raises one operand to the power of B^A will give 49
another
+ Adds two operands A + B will give 9
- Subtracts second operand from the A - B will give -5
first
* Multiplies both operands A * B will give
14
/ Divides one operand by another and B / A will give
returns a floating point result 3.5
\ Divides one operand by another and B \ A will give 3
returns an integer result
MOD Modulus Operator and remainder of B MOD A will
after an integer division give 1
Comparison Operators
Operator Description Example
= Checks if the values of two operands are (A = B) is not true.
equal or not; if yes, then condition becomes
true.
<> Checks if the values of two operands are (A <> B) is true.
equal or not; if values are not equal, then
condition becomes true.
> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand; if yes, then
condition becomes true.
< Checks if the value of left operand is less (A < B) is true.
than the value of right operand; if yes, then
condition becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand;
if yes, then condition becomes true.
<= Checks if the value of left operand is less (A <= B) is true.
than or equal to the value of right operand;
if yes, then condition becomes true.
Logical operators
Operator Description Example

And It is the logical as well as bitwise AND operator. If (A And B) is False.


both the operands are true, then condition
becomes true.
Or It is the logical as well as bitwise OR operator. If (A Or B) is True.
any of the two operands is true, then condition
becomes true.
Not It is the logical as well as bitwise NOT operator. Not(A And B) is True.
Use to reverses the logical state of its operand. If a
condition is true, then Logical NOT operator will
make false.
Xor It is the logical as well as bitwise Logical Exclusive A Xor B is True.
OR operator. It returns True if both expressions are
True or both expressions are False; otherwise it
returns False.
AndAlso It is the logical AND operator. It works only on (A AndAlso B) is False.
Boolean data. It performs short-circuiting.
OrElse It is the logical OR operator. It works only on (A OrElse B) is True.
Boolean data. It performs short-circuiting.
IsFalse It determines whether an expression is False.

IsTrue It determines whether an expression is True.


Bitwise Operators
Operator Description Example

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

= Simple assignment operator,. C = A + B will assign value of A


+ B into C
+= Add AND assignment operator, C += A is equivalent to C = C +
A
-= Subtract AND assignment operator, C -= A is equivalent to C = C - A

*= Multiply AND assignment operator. C *= A is equivalent to C = C * A

/= Divide AND assignment operator. C /= A is equivalent to C = C /


A
\= Divide AND assignment operator. C \= A is equivalent to C = C \A

^= Exponentiation and assignment operator. C^=A is equivalent to C = C ^ A

<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2

&= Concatenates a String expression to a String Str1 &= Str2 is same as


variable or property and assigns the result to the Str1 = Str1 & Str2
variable or property.
Miscellaneous Operators
Operator Description Example

AddressOf Returns the address of a procedure. AddHandler Button1.Click,


AddressOf Button1_Click
Await It is applied to an operand in an Dim result As res
asynchronous method or lambda =Await
expression to suspend execution of the AsyncMethodThatReturnsRes
method until the awaited task completes. ult()
Await AsyncMethod()
GetType It returns a Type object for the specified MsgBox(GetType(Integer).ToSt
type. The Type object provides information ring())
about the type such as its properties,
methods, and events.
Function It declares the parameters and code that Dim add5 = Function(num As
Expression define a function lambda expression. Integer) num + 5
'prints 10
Console.WriteLine(add5(5))
If It uses short-circuit evaluation to Dim num = 5
conditionally return one of two values. The Console.WriteLine(If(num >=
If operator can be called with three 0,
arguments or with two arguments. "Positive", "Negative"))
Operators Precedence in VB.Net
Operator Preceden
ce
Await Highest
Exponentiation (^)
Unary identity and negation (+, -)
Multiplication and floating-point division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
Arithmetic bit shift (<<, >>)
All comparison operators (=, <>, <, <=, >, >=, Is, IsNot,
Like, TypeOf...Is)
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor) Lowest
STATEMENT IN VB.NET

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.
S.N
Declaration Statements
Statements and Description Example

1 Dim Statement Dim number As Integer


Declares and allocates storage space for one or Dim quantity As Integer = 100
more variables. Dim message As String = "Hello!"
2 Const Statement Const maximum As Long = 1000
Declares and defines one or more constants. Const naturalLogBase As Object
= CDec(2.7182818284)
3 Enum Statement Enum CoffeeMugSize
Declares an enumeration and defines the values of Jumbo
its members. ExtraLarge
Large
Medium
Small
End Enum
4 Class Statement Class Box
Declares the name of a class and introduces the Public length As Double
definition of the variables, properties, events, and Public breadth As Double
procedures that the class comprises. Public height As Double
End Class
5 Structure Statement Structure Box
Declares the name of a structure and introduces Public length As Double
the definition of the variables, properties, events, Public breadth As Double
and procedures that the structure comprises. Public height As Double
End Structure
6 Module Statement Public Module myModule
Declares the name of a module and Sub Main()
introduces the definition of the Dim user As String =
variables, properties, events, and InputBox("What is your name?")
procedures that the module MsgBox("User name is" & user)
comprises. End Sub
End Module
7 Interface Statement Public Interface MyInterface
Sub doSomething()
Declares the name of an interface and End Interface
introduces the definitions of the
members that the interface comprises.
8 Function Statement Function myFunction
Declares the name, parameters, and (ByVal n As Integer) As Double
code that define a Function procedure. Return 5.87 * n
End Function
9 Sub Statement Sub mySub(ByVal s As String)
Declares the name, parameters, and Return
code that define a Sub procedure. End Sub
10 Declare Statement Declare Function getUserName
Declares a reference to a procedure Lib "advapi32.dll"
implemented in an external file. Alias "GetUserNameA"
(
ByVal lpBuffer As String,
ByRef nSize As Integer) As Integer
11 Operator Statement Public Shared Operator +
Declares the operator symbol, operands, (ByVal x As obj, ByVal y As obj) As obj
and code that define an operator Dim r As New obj
procedure on a class or structure. ' implemention code for r = x + y
Return r
End Operator
12 Property Statement ReadOnly Property quote() As String
Declares the name of a property, and the Get
property procedures used to store and Return quoteString
retrieve the value of the property. End Get
End Property
13 Event Statement Public Event Finished()
Declares a user-defined event.
14 Delegate Statement Delegate Function MathOperator(
Used to declare a delegate. 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.
 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
SCOPE AND LIFETIME OF A VARIABLE

Variable’s Scope : The scope of a variable is the section of the


application that can see and manipulate the variable. If a variable is
declared within a procedure, only the code in the specific procedure
has access to that variable. When the variable’s scope is limited to a
procedure it’s called local.
e.g.
Private Sub Command1_Click()
+-Dim i as Integer
Dim Sum as Integer
For i=0 to 100 Step 2
Sum = Sum +i
Next
MsgBox “ The Sum is “& Sum
End Sub
Lifetime of a Variable
 Itis the period for which they retain their value. Variables
declared as Public exist for the lifetime of the application.
Local variables, declared within procedures with the Dim
or Private statement, live as long as the procedure.
 You can force a local variable to preserve its value
between procedure calls with the Static keyword. The
advantage of using static variables is that they help you
minimize the number of total variables in the application.
 Variables declared in a Form outside any procedure take
effect when the Form is loaded and cease to exist when
the Form is unloaded. If the Form is loaded again, its
variables are initialized, as if it’s being loaded for the first
time.
One of the tasks performed by the application is to track the
average of the numeric values. Instead of adding all the values
each time the user adds a new value and dividing by the count,
you can keep a running total with the function RunningAvg(),
which is shown below.

Calculations with Global Variables


Function RunningAvg(ByVal newValue As Double) As Double
CurrentTotal= CurrentTotal + newValue
TotalItems = TotalItems + 1
RunningAvg = CurrentTotal / TotalItems
End Function
You must declare the variables CurrentTotal and TotalItems
outside the function so that their values are preserved between
calls. Alternatively, you can declare them in the function with
the Static keyword, as shown below.
Calculations with Local Static Variables
Function RunningAvg(ByVal newValue As Double) As Double
Static CurrentTotal As Double
Static TotalItems As Integer
CurrentTotal = CurrentTotal + newValue
TotalItems = TotalItems + 1
RunningAvg = CurrentTotal / TotalItems
End Function
The advantage of using static variables is that they help you
minimize the number of total variables in the application. All
you need is the running average, which the RunningAvg()
function provides without making its variables visible to the
rest of the application. Therefore, you don't risk changing the
variables' values from within other procedures.
End of slide

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy