Anda di halaman 1dari 113

Deccansoft Software Services – MS.NET Overview of .

NET Framework

According to Microsoft .Net is a platform built on open internet protocols & standards with tools and
services that meld computing and communication in new ways

It’s an environment for developing and running software applications featuring ease of development of web
based services, rich standard runtime services available to components written in variety of programming
languages & provides inter language & inter machine inoperability

The .NET Framework is a new computing platform that simplifies application development in the highly
distributed environment of the Internet. The .NET Framework is designed to fulfill the following
objectives:
• Preserve consistency between objects that are stored and executed locally, objects that are
Internet-distributed but are executed locally, and objects that are stored and executed remotely on
a server.
• Avoid deployment and versioning conflicts. No DLL update upon deployment. Solves the DLL
Hell Syndrome.
• Guarantee safe execution of code, even if written by unknown semi-trusted third party. Avoid the
slowness of scripted or interpreted languages.
• Preserve development environment consistency between windows-based applications and web-
based applications.

The .NET framework includes two main components:


1. The .NET Class library
2. The Common Language Runtime.

.NET Base Class Libraries (also called as Framework Class Libraries (FCL)
The .NET base class library is a collection of object-oriented types and interfaces that provide object
models and services for many of the complex programming tasks you will face. Most of the types presented
by the .NET base class library are fully extensible, allowing you to build types that incorporate your own
functionality into your managed code. These class libraries are distributed with MS.NET Framework and
works with any language under the common language runtime environment. Therefore if you are familiar
with one .NET language then you can easily migrate to other .NET Languages
All the base class libraries are grouped under the root namespace System.

Namespace: A namespace is a logical collection of classes and other types with unique name. The
structure of the namespace is like a tree where all the related classes are like leaves.

The most important namespaces in the .NET class library are:


• System
• System.IO
• System.Collections
• System.Threading
• System.Reflection
• System.Security
• System.Net
• System.Data
• System.XML
• System.Web
• System.Web.Services
• System.Windows.Forms
• System.Drawing
• System.Globalization
• System.Resources

1
Deccansoft Software Services – MS.NET Overview of .NET Framework

CLR (COMMON LANGUAGE RUNTIME)

CLR is the foundation of .NET Framework. The common Language Runtime manages code at execution time. It does
Memory management, thread management, and runs the code on different platforms (Client or Server). It enforces strict
variable type definitions, security, and robustness.

CLR provides the following benefits for the application developers:


• Vastly simplified development.
• Seamless integration of the code written in various languages.
• Evidence-based security with code identity.
• Assembly-based deployment that eliminates DLL Hell.
• Side-by-side versioning of reusable components.
• Code reuse through implementation inheritance.
• Automatic object lifetime management.
• Self describing objects.

CLR is a component divided in sub components which perform their own respective tasks. CLR as the name specifies
provides a common runtime environment for different languages like VC++, C#, VB.NET, J# and JavaScript. The code
written in these languages is compiled with their respective language compliers to give a common intermediate language
called MSIL (Microsoft Intermediate Language) and Metadata. This files generated are called as PE (Portable
Executable).
The CLR is a multi-language execution environment

MS Intermediate Language: MSIL is an intermediate instruction set which is processor and hardware independent. The
source code when compiled gives MSIL which is an input to the operating system and with the help of CLR is converted
into native code which is processor specific.

Microsoft supply a tool called Ildasm, which can be used to view the metadata and IL for an assembly. Source code can be
reverse-engineered from IL, it is often relatively straightforward to regenerate high-level source (e.g. C#) from IL.

You can write IL programs directly, for example:

.assembly MyAssembly {}
.class MyApp
{
.method static void Main()
{
.entrypoint
ldstr Hello, IL!"
call void System.Console::WriteLine(class System.Object)
ret
}
}
Note: Just put this into a file called hello.il, and then run ilasm hello.il. An exe assembly will be generated.

2
Deccansoft Software Services – MS.NET Overview of .NET Framework

Portable Executable (PE) is a Microsoft Win32 compatible format file for .Net applications which contains the MSIL code
and Metadata in binary form. It has the extension .exe or .dll. PE has COFF (Common Object File Format) specification.

Metadata: Metadata is the information that describes every element managed by the runtime i.e. an assembly, a loadable
file, type, methods etc. This can also include information required for debugging and garbage collection, security attributes,
marshalling data, extended classes and member definitions, version binding etc.

To enable the runtime to provide services to managed code, language compilers must emit metadata that describes the
types, members, and references in your code. Metadata is stored with Code; every loadable common Language runtime
Portable Executable (PE) file contains metadata. The runtime uses metadata to locate and load classes, lay out instances in
memory, resolve method invocations, generate native code, enforce security, and set run-time context boundaries.

What is Type safety?


Defining the data types in a language to be specific and strict in their behaviour.

Datatypes are different in different languages and hence cannot be compiled with a single compiler. In .net languages CLR
takes the responsibility of verifying the typesafety of the code being executed both at compile time and runtime. Hence the
code is Managed.

CTS (COMMON TYPE SYSTEM)


CTS is the main component of the CLR. It is a specification where different datatypes are defined. This .net datatype
collection is used by the language compilers to map the language datatypes to datatypes defined in CTS. This ensures the
interoperability at runtime as the code written in one language when invoked from another language would not be
misinterpret the data because the calling language datatypes would map the same CTS datatypes to which the called
language datatypes are mapped.

CTS provide a framework for cross-language integration and address a number of issues:

• Similar but subtly different, types (for example, Integer is 16 bits in VB6, but int in C++ is 32 bits; strings in VB6
are represented as BSTRs and in C++ as char pointers or a string class of some sort; and so on)
• Limited code reuse(for example, you can’t define a new type in one language and impart into another language)
• Inconsistent object models.

For example, an integer variable in C# is written as int, whereas in Visual Basic it is written as integer. Therefore in .Net
Framework you have single type called System.Int32 to interpret these variables. Similarly, for the ArrayList datatype .Net
framework has a common type called System.Collections.ArrayList. In .Net Framework, System.Object is the common base
type from where all the other types are derived.

3
Deccansoft Software Services – MS.NET Overview of .NET Framework

COMMON LANGUAGE SPECIFICATION


CLS = Common Language Specification. This is a subset of the CTS, which all .NET languages are expected to support. The
idea is that any program, which uses CLS-compliant types, can interoperate with any .NET program written in any language.

The CLS is a set of constructs and constraints that serves as a guide for library writers and compiler writers. It allows
libraries to be fully usable from any language supporting the CLS, and for those languages to integrate with each other. The
CLS is a subset of the CTS. The CTS is also important to application developers who are writing code that will be used by
other developers. When developers design publicly accessible APIs following the rules of the CLS, those APIs are easily
used from all other programming languages that target the common language runtime.

In short, this allows very tight interoperability between different .NET languages, for example allowing a C# class to inherit
from a VB class.

NOTE: CLS is not a part of CLR Specification.

Few .NET Framework Supported Languages:

APL, C++, C#, COBOL, Component Pascal, Curriculum, Eiffel, Forth, Fortran, Haskell, Java Language, Microsoft Jscript,
Mercury, Mondrian, Oberon, Oz, Pascal, Perl, Python, RPG, Scheme, Small Talk, Standard Ml, Microsoft Visual Basic.

JIT (JUST IN TIME COMPILER)


JIT compiler compiles the managed or MSIL code into native code. JIT compiles only the required code for execution i.e. as
the code is being visited for execution and also the compiled code is cached so that if the block of code is called again the
same cached copy is directly executed. Advantage of managed code being compiled in small required fractions is processor
time.

There are three types of JIT compilers


• Standard JIT
• Econo JIT = Standard JIT – Caching (this is ideal for small devices)
• Pre JIT

Standard JIT: This compiles a block of code as it is visited for execution. The compiled code is cached so that the
subsequent call to the same block of code is directly reused.

Pre JIT: was planned in the initial draft of .NET but was never supported instead Microsoft provided a utility program
called NGen.exe (Native Generator) the complete MSIL code is compiled to native code for that machine and is stored in
GAC (GLOBAL ASSEMBLY CACHE) The advantage being the first time execution is also very fast because its already
in precompiled form, but the output of the NGen is not portable and is always machine dependent.

Note: we don’t have any facility to set our choice of JIT. For desktops Standard JIT is used and for Compact devices Econo
JIT is used.

Garbage Collection
The variables in the application can be allocated memory either in Global memory or Stack memory or Heap memory.
All global variables are allocated memory when the program starts execution and would remain in memory for the lifetime
of the application.
All local variables and parameters of a function are allocated memory when the function is called and they are deallocated
memory automatically when the function returns.
Heap memory is used for all dynamic memory requirements i.e. variables like pointers for which until runtime we don’t
know the amount of memory required would be allocated memory from the heap. But the size of heap is limited and all
allocations done on heap must be also deallocated when that pointer doesn’t need the memory anymore. The deallocated
heap memory then can be used for another pointer dynamic memory allocation.

Memory Leakage: If some pointers are allocated memory on the heap and are never deallocated from the heap before the
pointer itself goes out of scope then such memory can never be freed and would remain as Leakage from the heap as it
cannot be allocated to another pointer.

In .Net the memory de-allocation in .Net is handled by Garbage Collector. It gets activated when the heap is full and there
is a need for release of memory.

Garbage Collector a background thread running within the application / CLR and is responsible for destroying all the

4
Deccansoft Software Services – MS.NET Overview of .NET Framework

unreferenced objects (objects which are no longer in use). When garbage collector is initiated all other threads running
within the application are temporarily suspended and hence the sooner the garbage collector finishes its job the better would
be the performance of the application.

To optimize the work of garbage collector Microsoft has divided the heap memory into three equal parts called Generations:
GEN0, GEN1, and GEN2. This is compact garbage collection. New objects are always created in GEN0 and are promoted
to other generations based on their lifespan. Long lived objects would be destroyed last as these objects would move from
GEN0 to GEN1 and from GEN1 to GEN2.

Note: In .net the address of object can changes at runtime as it goes from one generation to another.
In .Net the object once created are never guaranteed to be destroyed because they might get promoted to higher generations
and GC would not visit those generations as memory is mostly available in lower generations itself.

Security Manager
Security manager is a component in CLR.
There are three types of security models supported in .Net.
• Code Access Security(CAS)
• Role Based Security
• Asp .Net Web Application Security.

Code Access Security


CAS determines whether or not a piece of code is allowed to run and also what resources to use. For example, CAS will
prevent malicious code from entering your system and causing havoc.

It is an integrated security model that grants permission to resources based on evidence and the evidence may include
1. From where the assembly is being loaded.
2. If downloaded from web what is the URL of the source directory?
3. What is the Strong Name
4. Who is the publisher, if digitally signed?

The CAS security policy revolves around two key concepts - code groups and permissions. Each .NET assembly is a
member of a particular code group and each code group is granted the permissions specified in a named permission set. An
example: Using the default security policy, a control downloaded from a web site belongs to the 'Zone - Internet' code group
which complies with the permissions defined by the 'Internet' named permission set.

Microsoft defines some default policies but you can modify these and even create your own. To view the code groups
defined on your system; Run 'caspol' from the command-line and checkout the different options on display

When a program code is using another code from another machine the security manager in CLR is responsible for managing
the implementation of CAS based on the identity or evidence of that machine from which the code is being used.

Class Loader
It is a sub component in CLR and is responsible for loading classes and other types as and when needed .It also loads the PE
if it’s not already loaded.

Assemblies:
Assemblies are building blocks of .Net framework applications. They form the fundamental unit of code execution,
deployment, version control, reusability, activation scoping and security permissions. It can be made of one file or made of
multiple files

An assembly can be a single file or it may consist of the multiple files. In case of multi-file, there is one master module
containing the manifest while other assemblies exist as non-manifest modules. A module in .NET is a sub part of a multi-file
.NET assembly. Assembly is one of the most interesting and extremely useful areas of .NET architecture along with
reflections and attributes, but unfortunately very few people take interest in learning such theoretical looking topics

Assemblies are self-describing by means of their manifest, which is an integral part of every assembly.

The manifest: (also called as Assembly Metadata)


• Establishes the assembly identity (in the form of a text name), version, culture, and digital signature (if the
assembly is to be shared across applications).
• Defines what files make up the assembly implementation.

5
Deccansoft Software Services – MS.NET Overview of .NET Framework

• Specifies the types and resources that make up the assembly, including which are exported from the assembly.
• Itemizes the compile-time dependencies on other assemblies.
• Specifies the set of security permissions required for the assembly to run properly.

This information is used at run time to resolve references, enforce version-binding policy, and validate the integrity of
loaded assemblies. The runtime can determine and locate the assembly for any running object, since every type is loaded in
the context of an assembly. Assemblies are also the unit at which code access security permissions are applied. The identity
evidence for each assembly is considered separately when determining what permissions to grant the code it contains.

The self-describing nature of assemblies also helps makes zero-impact install and XCOPY deployment feasible.

Types of Assemblies
Private Assembly: An assembly which is present in the same directory as the exe with every application refining to it.
Shared Assembly: A single copy of it is in GAC and is shared by all the applications referring to that assembly.
Satellite Assembly: A resource only assembly for a given culture. We can have more than one satellite assembly for one
application.

Assembly Manifest
(Meta Data)

Type Meta Data

MSIL
.NET Module contains
Resources
1. Type Meta Data
2. MSIL

Resource
DLL

Managed code: by managed code, it means that the complete life cycle and execution is managed by the .NET Common
Language Runtime (CLR). The .NET CLR manages the memory on behalf of the managed code, performs garbage
collection on the managed heap, perform assembly validation and assembly (component) resolution on behalf of the
program. The CLR also maintains the security constraints applied to the managed code.

Note: By un-safe code, it means that the managed program can access the memory address using pointers. There are two
points to remember here
• Un-safe code is different from un-managed as it is still managed by the CLR
• You still can not perform pointer arithmetic in un-safe code.

Un-managed code runs outside the CLR control while the unsafe code runs inside the CLR’s control. Both un-safe and un-
managed codes may use pointers and direct memory addresses.

6
Deccansoft Software Services Console- Main
• Solution is a Collection of Projects. One Solution can have many projects and each project can be of
different language. This file has the extension .sln.
• A Project is a collection of files including Source Code (.vb/.cs), Resources(.resx),
Configuration(.config) files etc…The project file has the extension .vbproj or .csproj
• A project when build (Compilation + Linking) generates a EXE/DLL as output.
Module Module1
Sub Main()
Console.WriteLine("Module1")
End Sub
End Module
Commandline Arguments:
The best way of providing input to the commandline application is using commandline arguments.
Module Module1
Sub Main(ByVal args As String())
Console.WriteLine("Hello " & args(0))
End Sub
End Module
args is the commandline arguments.
View Æ Solution Explorer Æ Project Æ Right Click Æ Properties Æ Debug Tab Æ Command Line
Arguments Æ Provide string separated by space
Return value of Main:
Module Module1
Function Main(ByVal args() As String) as Integer
Console.WriteLine("Hello " & args(0))
Return 0
End Function
End Module
The return value of Main is used by the current application to communicate its state to the parent process
when it terminates. The Interger return value can be predefined with some meaning and same will be used
by the parent process to know the state of child process terminated. This return value of an application is
called as EXIT CODE.
Multiple Modules in One Project
One Project can have multiple modules but only one of many module with valid Main can be treated as
Startup or entry point.
Module Module2
Sub Main()
Console.WriteLine("Module2")
End Sub
End Module
To Set Startup Object:View Æ Solution Explorer Æ Project Æ Right Click Æ Properties Æ Application
Tab Æ Starup Object Æ Select Module whose Main should be used as entry point.
Every Module in the project can be placed in same file or in different files. In either case the compiler is
going to compile together.
To Add another File with Module to Project: Goto Solution Explorer Æ Right Click on Project Æ Add
ÆModule

Following are the possible forms of Main which can be used in a module so that the module can be marked
as startup object. If Main is written in Class it must be marked as Shared.
Module Module1 Class Class1
Sub Main() Shared Sub Main()
Console.WriteLine("Module1") Console.WriteLine("Module1")
End Sub End Sub
Sub Main(ByVal args As String()) Shared Sub Main(ByVal args As String())
Console.WriteLine("Hello " & args(0)) Console.WriteLine("Hello " & args(0))
End Sub End Sub
Function Main() As Integer Shared Function Main() As Integer
Console.WriteLine("Hello") Console.WriteLine("Hello")
Return 0 Return 0
End Function End Function
Function Main(ByVal args() As String) Shared Function Main(ByVal args() As String)
Deccansoft Software Services Console- Main
as Integer. as Integer.
Console.WriteLine("Hello " & args(0)) Console.WriteLine("Hello " & args(0))
Return 0 Return 0
End Function End Function
End Module End Class

As an Object Oriented programmer its always better to avoid usage of Module in a project. This is because
every member of a module is by default treated as Global Member and it has free access through out the
project and this is against object orientation principles
Deccansoft Software Services – VB.NET VB.NET Language Basics
Integral Types
DataType Size . Net (CTS) Comments
Byte 1 System.Byte 0 - 255 It is Unsigned
SByte 1 System.SByte -128 to 127 - Signed
Short 2 System.Int16 Signed short
UShort 2 System.UInt16 Unsigned short
Integer 4 System.Int32 Range For
UInteger 4 System.Unt32 n – bits signed numbers: -2n-1 to 2n-1 - 1
Long 8 System.Int64 n-bits unsigned numbers = 0 to 2n - 1
ULong 8 System.UInt64

Floating Types
Decimal 16 System.Decimal Has up to 28 digits after decimal
Single 4 System.Single Has up to 8 digits after decimal
Double 8 System.Double Has up to 15 digits after decimal

Other DataTypes
Char 2 System.Char Uses Unicode Charset
String * 4 Systring.String Uses Unicode Charset
Boolean 2 System.Boolean True / False
Object * System.Object Generic Datatype
Date 8 System.DateTime

All the above datatypes are ValueTypes but String and Object are Reference Types.
Value Types directly hold the value- ValueType
All Basic Types, Structures & Enum
Heap Mem
Var = Value Ref type Value
Reference Types hold the reference to the value –
Sring, Object, Class, Arrays, Delegates
Var

Variable Declaration Syntax: • Based on datatype every variable declared is set to default
Dim var1, var2 as <DataType> value
Dim n1, n2 as Integer. • A variable declared in a block is local to the block in which it
Dim n1 as Integer = 10 is declared
Dim n1, n2 as Integer = 10 ‘ Invalid • A variable declared in outer block cannot be re-declared in the
Dim n1 as Integer = 10, n2 as Integer = 10 ‘Valid inner block.

If “Option Explicit Off” is mentioned then we don’t need to declare the variable and by default it is treated to be of type
“Object”
But if “Option Explicit On” is mentioned then every variable used must be declared otherwise it gives compilation error.

Literal Constants
I – Integer, L – Long, D – Decimal, F – Single, R – Double, C – Char
Example: 10L is Long, “A” is String, “A”c is Char.

Alternate Variable Declaration Syntax:


% - Interger, & - Long, @ - Decimal, ! – Single, # - Double, $ - String
Dim n as Integer or Dim n%

Implicit Casting:
Conversions between types can normally be achieved automatically only if by doing so we can guarantee that the value is not
changed in any way.

To convert one data type say byte to another data type say integer, no data is lost during conversion that’s why compiler
directly casts byte to integer .This is called implicit casting.

Explicit Casting:
There are some conversions that cannot be implicitly made between data types and the compiler will give an error if we are
attempted. However we can explicitly carryout such transactions using conversion functions. When we cast one type to
another we deliberately force the compiler to make the transformation.
1
Deccansoft Software Services – VB.NET VB.NET Language Basics

Conversion Functions:
CByte CDec CStr CShort CDbl CDate CInt CSng CLng CBool CChar CType/DirectCast/TryCast

In VB.NET Explicit Casting is needed only if “Option Strict On” is mentioned on top of the file and only then the VB.NET
language compiler treats the code as Strongly Typed Programming Language.

Increasing order of Range: Byte Æ Short Æ Integer Æ Long Æ Decimal Æ Single Æ Double
Casting is done based on range and not based on size of the datatype.

Byte can be implicitly assigned to any datatype.


Short can be implicitly assigned to all datatypes except Byte and So on…

Dim b as Byte : Dim n as Integer


n = b ‘Because has no chance of overflow allows Implicit Casting is done
'b = n 'Invalid because not all values of n can be assigned b
b = CByte(n) 'Explicit Casting
b = Convert.ToByte(n) 'Explicit Casting - If the value of n is not in range of 0 to 255 (range of byte) then OverFlowException
is thrown.

In VB.net by default integral Overflows are checked at runtime, this ensures the type safety of the code, but at the same time
it adds to performance overhead at the runtime
To Disable Overflow checks at runtime:
Project Æ Properties Æ Compile Æ Advanced Compile Options Æ Remove Overflow Checks – Check It

Boxing & Unboxing


Boxing is the term used to describe the transformation from value type to reference type (Object). The runtime creates a
temporary reference-type box for the object on the heap.

Dim n as integer = 20
Dim o as object = n ‘Boxing – The value of n is boxed on Heap memory and ref to that is stored in “o”

UnBoxing is the term used to describe the transformation from reference type (Object) to value type. We use the term cast
here, as this has to be done explicitly.

Dim m as integer = 20
Dim o as object = m ‘Boxing – Storing Value type as Object
Dim n as integer = CInt (o) ‘Unboxing – Extracting the value of ValueType from Object.

• When a value is boxed to an object type the object type cannot be used in mathematical operations.
• When the value of object type variable cannot be assigned to variable on LHS, an Exception of type
InvalidCastException is thrown.

Example:
IIf (condition as Boolean, exp1 as Object, exp2 as Object) as Object
Dim m, n, max as Integer
max = CInt (IIf (m>n, m, n)) ‘m and n are boxed to Object but return value has to be unboxed.

Some more Special Cases of Casting.


‘chr = n ‘Invalid We cannot cast integer to char or vice versa directly. For that we have to use special
'n = chr 'Invalid functions like AscW and ChrW
chr = CChar(n) 'Invalid chr = ChrW(n)
'n = CInt(c) 'Invalid n = AscW(chr)

Casting of String to any datatype 'Casting between integer and bool


str = "65" Dim n As Integer
'n = str 'Invalid Dim bln As Boolean
'str = n 'Invalid 'n = bln 'Invalid
n = CInt(str) 'bln = n 'Invalid
n = Integer.Parse(str) ‘Throws FormatException if n = CInt(bln) 'Explicit casting
Parsing fails bln = CBool(n) 'Explicit casting
n = Convert.ToInt32(str) ‘Casting any datatype to string
If (Integer.TryParse(str, n)) Then str = n.ToString()

2
Deccansoft Software Services – VB.NET VB.NET Language Basics
Console.WriteLine("Success") str = n & ""
Else str = CStr(n)
Console.WriteLine("Failed") str = Convert.ToString(n)
End If
In TryParse if Parsing fails, the value of second
parameter (“n”) is set to default value.
Constant Declaration
Const PI As Double = 3.14
'PI = 10 'Invalid – The value of constant cannot be changed.
Note: It is recomemded to have the name of constant variable always in upper case.

Enumerated Datatype
• Enums can be subtype of integral types only.
• Enum is a collection of constants.

Enum WeekDays as Integer Dim wd As WeekDays


Sun = 1 wd = WeekDays.Tues
Mon n=1
Tues 'wd = 1 ‘Invalid – Casting is needed.
Wed wd = CType(n, WeekDays) 'explicit casting
Thus n = wd 'implicit casting
Fri Console.WriteLine(wd) ‘Prints the Numeric Value
Sat Console.WriteLine(wd.ToString()) ‘Print the String Value
End Enum

About String and StringBuilder


Strings in .net are called as Immutable (not modifiable) objects. They are Reference Types and hence are allocated memory
on heap.
Immutable means once the value of string is set it cannot be changed.

Dim s as String
S = “Deccan” ‘Creates a new string on the heap.
S = S & “soft” ‘Creates another string on the heap by value “Deccansoft” and the old string “Deccan” is ready for garbage
collection.

Because of the above behavour it is not recommended that the string datatype is used for those variables which need frequent
modifications in their value.

Instead we should use the class called as “System.Text.StringBuilder” for those operations which need very frequent
modification to the string. It allocates memory to the string in blocks and the capacity is automatically managed.

Dim sb As New System.Text.StringBuilder(100) Modified value of string must be assinged to it again


Console.WriteLine(sb.Capacity) otherwise the value of it doesn’t change
For i As Integer = 0 To 50 str = "Demo String"
sb.Append(i) str.Replace(" ", "-") ‘Incorrect Usage – str doesn’t change.
Next Console.WriteLine(str)
Console.WriteLine(sb.Capacity) str = str.Replace(" ", "-") ‘Correct Usage
str = sb.ToString() Console.WriteLine(str)

Operators
Arithmetic Operators: +, - , *, / (Floating Type Division), \ (Integral Division), Mod (Modulus / Remainder), ^ (Power),
+=, -=, *=, ^=, /=, \=

• ^= & /= Operators can be only used with float variables.


• Dividing a Integral Type with zero throws DivideByZeroException
Floating Point Division by zero is not a runtime exception but the value would be either PositiveInfinity (if numerator is
greater than zero) or NegativeInfinity (if nuderator is less than zero) or NaN(if numerator is also zero)

Dim n as Integer
Dim m as Double = -4.0 / n
If (Double.IsNaN(m)) Then Console.WriteLine(“Not a Number”)
ElseIf (Double.IsPositiveInfinity(m)) Then Console.WriteLine("Positive Infinity")
3
Deccansoft Software Services – VB.NET VB.NET Language Basics
ElseIf (Double.IsNegativeInfinity(m)) Then Console.WriteLine("Negative Infinity")

Logical Operators:
A B A AndAlso B A OrElse B A XOR B Not A
True True True True False False
True False False True True False
False True False True True True
False False False False False True

• Is and IsNot are used to compare two reference types


Ex: (a1 Is a2) is true if both a1 and a2 are referencing to the same object.
(a1 IsNot a2) is true if both a1 and a2 are not referencing to the same object.

• TypeOf: Is used to check if a given reference variable is referencing to an object of a give class.
Ex: a TypeOf CA is True if a is referencing to an object of class CA or any of its subclass

Relational Operators: <, >, <=, >=, <>, = (for both equal and assignment operator)

Bitwise Operators: And and OR are the only two bitwise operators supported in VB.NET

• If (E1 and E2) – If E1is false then also E2 is evaluated even though result will be false only.
• If (E1 AndAlso E2) - – If E1 is false then E2 is not evaluated and the result will be false only.
• If (E1 Or E2) – If E1 is True then also E2 is evaluated even though result will be True only.
• If (E1 OrElse E2) - – If E1 is True then E2 is not evaluated and the result will be True only.

• a = b = c will not be allowed for any datatype other than Boolean.


• Note: Operators not supported in VB.NET: ++, --, ?:, <<. >>

Statements
If (C1) Then S1 Select Case n GoTo Statement
---------------- Case 1 Dim n1 as Integer = 0
If (C1) Then S1() ‘if n=1 la: Console.WriteLine("s1")
S1() Case 2, 3 n1 += 1
ElseIf (C2) Then S2 ‘If n=2 or 3 If (n1 = 5) Then GoTo cont
S2() Case Is < 6 GoTo la
ElseIf (C3) Then S3() ‘if n= 4,5 and 0 & –ve numbers cont:
S3() If (n = 5) Then Exit Select
Else S4() ‘if n= 4 and 0 & –ve numbers Goto is generally avoided in
S4() Case 6 To 9 programming because then the
End If S5() ‘if n=6,7,8,9 clarity in code is lost.
• ( ) are optional with condition Case Else
S6() ‘if n > 9
End Select
While Loop
While (n < 10) Do While (c1) Do
n += 1 ‘Excutes if c1 is True ‘First Executes the statents and then checks
If (n = 5) Then Continue While If (c2) Then Exit Do for the condition
If (n = 7) Then Exit While If (c2) Then Continue Do Loop Until (C1)
Console.WriteLine(n) Loop Do
End While
Do Until (c1) Loop While(C1)
‘Excutes if c1 is False
Loop
For Loops
For i As Integer = 1 To 10 Step 1 Prints all command line arguments What is the output of the below prog
If (i = 5) Then For i = 0 To args.Length - 1 For i As Integer = 1 To 3 Step 1
Continue For Console.WriteLine(args(i)) For j As Integer = 1 To 3 Step 1
End If Next If (i = j) Then
Same result can be also achieved as Exit For
If (i = 7) Then below End If
Exit For For Each s As String In args Console.WriteLine(i & " " & j)

4
Deccansoft Software Services – VB.NET VB.NET Language Basics
End If Console.WriteLine(s) Next
Console.WriteLine(i) Next Next
Next i What is the o/p?
What is the o/p ?
Note: "For Each” statement can be used only for arrays and collections i.e objects which have implemented the interface
IEnumerable

With Statement
With (args) Its not supported in C#
Console.WriteLine(args.Length) Console.WriteLine(.Length)
Console.WriteLine(args.Rank) Console.WriteLine(.Rank)
End With

Working with Arrays


• Arrays are reference types and thus are allocated memory on heap.
• All arrays irrespective of their type are by default inherited from System.Array class
• They always dynamic because we can set the size of the arrays at runtime.
• Size of array can never be changed at runtime.

Dim ar(X) as Integer ‘X is the variable


Dim ar(10) As Integer ‘Declared and Initialized and array of lengh 11
Dim ar(0 To 10) As Integer
Dim ar as Integer(10) ‘Invalid
Dim ar As Integer() ‘Only declaration and “ar” is by default is set to “Nothing”
Dim ar() as Integer
ReDim ar(10) ‘Initializes an array of lengh 11
ReDim ar(0 To 10)
Console.WriteLine(ar.Length)

ReDim Preserve ar(0 To 20) ‘Creates a new array of 21 elements and because of the keyword Preserve copies the data from
the old array into new array and the old array would be ready for garbage collection.

Dim ar() as Integer = {1, 2, 3} ‘Declares and Initializes and array of 3 elements i.e Length 3.
ar = New Integer() {1, 2, 3}

Dim ar(-1) as Integer – Declares and Intialzes an Array with “0” length.

Multidimensional Arrays:
Dim ar(2, 3) As Integer
Console.WriteLine(ar.Length) ‘12
Console.WriteLine(ar.Rank) ‘2 – Gives the total number of dimensions
Console.WriteLine(ar.GetLength(0)) ‘ 3 – Gives the number of elements in 1st dimension.
Console.WriteLine(ar.GetLength(1)) ‘ 4 – Gives the number of elements in 2nd dimension.

Dim ar( , ) As Integer


ReDim ar(2, 3)
ReDim ar(3, 4) 'No Exception and both the dimension sizes can be changed as preserve is not used.
ReDim Preserve ar(3, 5) –No Exception as only outer Dimension size is changed.
ReDim Preserve ar(4, 5) 'Throws runtime ArrayTypeMisMatchException because only the Outermost dimension size
can be changed if keyword preserve is used and here the inner dimension size is changed.

Dim ar(,) As Integer = {{1, 2}, {3, 4}, {5, 6}}


Console.WriteLine(ar.Length) 'Prints - 6
Console.WriteLine(ar.Rank) ' Prints - 2
Console.WriteLine(ar.GetLength(0)) ' Prints - 3 ‘Gives the number of rows in a matrix.
Console.WriteLine(ar.GetLength(1)) ' Prints - 2 ‘Gives the number of columns in a matrix
Console.WriteLine(ar(1,1)) ‘Prints – 4

Workiing with Methods


Types of Procedues :
1. Sub Procedures – Do not return any value
2. Function Procedures – Returns a value.
5
Deccansoft Software Services – VB.NET VB.NET Language Basics
3. Property Procedures – To be covered in OOPS

In VB.NET the name of the function is treated as Variable in that function. The return type of the function is the datatype of
that variable and the last value assigned to that becomes the return value of that function.

Public Class Program


Shared Sub Main(ByVal args() As String)
Dim n1, n2 As Integer
n1 = Integer.Parse(args(0)) ‘Converts the command line argment to Integer.
n2 = CInt(args(1))
Dim res As Integer
res = Add(n1, n2) ‘Uses the default value for third parameter
Console.WriteLine(res)
Console.Write("Enter first number : ")
n1 = Integer.Parse(Console.ReadLine())
Console.Write("Enter second number : ")
n2 = Integer.Parse(Console.ReadLine())
res = Add(n1, n2)
Console.WriteLine("Result: " & res)
Console.WriteLine(Add("Deccan", "soft")) ‘Invokes the string parameter method
res = Add(1, 2, 3, 4)
res = Add(1, 2, 3)
res = Add(1, 2)
res = Add(1, 2, , 4)
res = Add(1, 2, d:=4, c:=3) ‘Named Arguments – Sequence of paramets is not important.
res = Add(b:=1, a:=2, d:=4, c:=3)
Dim mar() As Integer = {1, 2, 3}
res = Add(mar) ‘Invokes the Version with array parameter
Console.WriteLine(res)
Console.WriteLine(Add(1, 2, 3)) ‘Invokes the version with 3 parameters
Console.WriteLine(Add(1, 2, 3, 4, 5, 6)) ‘Invokes the ParamArray Version
Console.WriteLine(Add(1, 2, 3, 4, 5, 6, 7))
Console.WriteLine(Add(1, 2, 3, 4, 5, 6, 7, 8))
Console.WriteLine(Add(1, 2, 3, 4, 5, 6, 7, 8, 9))
Console.WriteLine(Add(1))
End Sub
'Shared Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
' Return a + b
'End Function
Shared Function Add(ByVal a As Integer, ByVal b As Integer,
Optional ByVal c As Integer = 0, Optional ByVal d As Integer = 0) As Integer
Return a + b + c + d
End Function
Shared Function Add(ByVal s1 As String, ByVal s2 As String) As String
Return s1 & s2
End Function
Shared Function Add(ByVal ParamArray ar() As Integer) As Integer
For Each n As Integer In ar
Add += n
Next
End Function
End Class

Method Overloading:
• Having two or more methods with same name and different parameters
• Parameters must be different either in their datatype or in their count.
• Method cannot be overloaded based on Return Type or ByVal/ByRef or Parameter names.
• Call to the Overloaded method is resolved at compile time and is done based on datatype and count of arguments passed
to the method.

Optional Parameters
• Every Optional Parameter must have default value
• Once a parameter is declared as optional all subsequent parameters in the list also must be declared as optional

6
Deccansoft Software Services – VB.NET VB.NET Language Basics

Named Arguments:
• While calling a method we can pass arguments based on parameter names and we don’t have to then pass them in
sequence.

ParamArray Parameter:
• Only Parameters which are of type array can be marked as ParamArray
• If parameter is marked as ParamArray either a reference to the array can be passed as argument or otherwise 0 or more
individual argurements can be passed.
• Only one parameter of a method can be declared as ParamArray Parameter.
• It must be always the last parameter in the list of parameters for a given method.
• If we have other parameters in the list they must be before the ParamArray parameter and they cannot be declared as
optional.

Static Local Variable


Public Class Program1 A local variable can be declared as static and in such case it is allocated memory when the
Public Shared Sub Main() method is called for the first time and it retains its value for the subsequent call to the same
Foo() method. It doesn’t get removed from the memory unlike other local variables or parameters
Foo() of a method.
Foo()
End Sub Note: Static Local Variables are not supported in C#
Shared Sub Foo()
Static Dim n As Integer Output of the program
n += 1 1
Console.WriteLine(n) 2
End Sub 3
End Class

Value Type passed ByVal and ByRef


Public Class Program The argument “n1” is passed by value (ByVal) i.e a
Public Shared Sub Main() dublicate copy of that variable is created on the stack of the
Dim n1, n2 As Integer function by name “a”. Any change made to parameter “a”
n1 = 10 is not reflected in “n1”
n2 = 10
Demo(n1, n2) The argument “n2” is passed by reference to “b”, i.e both
Console.WriteLine(n1 & " " & n2) “b” and “n2” reference to same memory and hence change
End Sub made to “b” is also reflected in “n2”.
Shared Sub Demo(ByVal a As Integer, ByRef b As Integer)
a += 1 Result: 10 11
b += 1
End Sub
End Class
In Main Before Foo is Called In Foo In Main After Foo is returned
n1 = 10 a = 10 11 n1 = 10
n2 = 10 n1 = 10 n2 = 11
b/n2 = 10 11

Reference Type can also be passed ByVal and ByRef (this topic will be covered in OOPS)

General Points:
• “:” is the Statement terminator ex: a=1 : b=2
• “_” is the Statement Continuation char
Ex: a = b _
+_
c
• If Keywords have to be used as Identifiers enclose them in [ ]
ex: Function [Sub](a as Integer, b as Integer) as Integer
return a-b
End Function
• If Strings have the value as "(Quote) replace that with Double Quote ("")
ex: Console.WriteLine("This is a ""Quoted"" word")
• vbCRLF is a New Line charater and vbTab is the Tab character.

7
Deccansoft Software Services- MS.NET VB.NET Examples
Program to check the range of a datatype Public Class IIF_Example
Public Class Max_Min_Values Public Shared Sub Main()
Public Shared Sub Main() Dim n, m, o As Integer
Console.WriteLine(Integer.MaxValue) m=5
Console.WriteLine(Integer.MinValue) n = 10
Console.WriteLine(Short.MaxValue) o = CInt(IIf(m < 10, m, n))
Console.WriteLine(Short.MinValue) Console.WriteLine(o)
End Sub End Sub
End Class End Class
Program to demonstrate If Statement
Program for type-casting
Public Class Type_Casting Dim grade As String
Public Shared Sub Main() Console.WriteLine("Enter a grade")
Dim n As Integer = 50 grade = Console.ReadLine()
Dim s As Short = 5 If (grade = "E" OrElse grade = "e") Then
Dim str As String = "100" Console.WriteLine("Excellent")
Dim m As Integer ElseIf (grade = "G" OrElse grade = "g") Then
Console.WriteLine("Good")
‘TO CONVERT FROM STRING TO INTEGER Else
'm = CInt(str) Console.WriteLine("No Such Grades")
'm = Integer.Parse(str) End If
'm = Convert.ToInt32(str) Program to demonstrate Select Statement
' Dim bl As Boolean Dim grade As String
Console.WriteLine("Enter a grade")
' To check if string is convertible to integer and try parsing grade = Console.ReadLine()
' bl = Integer.TryParse(str, m) Select Case (grade)
m += 1 Case "E" or “e”
Console.WriteLine("Excellent")
' TO CONVERT FROM INTEGER TO STRING Case "G" or “g”
'str = CStr(n) Console.WriteLine("Good")
'str = n.ToString Case Else
str = n & "" Console.WriteLine("No Such Grades")
str = str + " Days" End Select
Console.WriteLine(str)
End Sub Program to demonstrate GoTo statement
End Class Public Class Label_Example
Public Shared Sub Main()
Program to demonstrate In-Built Ascii Function: Console.WriteLine("One")
Public Class Ascii_Functions GoTo Five
Public Shared Sub Main() Console.WriteLine("Two")
Console.WriteLine(ChrW(65)) Console.WriteLine("Three")
Console.WriteLine(AscW("B")) Console.WriteLine("Four")
End Sub Five:
End Class Console.WriteLine("Five")
Program to demonstrate Boxing and UnBoxing Console.WriteLine("Six")
Public Class Boxing Console.WriteLine("Seven")
Public Shared Sub Main() Console.WriteLine("Eight")
Dim n As Integer = 65 Console.WriteLine("Nine")
Dim o As Object Ten:
o = n 'This is Boxing Console.WriteLine("Ten")
Console.WriteLine(o) End Sub
n = CInt(o) ' This is UnBoxing End Class
Console.WriteLine(n)
End Sub Program to demonstrate Select statement
End Class Public Class Select_Example
Public Shared Sub Main()
Program to demonstrate IIF function an alternative to Dim n As Integer = 1
Ternary operator Select Case n
Option Strict On Case 1

1
Deccansoft Software Services- MS.NET VB.NET Examples
Console.WriteLine(1) Console.WriteLine()
Case 2, 3 Do
Console.WriteLine(2 & " & " & 3) Console.WriteLine(n)
Case Is < 6 n=n-1
Console.WriteLine("Less Than 6") Loop Until (n = 0)
Case 6 To 9 End Sub
Console.WriteLine("6 To 9") End Class
If (n = 7) Then Exit Select
Console.WriteLine("Not 7") Program to generate tables from 1 to 5 using For loop
Case Else Class table
Console.WriteLine("Else Case") Public Shared Sub Main()
End Select Dim n, m As Integer
End Sub For n = 1 To 10
End Class For m = 1 To 5
Console.Write(m & "*" & n & "=" & n * m &
Program to demonstrate While loop vbTab)
Public Class While_Example Next
Public Shared Sub Main() Console.WriteLine()
Dim n As Integer = 5 Next
While (n > 0) End Sub
Console.WriteLine(n) End Class
n=n-1
End While Program to generate a triangle using For loop
Console.WriteLine() Class triangle
Do While (n < 5) Public Shared Sub Main()
Console.WriteLine(n) Dim n, m, o As Integer
n=n+1 For n = 10 To 1 Step -1
Loop For o = 0 To n - 1
Console.WriteLine() Console.Write(" ")
Do Next
Console.WriteLine(n) For m = n To 10
n=n-1 Console.Write("* ")
Loop While (n > 0) Next
End Sub Console.WriteLine()
End Class Next
End Sub
Public Class WhileDemo End Class
Shared Sub Main()
Dim marks, count, avg, total As Integer Program to demonstrate Arrays
Console.WriteLine("Enter 6 Subject marks") Public Class Array_Example
While (count < 6) Public Shared Sub Main()
marks = Console.ReadLine() Dim ar() As Integer = {1, 2, 3, 4, 5}
total += marks Console.WriteLine(ar.Length)
count += 1 For Each tmp As Integer In ar
End While Console.WriteLine(tmp)
avg = total / 6 Next
Console.WriteLine("Total is " & total) ReDim ar(10)
Console.WriteLine("Average is " & avg) For Each tmp As Integer In ar
End Sub Console.WriteLine(tmp)
End Class Next
'ReDim Preserve ar(10)
Program to demonstrate Do Until..Loop ‘For Each tmp As Integer In ar
Public Class Until_Example ‘Console.WriteLine(tmp)
Public Shared Sub Main() ‘Next
Dim n As Integer End Sub
Do Until (n = 5) End Class
Console.WriteLine(n)
n=n+1 Program to demonstrate Function Overloading
Loop Public Class Functions_Example

2
Deccansoft Software Services- MS.NET VB.NET Examples
Public Shared Sub Main() Console.WriteLine(ar1(0) & " " & ar2(0))
Dim m, n As Integer Console.WriteLine(ar1(1) & " " & ar2(1))
Dim o As Short = 12 End Sub
m = 10 : n=5 Shared Sub Foo(ByVal a As Integer, ByRef b As Integer)
'Console.WriteLine(Add("Hello", "Harish")) a += 1
'Console.WriteLine(m, n) b += 1
'Console.WriteLine(Add(o, 5)) End Sub
Console.WriteLine(Add(b:=10, a:=50)) Shared Sub Foo(ByVal a() As Integer, ByRef b() As Integer)
End Sub ReDim a(1)
Shared Function Add(ByVal a As Short, ByVal b As ReDim b(1)
Integer) As Integer End Sub
Console.WriteLine("One") End Class
Add = a + b
End Function Program to demonstrate passing an array of fixed length to
Shared Function Add(ByVal a As Integer, ByVal b As a function:
Integer) As Integer Public Class Array_Sum
Console.WriteLine("Two") Public Shared Sub Main()
Add = a + b Dim a() As Integer = {1, 2, 3, 4, 5}
End Function Console.WriteLine(Add(a))
Shared Function Add(ByVal a As String, ByVal b As String) End Sub
As String Shared Function Add(ByVal a() As Integer) As Integer
Add = a & b For Each a1 As Integer In a
End Function Add += a1
End Class Next
End Function
Program to demonstrate static variable End Class
Public Class Static_Local_Variable
Public Shared Sub Main() Program to demonstrate passing an arrays of variable
Foo () length to a function
Foo ()
Foo () Public Class ParamArray_Example
Foo() Public Shared Sub Main()
End Sub Dim m, n, o As Integer
Shared Sub Foo() m=5
Static Dim a As Integer n = 10
a += 1 o = 15
Console.WriteLine(a) Console.WriteLine(Add(m, n, o, 2, 3, 4, 5, 6, 7, 8, 9, 1))
End Sub Console.WriteLine(Add(1,2,3))
End Class Console.WriteLine(Add(m,1,n,2,0,3,4)
End Sub
Program to demonstrate Call-By-Value and Call-By- Shared Function Add(ByVal ParamArray a() As Integer) As
Reference Integer
Public Class ByRef_ByVal For Each tmp As Integer In a
Public Shared Sub Main() Add += tmp
'Dim m, n As Integer Next
'm = 5 End Function
'n = 10 End Class
'Console.WriteLine("a: " & m)
'Console.WriteLine("b: " & n)
'Foo(m, n)
'Console.WriteLine("After Foo():")
'Console.WriteLine("a: " & m)
'Console.WriteLine("b: " & n)
Dim ar1() As Integer = {10, 10}
Dim ar2() As Integer = {10, 10}
Console.WriteLine(ar1(0) & " " & ar2(0))
Console.WriteLine(ar1(1) & " " & ar2(1))
Foo(ar1, ar2)
Console.WriteLine("After Foo():")

3
Deccansoft Software Services C# - Language Basics
Integral Types
DataType Size . Net (CTS) Comments
byte 1 System.Byte 0 - 255 It is Unsigned
sbyte 1 System.SByte -128 to 127 - Signed
short 2 System.Int16
ushort 2 System.UInt16
int 4 System.Int32
uint 4 System.Unt32
long 8 System.Int64
ulong 8 System.UInt64

Floating Types
decimal 16 System.Decimal Has up to 28 digits after decimal
float 4 System.Single Has up to 8 digits after decimal
double 8 System.Double Has up to 15 digits after decimal

Other DataTypes
char 2 System.Char Uses Unicode Charset
string * 4 Systring.String Uses Unicode Charset
bool 2 System.Boolean
object * System.Object Generic Datatype
Variable Declaration Syntax:
int a,b;
int a=10, b=20;
• A local variable declared must be explicitly initialized before used in an expression otherwise gives an compilation
error.
• byte, short, char types when used in an expression are automatically raised to the rank of Integer.

using System; char c = 'S';


class Program
{ //int to char.
public static void Main() n = c;
{ //c = n; //invalid
int n; c = (char)n; //Explicit Casting
byte b=0;
n = b; //Implicit casting //bool – int/anydatatype explicit or implicit casting is
//b = n; //Invalid – Explicit casting is required not allowed either ways
n = 255;
b = (byte) n; //string to int
Console.WriteLine(b); s = "100";
checked //-always checks for Overflow n = int.Parse(s);
{ n = Convert.ToInt32(s);
b = (byte)n; //If overflows exception is thrown //n = (int)s;//Invalid
}
unchecked //-Neverchecks for Overflow //int to string
{ s = n.ToString();
b = (byte)n; //Exception in never thrown s = n + "";
Console.WriteLine(b); s = Convert.ToString(n);
}
float f = 0.6F; //long to float
Console.WriteLine(f); long lng = 10;
decimal dec = 0.6M; f = lng; //Long can be casted to float
double dbl = 0.6D; lng = (long) f;

//float and decimal requires casting object obj = n; //Boxing


dec = 10; //Integer to decimal n = (int)obj; //Unboxing
//f = dec; //Invalid
//dec = f; //Invalid const double PI = 3.14;
f = (float)dec;
dec = (decimal)f; byte b1, b2, b3;
b1 = b2 = b3 = 10;
string s = "Demo"; b1 = b2 + b3 //Compilation Error

1
Deccansoft Software Services C# - Language Basics

//if byte, short or char are used in an expression they are


automatically raised to the rank of int. //Enum Sample
b1 = (byte) (b2 + b3); n = 2; //n is int
WeekDays wd = WeekDays.Sat;
Console.WriteLine(3 / 2); //Result 1 wd = (WeekDays) n;
Console.WriteLine(3.0 / 2); //Result 1.5 //n = wd; //Invalid
n = (int) wd; //In VB.NET casting is not required
n = 10; }
int m = n++; //m=n; n=n+1 - Post Increment }
Console.WriteLine(n + " " + m); enum WeekDays : int
m = ++n; //n=n+1; m=n - Pre Increment {
Console.WriteLine(n + " " + m); Sun=1, Mon, Tues, Wed, Thus, Fri, Sat
}
//?: Conditional Operator Enum is a subtype of any integral type.
int max;
max = n > m ? n : m; //?: Operator

Note: You cannot use the checked and unchecked keywords to control floating point (non-integer) arithmetic. The
checked and unchecked keywords control only integer arithmetic. Floating point arithmetic never throws
OverflowException — not even when you divide by 0.0

Operators
Arithmetic +, -, * , / , % (mod)
Logical Operators ^ (XOR), !, && , ||
Ternary Operator ?:
String concatenation +
Increment, decrement ++ , --
Bitwise << , >>, & , | , ~
Relational = = , != , < , > , <= , >=
Assignment = , += , -= , *= , /= , %= , &= , | = , ^= , <<= , >>=
Type information is , sizeof , typeof, as
Indirection and Address * , -> , [] , &

Note for VB.NET Programmers:


• In C# the arithmetic operator ‘/ ’ is the same for both floating point and integer datatypes and the result depends on the
operands.

Control Statements
if-statement: Switch Statement
if (BooleanExpression) int n;
{ switch (expr) //expr can only integral type / char / string
statement; {
} case 0: //value of case must be a constant.
else if (Boolean-Expression) statements;
{ goto default; // or break can be used.
statement; case 1:
} case 2:
else statements;
{ break; //break must be preset after every case with statements
statement; default:
} statements;
break; //break must be present after default also.
}
while…..loop do
while (BooleanExpression) {
{ Statements;
Statements; }while (BooleanExpression)
}

2
Deccansoft Software Services C# - Language Basics
for and foreach statements class Program
for ( initializer; condition; iterator ) {
{ static void Main(string[] args)
statements; {
} for (int i = 0; i < 3; i++)
foreach (DataType identifier in <Array or Collection>) {
{ for (int j = 0; j < 3; j++)
embedded-statements; {
} if (i == j)
for (int i = 0; i < 10; i++) break;
{ Console.WriteLine(i + " " + j);
if (i == 7) break; }
if (i == 3) continue; }
Console.WriteLine(i); }
} }
foreach (string s in args)
Console.WriteLine(s);

• break and continue statements can be used in for and while loops.
• C# doesn’t support with statement

Working with Arrays


1.Single-Dimensional Arrays
int [] myArray = new int [5];
string myStringArray = new string[5];
int [] myArray = new int [] {1 , 2 , 3 };
string[] weekdays = new string[] {“Sunday”, “Monday”, ”Tuesday”};
When you initialize an array upon declaration, it is possible to use the following shortcuts:
int[] myArray = {1, 3, 5, 7, 9};
string[] weekDays = {"Sun", "Sat", "Mon", "Tue"};
It is possible to declare an array variable without initialization, but you must use the new operator when you
assign an array to this variable. For example:
int[] myArray;
myArray = new int[] {1, 3, 5, 7, 9}; // OK
myArray = {1, 3, 5, 7, 9}; // Error

2.MultiDimensional Arrays
int[,] myArray = new int[4,2];
Also, the following declaration creates an array of three dimensions, 4, 2, and 3:
int[,,] myArray = new int [4,2,3];
You can initialize the array upon declaration as shown in the following example:
int[,] myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}};
You can also initialize the array without specifying the rank:
int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};
If you choose to declare an array variable without initialization, you must use the new operator to assign an array to the
variable. For example:
int[,] myArray;
myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}}; // OK
myArray = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error

class Program
{
static void Main(string[] args)
{

int[] ar = new int[ ] {1,2,3,4};


Console.WriteLine(a.Length);
Console.WriteLine(a.Rank);
foreach (int n in ar)
Console.WriteLine(n);
//To increase the size of existing array and also retaining data in it. i.e equivalent of Redim Preserve in VB.
int[] temp =ar;
ar = new int[temp.Length +1];

3
Deccansoft Software Services C# - Language Basics
Array.Copy(temp, ar, temp.length);
temp=null;
foreach (int n in ar)
Console.WriteLine(n);
}
}
3.Jagged Arrays(Array-of-Arrays)
A jagged array is an array whose elements are arrays. A jagged array is sometimes called an "array-of-arrays." The
following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array
of integers:
int[][] myJaggedArray = new int[3][];
Before you can use myJaggedArray, its elements must be initialized. You can initialize the elements like this example:
myJaggedArray[0] = new int[5];
myJaggedArray[1] = new int[4];
myJaggedArray[2] = new int[2];
It is also possible to use initializers to fill the array elements with values, in which case youDon’t need the array size,
Example
myJaggedArray[0]=new int[] {1,2,3,4,5};
myJaggedArray[1]=new int[] {1,2,3,4};
myJaggedArray[2]=new int[] {1,2};
You can also initialize the array upon declaration like this
int[][] myJaggedArray = {new int[]{1,2,3,4,5},new int[]{1,2,3,4}, new int[]{1,2}};
int[][] myJaggedArray;
myJaggedArray = new int[][] {new int[]{1,2,3,4,5},new int[]{1,2,3,4},new int[]{1,2}};

Working with functions


using System; static int Add(int a, int b)
class Program2 {
{ return Add(a, b, 0);
public static void Main() }
{ static int Add(int a, int b, int c)
int res = Add(10, 2); {
int[] mar = { 1, 2, 3, 4 }; return a + b + c;
res = Add(mar); }
res = Add(); static string Add(string s1, string s2)
res = Add(1, 2, 3, 4, 5); {
res = Add(1, 2, 3, 4, 5, 6); return s1 + s2;
res = Add(1, 2, 3, 4, 5, 6, 7); }
int n1, n2, n3; static int Add(params int[] ar)
n1 = n3 = 10; {
Foo(n1, out n2, ref n3); int sum = 0;
Console.WriteLine(n1 + " " + n2 + " " + n3); //for (int i = 0; i < ar.Length; i++)
} // sum += ar[i];
//Pass by value and reference example
static void Foo(int a, out int b, ref int c) foreach (int n in ar)
{ sum += n;
a++;
b = 20; return sum;
c++; }
} }

• If a function has return type anything other than “void”, it must return a value.
• Out parameter must be initialized in the method and are generally used in situtions where we want to return more
than one value from the function.
• C# doesn’t support Optional parameters with default values and Named arguments.

General Points;
For String: @”C:\Demo” or “C:\\Demo”, “This is \”Quoted\” String”

4
Deccansoft Software Services C# - Language Basics
class ProgramForPyramid
{
static void Main(string[] args)
{
int k=0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j <= i; j++)
Console.Write(k++ + "\t");
Console.WriteLine();
}
}
}

class ProgramForMaxOf3Numbers
{
static void Main(string[] args)
{
int n1 = int.Parse(Console.ReadLine());
int n2 = int.Parse(Console.ReadLine());
int n3 = int.Parse(Console.ReadLine());
int max = n1 > n2 ? n1 : n2;
max = max > n3 ? max : n3;
Console.WriteLine(max);
}
}
class ProgramForMaxOf3Numbers
{
static void Main(string[] args)
{
string str = Console.ReadLine();
string[] ar = str.Split(' ');
int sum = 0;
for (int i = 0; i < ar.Length; i++)
{
sum += int.Parse(ar[i]);
Console.WriteLine(ar[i]);
}
Console.WriteLine("Average: " + sum / ar.Length);
}
}

5
MS.NET - Object Oriented Concepts
Overview

Class: A class is a template / skeleton / blueprint for creating an object.

Object: An Object is an entity that has properties for validations, methods for functionality and events for depicting the change of
state.

Every object has the data and behavior with which they are differed. Data associated at any given instance of time is the state of an
object.

Component: A ready to use third party object can be called as a Component. It can be replaced without any changes in the
application. A component is generally used by a programmer as an object.

An application can be called as a collection of related objects exchanging messages with each other.

Loosely coupled objects are better than tightly coupled objects i.e. the lesser the information given to other objects the better it is
as the objects are loosely coupled the dependencies are less and stronger security.

Every object oriented language should have three features


• Encapsulation
• Inheritance
• Polymorphism

Encapsulation: Binding of data and behavior i.e. functionality of an object within a secured and controlled environment is
encapsulation.

Inheritance: The Process of acquiring the existing functionality of the parent and with new added features and functionality by a
child object is called inheritance.
The advantages of inheritance are Generalization, Extensibility and Reusability.
For example: A calculator is a generalized form of mathematical operations where as a Scientific calculator is an Extended and
Specific form.

Polymorphism: An object in different forms and in each form it exhibits the same functionality but implemented in different
ways.
For example:
A man who knows more than one language can speak any language he knows. Here the functionality is speech and person is the
object.
A faculty can take a form of Java Faculty or MSNET faculty and in both the forms he teaches, but what he teaches differs. Here
functionality is teach and faculty is the object.

Object Creation and Instantiation


In MS.NET when an object is created there is no way to get the address of an object. Only the reference to the object is given
through which we can access the members of the class for a given object. When an object is created all the variables (value/
reference types) are allocated the memory in heap as a single unit and default values are set to them based on their data types.

Working with Classes & Objects


Syntax in VB:
Dim <Variable name> as <Class name> or Dim <Variable name> As New <Class Name>
<Variable name> = New <Class Name>
Syntax in C#:
<Class name> <Var name>; or <Class name> <Var name> = new <Class name> ( );
<Var name> = new <Class name> ( );

Account Example
1. Create a new project (File -> New Project). Give the name of project as AccountApplication.
2. In Solution Explorer, Right Click and add to the project a new class and name it as Account

Code in VB: Code in C#:

1
MS.NET - Object Oriented Concepts
Public Class Account class Account
Public Id As Integer {
Public Name As String public int Id;
Public Balance As Decimal public String Name;
Public Sub New() public Decimal Balance;
MsgBox("Object created") public Account()
End Sub {
Protected Overrides Sub Finalize() MessageBox.Show(“Object Created”);
MsgBox("Object Destroyed") }
End Sub ~Account()
End Class {
MessageBox.Show(“Object Destroyed”);
}
}

In VB: Dim a As Account In C#: Account a;


“a” is a refrence variable of type Account with default value set to Nothing(VB) or null(CS).
Note: “a” is not an object of type Account.

In VB: a = New Account() In C#: a = new Account()


• ‘Creates an object of type Account (allocating memory on heap to every member of the Account class) and its reference is
assigned to ‘a’.
• Every member allocated memory is set to its default value based on the datatype.
• The value of the reference variable is reference to an object.

In the AccountApplication, Change the name of the Form, “Form1” to “AccountForm”


a.Id = 1
The above statement can be read as: “Id” menber of an object of type Account
referenced by “a” is set to 1.

Note: If a reference variable is not intialized i.e referring to either “Nothing” or null,
then trying to access any member using it will throw a runtime exception i.e.
“NullReferenceException”.

Dim a1 as Account
a1 = a //Copies the value of a (reference to the object) into a1 and thus both “a1” and
“a” refers to the same object.

Note: One Object can have many references but one reference variable cannot refer to
many objects.
Account Form Code in VB: Account Form Code in C#:
Public Class AccountForm public partial class AccountForm: Form
Dim a As Account {
Private Sub btnCreate_Click(. . .) Handles btnCreate.Click Account a;
a = New Account() private void btnCreate_Click(...)
End Sub {
a = new Account();
Private Sub btnGet_Click(...) Handles btnGet.Click }
txtId.Text = a.Id.ToString private void btnGet_Click(...)
txtName.Text = a.Name {
txtBalance.Text = a.Balance.ToString txtId.Text = a.Id.ToString;
End Sub txtName.Text = a.Name;
txtBalance.Text = a.Balance.ToString;
Private Sub btnClear_Click(...) Handles btnClear.Click }
txtId.Text = "" private void btnClear_Click(...)
txtName.Text = "" {
txtBalance.Text = "" txtId.Text = "";
End Sub txtName.Text = "";

2
MS.NET - Object Oriented Concepts
txtBalance.Text = "";
Private Sub btnSet_Click(...) Handles btnSet.Click }
a.Id = Integer.Parse(txtId.Text) private void btnSet_Click(...)
a.Name = txtName.Text {
a.Balance = Decimal.Parse(txtBalance.Text) a.Id = int.Parse(txtId.Text);
End Sub a.Name = txtName.Text;
a.Balance = double.Parse(txtBalance.Text);
Private Sub btnDestroy_Click(...) Handles btnDestroy.Click }
a = Nothing private void btnDestroy_Click(...)
End Sub {
a = null;
Private Sub btnGC_Click(...) Handles btnGC.Click }
GC.Collect() ‘Forces the activation of GC private void btnGC_Click(...)
End Sub {
GC.Collect();
Private Sub btnTemp_Click(...) Handles btnTemp.Click }
Dim a1 = New Account() private void btntemp_Click(...)
a = a1 {
End Sub Account a1 = new Account();
a = a1;
Private Sub btnGetGeneration_Click(...) Handles btnGC.Click }
MsgBox(GC.GetGeneration(a)) private void btnGetGeneration_Click(...)
End Sub {
End Class MessageBox.Show(GC.GetGeneration(a).ToString());
}
}

Working with Methods


The methods of the object are used to describe the functionality / behavior.
Recommendation: A method name should start with uppercase and parameters should use camel notation.
Syntax in VB:
Public Sub/Function <MethodName> (ByVal/ByRef parameter1 as <datatype>, . . .) as <returntype>
End Sub/Function
Syntax in C#:
public <return type> <MethodName>(<datatype> paramerter1,….)
{}
Note:
• An instance method is always invoked on the current object.
• While writing a method in a class, do not set to it any data as parameter which is already the data member in the same class.
• If parameter/local variable and datamember of a class have same name, the local variable takes the precedence over
datamember and if datamember has to accessed in such same “Me” (VB) or “this” (CS) can be used.
• In a method “Me” or “this” is reference to the current object on which the method is invoked.

In the Account Class add the following Code


Code in VB: Code in C#:
Public Sub Deposit(ByVal Amount As Decimal) public void Deposit(Decimal Amount)
Me.Balance += Amount {
End Sub this.Balance += Amount
Public Sub Withdraw(ByVal Amount As Decimal) }
If (Me.Balance <= 500) Then public void Withdraw(Decimal Amount)
Throw New ApplicationException("Insufficient {
Balance") if (this.Balance <= 500)
Else throw new ApplicationException("Insufficient Balance");
Me.Balance -= Amount else
End If this.Balance -= Amount
End Sub }

3
MS.NET - Object Oriented Concepts

Add Deposit (btnDeposit) and Withdraw (btnWithdraw) buttons and Amount (txtAmount) textbox to the AccountForm. Add the
following code to the event handler of the btnDeposit and btnWithdraw.

Private Sub btnDeposit_Click( . . . ) Private Sub btnWithraw_Click( . . . )


a.Deposit(CDec(txtAmount.Text)) a.Withdraw(CDec(txtAmount.Text))
End Sub End Sub

Note: Whenever any of the business rules of an object or the integrity of the property or a method is violated, it should respond by
throwing a runtime exception.
For example in the above method Withdraw( ) we are checking for balance to be a minimum of 500, if violated an exception is
raised.

Working with Object Properties


• A property procedure doesn’t hold any data by itself.
• A property is made up of two blocks i.e. Get and Set. Get block is invoked when the property is used on RHS and Set block is
executed when the property is used on LHS.
• Generally a Property encapsulates a field member of the same class, so that the filed member is not freely accessible outside
the class and the required validation/restriction can be put over it.
• A Set block can validate the value before it is assigned to the field member of the class being encapsulated by the property.
Similarly the field member can be validated before its value is returned in the Get block.
• Using a property the field member can be restricted with either “ReadOnly” or “WriteOnly” access.

Syntax in VB: Syntax in C#:


Private _property name as <Datatype> Private <Datatype> _<propertyname>;
Public [ReadOnly/WriteOnly] Property <property name> as <Datatype> public <Datatype> <PropertyName>
[Private] Get {
Return _<property name> [private] get
End Get {
[Private] Set (ByVal Value as < Datatype >) return _propertyname;
_<property name> = Value }
End Set [private] set
End Property. {
_propertyname = value;
}
}
Recommendations:
1. Let every private member of a class begins with underscore ( _ ).
2. Property in a class mostly wraps around the field member of the same class and thus they can be used for giving restricted and
valid access to the field member.

Add the properties for Name, Balance and Id with following conditions:
• Name should accept less than or equal 8 characters only
• Balance should be ReadOnly.
• Id should be set only once.
Edit the code in the Account Class as below.
Code in VB: Code in C#:
Private _Id As Integer private int _Id
Private _Name As String private string _Name
Private _Balance As Decimal private decimal _Balance
'Balance Property public decimal Balance
Public ReadOnly Property Balance() As Decimal {
Get get
Return _Balance {
End Get return _Balance;
End Property }
Public Property Name() As String }
Get public String _Name
4
MS.NET - Object Oriented Concepts
Return _Name {
End Get get
Set(ByVal Value As String) {
If (Value.Length > 8) Then return _Name;
Throw New ApplicationException("Name cannot be }
> 8 characters") set
Else {
_Name = Value if (value.Length > 8)
End If throw new ApplicationException("Name
End Set cannot be > 8 characters");
End Property _Name = value;
Private _idAlreadySet As Boolean }
Public Property Id() As Integer }
Get private bool idAlreadySet;
Return _Id public int Id
End Get {
Set(ByVal value As Integer) get
If (idAlreadySet) Then {
Throw New ApplicationException("Id is already set") return _Id;
End If }
_Id = Value set
idAlreadySet = True {
End Set if (idAlreadySet)
End Property throw new ApplicationException("Id is already set");
_id = value;
idAlreadySet = true;
}
}
Note: In btnSet_Click of AccountForm please
replace Balance = Decimal.Parse(txtBalance.text)
with a.Deposit(Decimal.Parse(txtBalance.Text))

• A class which has all its field members as Private and if required restricted access to these members is given using Public
Properties, then such a aclass is called “Fully Encapsulated Class”.

Constructors and Destructors


• A constructor is a member method of a class which is automatically executed as soon as the object is created and thus it be
ideally used for initializing the field members of the class.
• In VB.NET a constructor is always named as “New” and is a sub procedure
• In CS it has same name as the class and doesn’t have a return type not even void.
• A constructor without a parameter is called default constructor.
• Parameterized constructor is a constructor in which the parameters are defined.
• Copy Constructor: A constructor with object reference of the same class as the parameter is called a copy constructor. When
we want to create a new object with is the duplicate of the existing object we use a copy constructor.

5
MS.NET - Object Oriented Concepts
Syntax in VB: Syntax in C#:
Sub New () <Class name>()
‘default constructor {
End Sub ‘default constructor
}
Sub New (Byval p1 as <datatype>, p2 as <datatype>,….) <Class name>(<datatype> p1, …)
‘Parameterized constructor {
End Sub ‘parameterized constructor
}
Sub New (Byval <parname> as <classname>) <Class name>(<Class Name> <parname>)
‘copy constructor {
End Sub ‘copy constructor
}
1. If a class doesn’t have any form of constructor, a public default constructor is automatically added to it by the language
compiler.
2. Mostly Copy Constructor is used to create a new object by duplicating the state of an existing object and this is done by
copying the datamembers value of existing object in new object.
Note:
In VB: Using Me.New() or MyClass.New() as the first statement in a constructor we can call another constructor of same class.
In C#: We have to put “this” after the declaration of the constructor.

Add the Following code to the Account Class


Code in VB: Code in C#;
'Default Constructor //default constructor
Public Sub New() public Account()
End Sub {}
Public Sub New(ByVal id As Integer, ByVal name As String, //Parameterised constructor
ByVal balance As Double) public Account(int id, String name, decimal balance) : this()
MyClass.New() {
Me.Id = id this.Id = id;
Me.Name = name this.Name = name;
Me._Balance = balance this._Balance = balance;
End Sub }
'Copy Constructor //Copy Constructor
Public Sub New(ByVal a As Account) public Account(Account a) : this(a.Id,a.Name,a.Balance)
MyClass.New(a.Id,a.Name,a.Balance) {
‘Me.Id = a.Id //this.Id = a.Id;
‘Me.Name = a.Name //this.Name = a.Name;
‘Me._Balance = a.Balance //this._Balance = a.Balance;
End Sub }

In AccountForm make the following Changes:


Private Sub btnCreate_Click(. . .) Handles btnCreate.Click
Dim id As Integer = Integer.Parse(txtId.Text)
Dim name As String = txtName.Text
Dim initialBalance As Decimal = Decimal.Parse(txtBalance.Text)
a = New Account(id,name, initialBalance)
End Sub

Destructor: A destructor is used to release the resources that an object is holding. When a object is ready to be destroyed finalize
method is called on that object.

Syntax in VB: Syntax in C#: Note: A destructor in C# cannot have any access modifiers
Protected Overrides Sub Finalize() ~<class name>()
End Sub { }.
Microsoft has suggested two methods that can be invoked by the programmer for the release of resources Close() or Dispose(). If
any one of these methods is invoked by the programmer care must be taken to avoid Finalize() method to be invoked on the same

6
MS.NET - Object Oriented Concepts
object.
Public Sub Dispose () ‘ or Close()
‘Write code here to release the resources.
GC.SuppressFinalize(Me)
End Sub
Note: In VB.NET only the above mentioned form of Finalize is treated as Destructor. We can have multiple overloaded forms of
Finalize but they will be treated as Destructors.

Working with Shared Members


For every new instance of a class all the instance members are allocated memory, but shared field members of a class are allocated
memory only once irrespective of the number of objects created and they are allocated memory when the class is loaded. These
members are also called class members and are accessed outside the class using class name.
Note: A class is loaded when either the shared members of the class is accessed for the first time or when the first instance of the
class is created. A class once loaded will remain in memory permanently.

Note: A public Shared/Static member of class can be used as Global member of the application because it can be assessed using
class name from any part of the application.

Shared Constructor: A constructor is defined with the keyword as Shared (in VB) or static (in C#). It is used to initialize the
shared member and is executed when the class is loaded.
This is invoked by the class loader of CLR when the class is loaded by it and hence cannot be overloaded nor can be declared
with any accesses specifier like public or private.

Series of events that occur when the first object is created:


1. Class is loaded
2. Shared members are loaded and allocated memory
3. Shared constructor is executed
4. Instance members are loaded and allocated memory and Initialized
5. Instance constructor is executed.

Code in VB: (Make Id as AutoIncrement Field) Code in C#:


Public Shared MinBalance as Integer = 500 public static int MinBalance = 500;
Private Shared _PrevId as Integer public static int _PrevId
Private _Id As Integer private int Id;
Public Sub New() public Account()
_PrevId += 1 {
_Id = _PrevId _PrevId += 1;
End Sub _Id = _PrevId;
Public ReadOnly Property Id() As Integer }
Get public int Id
Return _Id {
End Get get { return _Id }
End Property }
Note: Because “Id” is now auto increment field, remove the id parameter from the constructor.
In Withdraw method replace the constant value 500 with the shared member “MinBalance”
In Form: Add btnGetMB and btnSetMB buttons and txtMB textbox

Private Sub btnGetMB_Click(...) Private Sub btnSetMB_Click(...)


txtMB.Text = Account.MinBalance.ToString() Account.MinBalance = CDec(txtMB.Text)
End Sub End Sub

Shared Method: A method which does not have anything to do with the state of the object can be marked as Shared method.
Note: All methods and properties (procedures) of the class irrespective of whether they are shared or not are allocated memory
only once.

7
MS.NET - Object Oriented Concepts
Shared Version Instance Version
Public Shared Function GetAccountWithLessBalance(ByVal Public Function GetAccountWithLessBalance(ByVal a As
a1 As Account, ByVal a2 As Account) As Account Account) As Account
If (a1.Balance < a2.Balance) Then If (Me.Balance < a.Balance) Then
Return a1 Return Me
Else Else
Return a2 Return a
End If End If
End Function End Function
• A shared members of a class can be accessed using class name where as the instance member of a class is accessed using a
reference variable referring to an object
• Instance members of the class cannot be accessed in a shared/static constructor or any other shared/static method or property
of the class unless it is qualified by a reference variable referring to an object of that class.. For Example: In the above method
a1.Balance is valid even though “Balance” is instance member of a class, but using Me.Balance (or just Balance) is not valid.
• An instance member can access shared members of the class
• The keyword “Me” or “this” cannot be used in the shared / static member because it doesn’t have any object on which it is
invoked.
• The entry point method Main in the class is marked as Static/Shared because it has to be invoked by the CLR even before the
object of our class is created.
• It is recommended that for all Global requirements we use Class with Shared / static members instead of using Module. This is
because a public member of a Module can be accessed anywhere in the project without qualifying it with Module name but the
same is not valid for public Shared / static member of a class.
Singleton Class: A class that can be instantiated only once is called a singleton class.
1. The constructor of the class has to be made as private so that an object of the class cannot be directly created outside the class.
2. An Static / Shared method (ex: GetObject) can be added to the class to create the instance and return the reference to the same.
Code in VB: Code in C#:
Public Class SingletonDemo public class SingletonDemo
Private Shared obj As SingletonDemo {
Private Sub New() private static SingletonDemo obj;
End Sub private SingletonDemo ()
Public Shared Function GetObject() As SingletonDemo {}
If obj Is Nothing Then public static SingletonDemo GetObject()
obj = New SingletonDemo() {
End If if (obj == null)
Return obj obj = new SingletonDemo ();
End Function return obj;
End Class }
}
static void Main(string[] args)
{
SingletonDemo s1, s2;
//s1 = new SingletonDemo(); //Invalid because the constructor is Private
s1 = SingletonDemo.GetObject();
s2 = SingletonDemo.GetObject();
Console.WriteLine(s1 == s2); //True
s1.Data = 10;
Console.WriteLine(s2.Data); //Prints 10 because both s1 and s2 are refencing to same object.
}
• A class in C# can be declared as static and such a class can have only static members and instance members are not allowed.
Also such a class cannot be instantiated. (This is not supported in VB.NET)
static class Demo
{
public static int P1;
public static void Foo()
{}
}
• A VB.NET module is compiled as a class with all members declared as static.

8
Deccansoft Software Services Inheritance
Inheritance depends upon “is a” relationship.
.Syntax in VB: Syntax in c#:
Public Class CA class CA
{
End Class }
Public Class CB class CB : CA
Inherits CA {
End Class }

Constructors and Inheritance:


1. Create a New Project (ConsoleApplication1)
2. Add the following code to the file in that project.
class CA class CB:CA
{ {
public int PubA; public int pubB;
private int PriA; public CB() //First calls parent class constructor.
protected int ProA; {}
public CA() public CB(int a,int b,int c,int d) : base(a,b,c)
{} {
public CA(int a, int b, int c) : this() //this.PriA = a; this.ProA =b; this.PubA = c;
{ this.PubB = d;
this.PriA = a; }
this.ProA = b; public void Foo()
this.PubA = c; {
} ProA = 10; //Allowed
} CA a = new CA();
Protected members are accessible only within a.ProA = 10 //Compilation Error.
the class and in derived classes and not }
accessible to non derived classes }

class Program
{
static void Main(String[] args)
{
CB b;
b = new CB(1,2,3,4);
b.Foo()
}
}
Notes:
• The protected member of parent class cannot be accessed in a child class method using the reference variable of
type parent.
• When an object of child class is created all the data members of the parent class and child class are allocated memory
irrespective of their access modifier i.e. public or private or protected.
• Whenever an object of child class is created, even though the child class constructor is visited first, it it’s the parent
class constructor which is executed first. This is because the child class constructor can then override the code already
executed in parent class constructor.
• Every child class constructor by default links itself to the parent class default constructor.
• Using “base” in C# and “MyBase.New” in VB.NET and with appropriate arguments a child class constructor can
make an explicit link to any other constructor of parent class.
• The order of visiting constructor is child class first and then parent because the child class constructor if required can
pass data to the parent class constructor using MyBase.New or base
• It is recommended that a child class constructor always pass data to the parent class constructor so that the parent
class constructor can initialize the data members of the parent class and the child class constructor remains with
initializing only its own data members.
• If the parent class doesn’t have default constructor, then every child class constructor must explicitly link to any other
constructor of the parent class.
• The order of execution of destructor is child first and then parent which is reverse order of execution of constructor.
• In VB.NET we cannot use both MyBase.New and MyClass.New in same constructor and same is also applicable for
C#.

1
Deccansoft Software Services Inheritance

Type Casting of Reference Types


CA a; CB b;
a = new CB(); //Valid
b = new CA(); //Invalid and Compilation Error.
• A reference variable of type parent can refer to an object of child class because all the members which the reference
variable can access exist in memory when the object is of child class.
• A reference variable of child cannot refer to an object of parent class because the child class members that the
reference variable can access do not exit in memory when the object is of parent class.

CA a; CB b;
a = b //is Valid – Does Implicit casting because the object to which “b” can refer to is only of type “CB” (child class) and a can
also refer to that object.

b = a // In invalid because a can refer to an object of type “CA” and “CB” but “b” cannot refer to an object of type “CA”,
hence the compiler for such statements give an error.

a = new CB () ;
b = a; //is still not allowed
b = (CB) a;
//Explicit Casting is valid but if “a’ refer to an object of type “CA” then at runtime InvalidCastException is thrown

CA a = new CB() / CA();


CB b = a as CB; //”as” is an operator
Console.WriteLine(b == null);

About as Operator: if “a” is referring to an object of Type “CB” or any of its subclass then the reference is assigned to “a”
otherwise it assigns “null” to “a” (it doesn’t throw InvalidCastException)

In VB, the syntax is for casting from one object to another type:
b = CType (a, CB)
or
b = DirectCast(a,CB)

The DirectCast method is much faster in performance but should be used only if the datatype of the object is exactly known.
Even otherwise it doesn’t throw an exception but we don’t get performance benefit (it would be same CType).

b = TryCast(a,CB) ---Equivalent of as operator in CS


if “a” is referring to an object of Type “CB” or any of its subclass then the reference is assigned to “a” otherwise it assigns
“Nothing” to “a” (It doesn’t throw InvalidCastException)

Note: Compiler always compiles every statement or expressions based on the datatype variables invoked.
In VB explicit casting is required only if “Option Strict On”
Note: Using a reference variable of type parent, child class members cannot be accessed even if the object is of child class.

Operator for comparing two reference types: “= =” in C# and “Is” / “IsNot” in VB.NET
Static and Dynamic Binding
Virtual methods are dynamically binded i.e., the actual method called would be decided at runtime and the decision is based on
the object to which the reference variable invoking the method is referring to.

Methods not declared as virtual are statically binded i.e., the actual method invoked would be decided at compile time and is
based on purely the datatype of the reference variable invoking the method.

In Class CA ---- virtual virtual


In Class CB new new override
CA a = New CB (); Static Dynamic Dynamic
a.Foo (); CA CA CB

If the method is not declared as anything then it is static binding.


If the method is declared as “virtual” then it is dynamic binding.
class CA class CB : CA
{ {
public virtual void Foo() public override void Foo()
{ {

2
Deccansoft Software Services Inheritance
Console.WriteLine("Foo() of CA"); Console.WriteLine(“Foo() of CB");
} }
} }
class Program
{
static void Main()
{
CA a = new CB();
a.Foo();
}
}
Try the above program with and without “virtual” and “override” keywords in both “CA” and “CB classes.
• If the object is of child class and the child class “overrides” the method only then the child class method is executed.
Otherwise, it will always execute parent class method.
• A method declared as new can also be declared as “virtual new” and such methods shadows the parent class method but
can be overridden in the child class if needed.
• Only virtual methods can be overridden in the child class.

Note: Instance variable are always statically binded at compile time based on the datatype of the reference variable accessing
the member. Instance variable cannot be declared as “virtual” in parent class and thus if a variable with same name is declared
in the child class then it should be declared as “new”.

CA: ------ virtual

CB: new virtual new new override new virtual override sealed

CC: new new override new new override override new new

1 2 3 4 5 6 7 8 9
CA a= new
CB() Static Static Static Dynamic Dynamic Dynamic Dynamic Dynamic Dynamic
a.Foo() CA CA CA CA CB CB CA CA CB

CB b = new
Static Dynamic Dynamic Static Dynamic Dynamic Dynamic Dynamic Dynamic
CC()
CB CB CC CB CB CC CC CB CB
b.Foo()
CA a= new
Static Static Static Dynamic Dynamic Dynamic Dynamic Dynamic Dynamic
CC()
CA CA CA CA CB CC CA CA CB
a.Foo()
• A class declared as sealed (C#) or NotInheritable (VB) cannot be inherited.
• A method declared as sealed (C#) or NotOverridable (VB) cannot be overridden in the child class.
• A sealed class cannot have abstract methods.
• In VB.net if a method of parent class is overloaded in child class then it may be declared as “Overloads”.

Abstract Classes and Methods


• An abstract method is a method in which declaration is provided but implementation is not.
• A class with one or more methods declared as abstract must be declared as abstract and cannot be instantiated because its
behaviour is incomplete.
• A class that inherits the abstract class must override all the abstract members of the parent class, if not it should also be
declared as abstract.

3
Deccansoft Software Services Inheritance
• A class need not have any method defined as abstract but still can be defined as abstract and cannot be instantiated.
• A class which is not abstract is called as Concrete Class.
• An abstract class can have all types of members which a concrete class can have including constructor, destructor, static
members etc…

Create a New Console Application and add the following code to the various files added to that project.
abstract class Figure Public MustInherit Class Figure
{ Public Dimension As Integer
public int Dimension; Public MustOverride Function Area() as Double
public abstract double Area(); Public MustOverride Function Perimeter() as Double
public abstract double Perimeter(); End Class
}
class Square : Figure
{ Class Square
public override double Area() Inherits Figure
{ Public Overrides Function Area() as Double
return Dimension * Dimension; Return Dimension ^ 2
} End Function
public override double Perimeter() Public Overrides Function Perimeter() as Double
{ Return 4 * Dimension
return 4 * Dimension; End Function
} End Class
}
class Circle : Figure
{ Class Circle
public override double Area() Inherits Figure
{ Public Overrides Function Area() as Double
return Math.PI*Dimension* Dimension; Return Math.PI * Dimension ^ 2
} End Function
public override double Perimeter() Public Overrides Function Perimeter() as Double
{ Return 2 * Math.PI * Dimension
return 2 * Math.PI * Dimension; End Function
} End Class
}
class Program Public Class Program
{ Shared Sub Main()
static void Main() Dim fig As Figure
{ fig = New Circle() ‘ or Square()
Figure fig=new Square() // or Circle(); fig.Dimension = 10;
fig.Dimension = 10; Console.WriteLine(fig.Area())
Console.WriteLine(fig.Area()) Console.WriteLine(fig.Perimeter())
Console.WriteLine(fig.Perimeter()) End Sub
} End Class
}
Try the above program once with Square object and once with Circle Object.
• If Object is of type Square, then the Area and Perimeter of Square is printed. Similarly if the object is of type Circle then
Area and Perimeter of Circle is printed.

VB.NET C#
Overridable virtual
Overrides override
Shadows new
NotInheritable (class) sealed (class)
NotOverridable sealed
MustInherit abstract (class)
MustOverride abstract (method)
Overloads ---

4
Deccansoft Software Services Inheritance

Late Binding
Every Class in .Net is inherited from System.Object and hence a reference variable of type System.Object can refer to an
object of any class.
Class CA Dim ob as Object
Public Sub Foo() ob = new CA
Console.WriteLine(“Foo in CA”) ob.Foo() ‘is correct in VB if Option String Off .
End Sub ob.Foo1() ‘Throws MissingMemberException
End Class
• The above call is called as Late Binding because until runtime it is not decided whether the method “Foo” is existing in
the object of the class referenced by “ob”. If the method is existing it would be called otherwise it would through a
runtime exception i.e. MissingMemberException.
• It also degrades the performance as the existence of the method has to be checked at runtime and then the method has to
be called.
• C# doesn’t support LateBinding and it is not allowed in VB.Net only if “Option Strict” is on.

Passing Reference Type ByVal and ByRef

Class Demo Public Shared Sub Foo1(ByVal d1 As Demo, ByRef d2 As Demo)


Public N As Integer d1.N += 1
End Class d2.N += 1
Class Test End Sub
Public Shared Sub Main() Public Shared Sub Foo2(ByVal d1 As Demo, ByRef d2 As Demo)
Dim md1 As New Demo() d1 = New Demo()
Dim md2 As New Demo() d1.N = 10
md1.N = 5 : md2.N = 5 d2 = New Demo()
Foo1(md1, md2) d2.N = 10
Console.WriteLine(md1.N & " " & md2.N) End Sub
Foo2(md1, md2) End Class
Console.WriteLine(md1.N & " " & md2.N)
End Sub

Explanation for Foo1 call


A reference type, irrespective of pass ByVal or ByRef, if the data of the object referenced is modified using the parameter then
the change is also reflected in the reference variables used as arguments of the calling method because both the argument and
parameter are referring to the same object.

Explanation for Foo2 call


A reference type when passed ByVal and if the reference to the object is modified in the called method i.e. if the parameter
reference is changed to refer new object, the reference to the object in the calling method doesn’t change i.e. the argument
will continue referencing the old object only.

A reference type when passed ByRef and if the reference to the object is modified in the called method i.e. if the parameter
reference is changed to new object, the reference to the object in the calling method also changes i.e. the argument will
continue referencing the old object only.

Note: Please also try the above program replacing “Class Demo” with “Structure Demo”

Differences between Structure and Class


1. Structures are value type and memory is allocated on stack. Classes are ref types and memory is allocated on heap.
2. Structure cannot be inherited but class can be inherited.
3. Structures must at least one data member in it.
4. Structure cannot have default constructor. But it can have all other types of constructors. If a Constructor is provided in
the structure it must initialize all the datamembers of the Structure.
5. If Demo is a Structure the following two lines means the same
Demo d = new Demo();
Demo d;
i.e The variable of type Structure by default is initialized with default data and default value is not “null” unlike class.

5
Deccansoft Software Services Interfaces
• An interface is a collection of related methods and properties without implementation.
• An interface is a point of contact with an object.
• All public members of a class can be said as an interface to it.
• Interface = Pure Abstract Class (A class with all members declared as abstract)
• An interface represents a contract, in that a class that implements an interface must implement every aspect of that
interface exactly as it is defined.
• An interface cannot be instantiated.
• Every member of an interface is by default public and abstract. Interface cannot have field members
• An interface can have Static Constructor but cannot have default constructor.
• A variable of type interface can refer to an object of a class implementing that interface.

Where do we use Interface?


• Interfaces are better suited to situations in which your applications require many possibly unrelated object types to
provide certain functionality.
• Interfaces are a powerful programming tool because they allow us to separate the definition of objects (interface) from
their implementation (class).
• Interfaces are more flexible than base classes because you can define a single implementation that can implement
multiple interfaces.
• Interfaces are better in situations in which you do not need to inherit implementation from a base class.
• Interfaces are useful in cases where you cannot use class inheritance. For example, structures cannot inherit from classes,
but they can implement interfaces.

Figure Example: Create a new Console Application and add the following code to the project.
Interface IFigure interface IFigure
Property Dimension() As Integer {
Function Area() as double int Dimension {get;set;}
Function Perimeter() as double double Area();
End Interface double Perimeter();
}
Class Circle Class Square
Implements IFigure Implements IFigure
Public Function Area() as double Implements IFigure.Area Public Function Area() as double Implements IFigure.Area
Return Math.PI * Dimension ^ 2 Return Dimension ^ 2
End Sub End Sub
Private _Radius as Integer Private _Side as Integer
Public Property Dimension() As Integer _ Public Property Dimension() As Integer _
Implements IFigure.Dimension Implements IFigure.Dimension
Get Get
Return _Radius Return _Side
End Get End Get
Set(ByVal value As Integer) Set(ByVal value As Integer)
_Radius = value _Side = value
End Set End Set
End Property End Property
Public Function Perimeter() as double Public Function Perimeter()as double
Implements IFigure.Perimeter Implements IFigure.Perimeter
Return 2 * Math.PI * Dimension Return 4 * Dimension
End Sub End Sub
End Class End Class

1
Deccansoft Software Services Interfaces
class Circle: IFigure Implicit Implementation
{ class Square : IFigure
private int mRadius; {
int IFigure.Dimension //Explicit Implimentation private int _Side;
{ public int Dimension //Implicit Implimentation
get { {
return _Radius; get {
} return _Side;
set { }
_Radius = value; set {
} _Side = value;
} }
double IFigure.Area() //Explicit Implimentation }
{ public double Area() //Implicit Implimentation
return Math.PI * _Radius * _Radius; {
} return Math.PI * _Side * _Side;
double IFigure.Perimeter() //Explicit Implimentation }
{ public double Perimeter() //Implicit Implimentation
return 2 * Math.PI * _Radius; {
} return 2 * Math.PI * _Side;
} }
}
Main in VB.NET Main in C#
class Program class Program
Public Shared Sub Main(ByVal args() as String) {
Dim fig as IFigure = Nothing public static void Main(string[] args)
If (args(0) == "S") {
fig = new Square() IFigure fig=null;
Else if (args[0] == "S")
fig = new Circle() fig = new Square();
End If else if (args[0] == "C")
fig.Dimension = 10 fig = new Circle();
Console.WriteLine(fig.Area()) fig.Dimension = 10;
Console.WriteLine(fig.Perimeter()) Console.WriteLine(fig.Area());
End Sub Console.WriteLine(fig.Perimeter());
End Class }
}

Implicit / Explicit Implementation of Methods in C#:


interface IA1 interface IA2 //Case 2
{ { class CA : IA1, IA2
void Foo1(); void Foo1(); {
void Foo2(); void Foo3(); void IA1.Foo1()//Explicitly Implemented
} } {
// Case1 Console.WriteLine("In IA1.Foo1");
class CA : IA1, IA2 { }
public void Foo1() //Implicitly Implemented void IA2.Foo1() //Explicitly Implemented
{ {
Console.WriteLine("In Foo1"); Console.WriteLine("In IA2.Foo1");
} }
public void Foo2() //Implicitly Implemented public void Foo2() //Implicitly Implemented
{ {
Console.WriteLine("In Foo2"); Console.WriteLine("In IA1.Foo2");
} }
public void Foo3() //Implicitly Implemented public void Foo3() //Implicitly Implemented
{ {
Console.WriteLine("In Foo3"); Console.WriteLine("In IA2.Foo3");
} }
} }

Note: In the project either add code of “Case 1” or “Case 2”

2
Deccansoft Software Services Interfaces
class Program
{
public static void Main()
{
IA1 a1; IA2 a2; CA a;
a = new CA();
a1 = a; //Implicit Casting because every object referenced by “a” can also be refenced by “a1”
a2 = a; //Implicity Casting because every object referenced by “a” can also be refenced by “a2”
//a = a1; //Invalid because every object referenced by “a1” cannot also be refenced by “a”
a = (CA)a1; //Explicit Casting
a1 = (IA1)a2; //Explicity Casting - //an interface variable can never be implicitly casted to
another unless they are related as parent and child.
a.Foo1(); //In case1 prints “In Foo1” but in case 2 it compilation error.
a.Foo2();
a1.Foo1(); //In case1 prints “In Foo1” but in case 2 it prints “In IA1.Foo”.
a2.Foo1(); //In case1 prints “In Foo1” but in case 2 it prints “In IA2.Foo”.
}
}
• In VB.Net, a class member implementing the interface need not have same name as the name of the interface member
and it can have any access modifer.
• In C# every implicity implementated method must be public and must have same name as the member of interface
being implemented by it.
• Explicitly implemented method can’t have any access modifier because it can be never called directly using the
reference variable of the class in which it is member.
• If we have a common method in more than one parent interface, different implementations can be provided in the
implementating class by explicitly implementing the interface.
• In VB.NET if an implemented interface member is declared as private then it can treated as equivalent of explicit
implementation of interface member in C#. This is because such members cannot be directly called on a using a
reference variable of a class in which it is a member.

Types of Inheritance:
1. Implementation Inheritance: A class is parent of another class and the methods implemented in parent class can be
used in the child class.
2. Interface Inheritance: An interface is parent of another interface or class. The Child only inherits the Prototype of
the method but will have to provide implementation to the method i.e. implementation is not inherited.
3.

class CA : CA1, CA2 //Invalid - .Net doesn’t support Multiple Implementation Inheritance.
{}
.Net doesn’t support Multiple Implementation Inheritance because if the same method is present in both the parent
classes and not overridden in the child class, it would be ambigious to call the method on the variable of child class.
For Example: If we have a method Foo in both CA1 and CA2 and if not overriden in the class CA then following would be
ambigious.
CA a = new CA();
a.Foo() //Ambigious call as both CA1 and CA2 qualify for execution.

class CA : IA1, IA2 //Valid - .Net supports Multile Interface Inheritance.


{}
In this case we have to compulsory implement in the methods of Interface in the class and thus we don’t have any ambiguity.

interface IA : IA1, IA2 //An interface in inherit from more than one interface.
{} //In this case the class implementing IA must implement members of both IA1 and IA2 also.

We can write a class which inherits from another class and also implements an interface.
In C#: In VB:
Class CA : CA1, IA1, IA Class CA
Inherits CA
Note: In sequence parent class should come before interface Implements IA1, IA2

3
Deccansoft Software Services Exception Handling

• In .Net, every exception is an object of type System.Exception or any of its sub class.
• Whenever the integrity of an object is violated, exception is thrown.
• Exception is a Runtime Error and the reason could be either because of Logical Error or S/W error or Limitations of
Hardware
• Exception handling is done by providing try … catch blocks.
• A try block can have any number of catch blocks but only one catch block is executed if an exception occurs i.e. the first
matching catch block is executed.
• If an exception is thrown and is unhandled in an inner block then it is automatically thrown to its outer block.
• If the exception occurs in Main function and is unhandled then CLR handles it and the program is terminated.
• A catch block of parent class must be always after the catch block of child class.
• For one try we can have multiple catch blocks and each catch block can be used as an exception handler for a particular type
of exception
• A variable declared in try block cannot be accessed in catch or finally block. i.e. try block ends with the starting of catch
block.
VB: C#:
static void Main(string[] args)
Sub Main(ByVal args As String()) {
Try try
Dim n1, n2 As Integer {
n1 = CInt(args(0)) int n1, n2;
n2 = CInt(args(1)) n1 = Convert.ToInt32(args[0]);
Dim res As Integer n2 = Convert.ToInt32(args[1]);
res = n1 \ n2 int res;
Console.WriteLine("Result" & res) res = n1 / n2;
Catch ex As IndexOutOfRangeException Console.WriteLine("Result:" + res);
Console.WriteLine("IOR: " & ex.Message) }
Catch ex As InvalidCastException catch (IndexOutOfRangeException ex)
Console.WriteLine("ICE: " & ex.Message) {
Catch ex As Exception Console.WriteLine("IOR: " + ex.Message);
Console.WriteLine(ex.Message) }
End Try catch(InvalidCastException ex)
End Sub {
Console.WriteLine("ICE: " + ex.Message);
Run the above program with }
1. No command line args catch (Exception ex)
2. a and b as command line args {
3. 10 and 2 command line args Console.WriteLine(ex.Message);
4. 10 and 0 command line args. }
}
Example:
Shared Sub Main() Shared Sub Foo() Output:
Console.WriteLine(“s1”) Console. WriteLine (“f1”) Exception Statements Executed
Try Try f1 Æ s4,s5
Console. WriteLine (“s2”) Console. WriteLine (“f2”) f2 Æ f6, f7, s3, s5
Foo() Try f3 Æ f4, f5, f7, s3, s5
Console. WriteLine (“s3”) Console. WriteLine (“f3”) f3, f4 Æ f6, f7, s3, s5
Catch Catch f5 Æ f6, f7, s3, s5
Console. WriteLine (“s4”) Console. WriteLine (“f4”) f5, f6 Æ s4, s5
End Try End Try
Console. WriteLine (“s5”) Console. WriteLine (“f5”)
End Sub Catch
Console. WriteLine (“f6”)
End Try
Console. WriteLine (“f7”)
End Sub
To test the above program replace Console.WriteLine with Throw New Exception

1
Deccansoft Software Services Exception Handling

Syntax to throw an exception, try


In VB: Throw New ApplicationException(“message”) { …}
In CS: throw new ApplicationException(“message”); catch(Exception e)
{
Only objects of type Exception or any of its subclass can be throw; //Throws the exception which has been caught
thrown. }

Finally Block
Once the code has entered the try Block and even if a condition is executed and has to return a value, it would still execute the
"Finally" block and then goes to the caller block.
Try
fs.Open ()
.....
Catch
.....
Finally
fs.Close ()
End Try
• If the code execution enters finally then is 100% guaranteed that it would also execute finally block.
• In “finally” block, you cannot use GoTo or Return statements.
• There can be only one “finally” block per Try block and it must be the last block after all catch blocks if present.

User-defined Exception class


• Every user defined exception class inherits from System.Exception or any of its subclass.
• Mostly the exception class has readonly properties and they are initialized in the constructor of the class.
Class DemoException class DemoException : ApplicationException
Inherits ApplicationException {
Public ReadOnly Number As Integer public readonly int Number
Public Sub New(ByVal message As String, public DemoException(string message, int
ByVal Number As Integer) number) : base(message)
MyBase.New(message) {
Me.Number = Number this.Number = Number;
End Sub }
End Class }

Note: ReadOnly field members of a class can be initialized only in the constructor of the class.

Important Properties of Exception Properties:


Message Æ Gets a message that describes the current exception.
StackTraceÆ Gets a string representation of the frames on the call stack at the time the current exception was thrown.
InnerException Æ Gets the System.Exception instance that caused the exception. This is used for exception chaining.

Using Statement Example


using (SqlConnection con = new SqlConnection ()) SqlConnection con;
{ try
con.Open (); {
} con = new SqlConnection();
When the using block ends it automatically calls the Close or con.Open();
Dispose method of the connection object }
finally
{
if (con.State = ConnectionState.Open)
con.Close();
}

2
Deccansoft Software Services – MS.NET Collections & Generics
The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit
arrays, hash tables and dictionaries.
A collection is an object, which would manage other objects i.e., ArrayList, HashTable, Stack, Queue, Linked Lists, Sorted List etc.
IEnumerable

ICollection

IList IDictionary

ArrayList (Index-Value) HashTable (Key-Value)

ArrayList:
• Implements the IList interface using an array whose size is dynamically increased as required.
• The capacity of a ArrayList is the number of elements the ArrayList can hold. The default initial capacity for an ArrayList is 0. As
elements are added to a ArrayList, the capacity is automatically increased as required through reallocation with the default
capacity on adding first item set to 4. The capacity can be decreased by calling TrimToSize or by setting the Capacity property
explicitly.
• Elements in this collection can be accessed using an integer index.
• ArrayList accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.
• If we look for information based on index, then we use ArrayList. For e.g. get the 5th student who has enrolled in this batch.
HashTable:
• Represents a collection of key/value pairs that are organized based on the hash code of the key.
• Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual Basic),
but a value can be.
• The capacity of a Hashtable is the number of elements the Hashtable can hold. The default initial capacity for a Hashtable is 0. As
elements are added to a Hashtable, the capacity is automatically increased as required through reallocation with the default
capacity on adding first item set to 4.
• In HashTable Key must be unique but value need not be unique.

‘Demo of ArrayList ‘Demo of HashTable


Dim al As New ArrayList() Dim ht As New Hashtable()
Console.WriteLine(al.Capacity) ht.Add(101, "Student1")
al.Add("s1") ht.Add(102, "Student2")
al.Add("s2") ht.Add(103, "Student3")
al.Add("s3") ht.Add(104, "Student4")
al.Insert(2, "s1.5") ht.Remove(102)
al.Remove("s2") For Each de As DictionaryEntry In ht
al.RemoveAt(1) Dim key As Object = de.Key
Console.WriteLine(al.Count()) Dim value As Object = de.Value
Console.WriteLine(al.Capacity) Console.WriteLine(key & " " & value)
Console.WriteLine(al.Contains("s1")) Next
For Each str As String In al Console.WriteLine(ht.Count)
Console.WriteLine(str) Console.WriteLine(ht.ContainsKey(104))
Next Console.WriteLine(ht.ContainsValue(“Student5”))
Dim s as String Dim s as String
s = CType(al(1),String) ' or CStr(al(1)) s = CType(ht(101),String) ' or CStr(ht(101))
Console.WriteLine(“Item with index 1 : “ & s) Console.WriteLine(“Item with key 101: “ & s)

Note: Both ArrayList and HashTable collection objects are not type safe because any type of object can be added to them as the data
type of value is object.
All arrays in .Net are inherited from System.Array.

1
Deccansoft Software Services – MS.NET Collections & Generics
Example for Sorting Custom Objects in ArrayList
Class Demo Class Program
Implements IComparable Public Shared Sub Main()
Public N As Integer Dim alDemo As New ArrayList()
Public Sub New(ByVal n As Integer) alDemo.Add(New Demo(5))
Me.N = n alDemo.Add(New Demo(50))
End Sub alDemo.Add(New Demo(15))
Public Function CompareTo(ByVal obj As Object) As Integer alDemo.Add(New Demo(9))
Implements IComparable.CompareTo Console.WriteLine(“Before Sorting”)
Dim d As Demo = CType(obj, Demo) For Each d As Demo In alDemo
If (Me.N < d.N) Then Console.Write(d.N.ToString() & " ")
Return -1 ‘the current object comes before the parameter object Next
ElseIf (Me.N > d.N) Then alDemo.Sort() ‘Here the Demo object must
Return 1 ‘the current object comes after the parameter object ‘ implement IComparable
Else Console.WriteLine("Sorted:")
Return 0 Console.WriteLine(“After Sorting”)
End If For Each d As Demo In alDemo
End Function Console.Write(d.ToString() & " ")
‘ToString is the method in object class and the default implementaiton Next
return the class name. We can override and return any other value. ‘For converting elements in the array list to array.
Public Overrides Function ToString() as String Dim arDemo() as Demo
Return N.ToString() Dim ar as Array = alDemo.ToArray(GetType(Demo))
End Function arDemo = CType(ar,Demo())
End Class 'To Add an array/collection to ArrayList
Dim alNew As New ArrayList()
• ArrayList collection can sort only those items in collection whose alNew.AddRange(arDemo)
class has implemented the interface “IComparable” ‘Using enumerator which is internally used by for each
statement also.
IEnumerable Dim en as IEnemerator
GetEnumerator as IEnumerator en = alDemo.GetEnumerator( )
While(en.MoveNext())
IEnumerator Dim d as Demo = CType(en.Current,Demo)
MoveNext() as Boolean Console.Write(d.ToString() & “ “)
Reset() End While
Current as Object ‘To Get a particular item reference from the collection
based on its index.
The Statement ForEach can be used only on those objects which have Dim d1 as Demo = CType(alDemo(2), Demo)
implemented the interface IEnumerable. Console.WriteLine(“Item with index 2 : “ & d1)
End Sub
End Class

Generics
Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic counterparts cannot.
Generic collection classes are like Templates in C++.
Advantages of Generics
• Type Safety -- Generic types enforce type compliance at compile-time, and not run-time (as in the case of using Object).
This reduces the chances of data-type conflict during run-time.
• Performance -- The data types to be used in a Generic class are determined at compile-time, hence there is no need to
perform type casting during run-time, which is a computationally costly process.
• Code reuse -- Since you only need to write the class once and customize it to use with the various data types, there is a
substantial amount of code-reuse.
Using Constraints in a Generic Type
You can further impose constraints on a Generic type. For example, if I want my Stack class to only manipulate objects of a certain
type, say of type Account, I can declare my Generic type as:
In VB: Class Stack(Of T As Account) In CS: class Stack<T> where T : Account
You can also specify multiple constraints in a Generic type. I want the Stack class to manipulate objects of type Employee and also
implement the ICompatable interface, I can declare my Generic type as:
In VB: Class Stack(Of T As {Account, IComparable}) In CS: class Stack<T> where T : Account, IComparable
2
Deccansoft Software Services – MS.NET Collections & Generics
Stack Example:
Non – Generic Stack Class Generic Stack Class:
public class Stack public class Stack<T>
{ {
object[] data; T[] data;
int current; int current;
public Stack(int size) public Stack(int size)
{ {
data = new object[size]; data=new T[size];
} }
public void Push(object value) public void Push (T value)
{ {
data[current] = value; data[current]=value;
current++; current++;
} }
public object Pop() public T Pop()
{ {
current--; current--;
return data[current]; return data[current];
} }
public object GetTopElement() public T GetTopElement()
{ {
return data[current - 1]; return data[current -1];
} }
public void Print() public void Print()
{ {
for (int i = 0; i < current; i++) for(int i =0; i<current;i++)
Console.WriteLine Console.WriteLine
(data[i].ToString()); (data[i].ToString());
} }
} }
//Would allow all types of data to be added to collection //Only objects of a particular type can be added to the
class Program collection
{ class Program
static void Main(string[] args) {
{ static void Main(string[] args)
Stack s=new Stack(5); {
s.Push (5); Stack<int> s=new Stack<int>(5);
s.Push (“Demo”); s.Push (2);
s.Push (new Computer()); s.Push (5);
s.Push (new Account()); s.Push (9);
s.Push (12); s.Push (22);
s.Print (); s.Push (12);
Console .WriteLine ("Popped: " + s.Pop ()); s.Push(“Demo”); //Compilation Error.
s.Print(); s.Print ();
} Console .WriteLine ("Popped: " + s.Pop ());
} s.Print();
}
}
Syntax for writing Generic Class in VB In VB to create an instance of Generic Class.
Class Complex (Of TReal, TImag) Dim c as New Complex(of Integer,Integer)
Public Real as TReal
Public Imaginary as TImag
End Class

A Generic class is not specific to any particular DataType. But the instance of such a class would be specific to a DataType mentioned
while creating the instance that class.

3
Deccansoft Software Services – MS.NET Collections & Generics
Generic Collection Classes
Important generic collection classes in System.Collections.Generic are List, Dictionary, Stack, Queue, LinkedList, and SortedList.

Examples:
Dim al As New List(of String)() Dim dic As New Dictionary(of Integer,String) ()
Console.WriteLine(al.Capacity) dic.Add(101 "Student1")
al.Add("s1") dic.Add(102, "Student2")
al.Add("s2") dic.Add(103, "Student3")
al.Add("s3") dic.Add(104, "Student4")
al.Insert(2, "s1.5") dic.Remove(102)
al.Remove("s2") For Each kv As KeyValuePair(Of Integer, String) In dic
al.Remove("s1.5") Dim key As Integer = kv.Key
Console.WriteLine(al.Count()) Dim value As String = kv.Value
Console.WriteLine(al.Capacity) Console.WriteLine(key & " " & value)
Console.WriteLine(al.Contains("s1")) Next
For Each s As String In al Console.WriteLine(dic.Count)
Console.WriteLine(s) Console.WriteLine(dic.ContainsKey(104))
Next Console.WriteLine(dic.ContainsValue(“Student5”))

Using Generic versions of IComparable to implement sorting in Generic Collection Objects.

using System.Collections.Generic class Program


class Demo : IComparable<Demo> {
{ static void Main(string[] args)
public int N; {
public Demo(int n) List<Demo> lst = new List<Demo>();
{ lst.Add(new Demo(2));
this.N = n; lst.Add(new Demo(1));
} lst.Add(new Demo(5));
public int CompareTo(Demo other) lst.Add(new Demo(4));
{ lst.Add(new Demo(6));
if (N < other.N) lst.Add(new Demo(3));
return -1; lst.Sort( );
else if (N > other.N) foreach (Demo d in lst)
return 1; Console.Write(d.N + " ");
else Console.WriteLine();
return 0; IEnumerator<Demo> en = lst.GetEnumerator();
} while (en.MoveNext())
} {
Demo d = en.Current;
Console.Write(d.N + " ");
}
}
}

System.Collections.Specialized namespace has a set of collection classes which are for specific type of items. These are not generic
collection classes.

4
Deccansoft Software Services – MS.NET Collections & Generics

Writing Custom Collection Class


IDictionary IList

DictionaryBase CollectionBase

AccountCollection AccountCollection

CollectionBase:
• Provides the abstract base class for a strongly typed collection.
• CollectionBase class implements IList and has implemented all the methods in IList.
• The implementation of methods which has object as parameter or return type is marked as private (in VB) and Explicit
Implitation (in CS) and is hidden from the inherited class. That is the reason this is still an abstract class even though it does
not have any abstract method.
• For writing custom collection class for a given type we have to write a class inherited from CollectionBase and add to it all
the methods for our type as parameter or return type. The CollectionBase has “List” which is declared as protected member.
We use this member of implementation of our methods in the derived class.

Public Class AccountCollection To the AccountForm add the controls btnAdd,


Inherits CollectionBase btnRemove and btnGetFromCollections and txtIndex:
Public Function Add(ByVal value As Account) As Integer
Return List.Add(value) Dim ac As New AccountCollection
End Function Private Sub btnAdd_Click(. . .)
Public Function Contains(ByVal value As Account) As Boolean ac.Add(a)
Return List.Contains(value) End Sub
End Function Private Sub btnRemove_Click(. . .)
Public Function IndexOf(ByVal value As Account) As Integer If (ac.Contains(a)) Then
Return List.IndexOf(value) ac.Remove(a)
End Function End If
Public Sub Insert(ByVal index As Integer, ByVal value End Sub
As Account) Private Sub btnGetFromCollection_Click(. . .)
List.Insert(index, value) a = ac(CInt(txtIndex.Text))
End Sub btnGet_Click(Nothing, Nothing)
Default Public Property Item(ByVal index As Integer) As Account End Sub
Get Private Sub btnCount_Click(. . .)
Return CType(List.Item(index), Account) MsgBox (ac.Count)
End Get End Sub
Set(ByVal value As Account)
List.Item(index) = value
End Set
End Property
Public Sub Remove(ByVal value As Account)
List.Remove(value)
End Sub
End Class
Easy Steps for writing a Collection Class for a specific Class:
1. Write a class and say it implements ‘IList’. Generate all the using studio shortcut.
2. Delete all the members of the IList which doesn’t have either parameter or return type as Object. List to methods to delete:
CopyTo, Count, IsSynchronized, SyncRoot, GetEnumerator, Clear, IsFixedSize, IsReadOnly and RemoveAt.
3. In the remaining methods and properties replace all occurrence of object in parameter or return type with the type of Class for
which we are writing our custom collection class. For example replace object with Account..
4. Replace the interface “IList” with class “CollectionBase” as the parent class.
5. Implement all the members using the protected member “List” of the parent class (as given in the above example).

Note: If we want to write a custom collection class which is similar to HashTable, then that class should inherit from DictionaryBase
and the other methods can be written in the custom collection class using the Protected member of the DictionaryBase i.e.
“Dictionary”.

5
Deccansoft Software Services – MS.NET Unsafe Code, Operator Overloading, Reflection,Attributes & Config file

UnsafeCode and Pointers


To maintain type safety and security, by default C# does not support pointer arithmetic. However, by using the unsafe keyword, it
is possible to define an unsafe context in which pointers can be used.

In the common language runtime (CLR), unsafe code is referred to as unverifiable code. Unsafe code in C# is not necessarily
dangerous; it is simply code whose safety cannot be verified by the CLR. The CLR will therefore only execute unsafe code if it is
within a fully trusted assembly. If you use unsafe code, it is your responsibility to ensure that your code does not introduce
security risks or pointer errors.

You can use the unsafe modifier in the declaration of a type or a member. The scope of the unsafe context extends from the
parameter list to the end of the method, so pointers can also be used in the parameter list
Example:
public struct Test • Unsafe code is not unmanaged code.
{ Note: Unmanaged code is code of COM component or a Win32
public int a,b; library function invoked from a .net application.
public Test(int a)
{ • A structure with reference type as its member is treated as
this.a = a; managed type and pointer to such a structure is not allowed.
b = 0;
} • Test t; or Test t = new Test() are Same
} Here “t” is a structure and holds the values, it’s not a ref to
unsafe public class UnsafeDemo the structure.
{
static void Square(int *pn) • To compile unsafe code:
{ ProjectÆPropertiesÆbuildÆ Allow Unsafe code
*pn = *pn * *pn;
}
• When the code being executed is in the fixed block and if the
static unsafe void ShowTest(Test *pT)
GC is activated, the address of the array in the heap memory
{
will not change.
Console.WriteLine(pT->a);
}
static unsafe void ShowArray(int *ar,int len)
{
for(int i=0; i <= len; i++) ---It doesn’t give exception even if the index is not valid as it is not verified.
Console.WriteLine(ar[i]); // Console.WriteLine(*(ar+i) );
}
public static void Main()
{
int m = 10;
int []ar={1,2,3,4,5};
unsafe
{
Square(&m);
Console.WriteLine(m);
Test t = new Test(10);
ShowTest(&t);
fixed(int *pA=ar)
{
ShowArray(pA,ar.Length);
}
}
}
}
Operator Overloading Examples
All operator overloaded methods are static methods of the class. Also be aware that if you overload the equality (==) operator, you
must overload the inequality operator (!=) as well. The < and > operators, and the <= and >= operators should also be overloaded
in pairs.
The complete list of operators that can be overloaded is:
• Unary operators: +, -, !, ~, ++, --, true, false
• Binary operators: +, -, *, /, %, &, |, ^, <<, >>, ==, !=, >, <, >=, <=

1
Deccansoft Software Services – MS.NET Unsafe Code, Operator Overloading, Reflection,Attributes & Config file

using System;
Option Strict On using System.Collections.Generic;
Public Class Point using System.Text;
Public X, Y As Integer public class Point
Public Sub New() {
End Sub public int X, Y;
Public Sub New(ByVal a As Integer, ByVal b As Integer) public Point()
X=a {}
Y=b public Point(int a, int b)
End Sub {
Public Shared Operator +(ByVal p1 As Point, ByVal p2 As X = a;
Point) As Point Y = b;
Dim p As Point = New Point() }
p.X = p1.X + p2.X public static Point operator +(Point p1, Point p2)
p.Y = p1.Y + p2.Y {
Return p Point p = new Point();
End Operator p.X = p1.X + p2.X;
Public Shared Widening Operator CType(ByVal p As Point) p.Y = p1.Y + p2.Y;
As String return p;
Return p.X & ", " & p.Y }
End Operator public static implicit operator string(Point p)
Public Shared Narrowing Operator CType(ByVal s As String) {
As Point return p.X + ", " + p.Y;
Dim p As New Point() }
p.X = Integer.Parse(s.Split(","c)(0)) public static explicit operator Point(string s)
p.Y = Integer.Parse(s.Split(","c)(1)) {
Return p Point p = new Point();
End Operator p.X = int.Parse(s.Split(',')[0]);
p.Y = int.Parse(s.Split(',')[1]);
Public Shared Sub Main() return p;
Dim p1 As Point = New Point(1, 2) }
Dim p2 As Point = New Point(3, 4) public static void Main()
Dim p3 As Point = p1 + p2 {
Dim s As String = p3 'Implicit Casting from point to string Point p1 = new Point(1, 2);
Console.WriteLine(s) Point p2 = new Point(3, 4);
Dim p As Point = CType("10,2", Point) ‘Explicit Casting Point p3 = p1 + p2;
Console.WriteLine(p.X & " " & p.Y) string s = p3; //Implicit Casting from point to
End Sub string
End Class Console.WriteLine(s);
• The name of method must be “operator” followed by the operator Point p = (Point)"10,2"; //Explicit Casting
being overloaded. Console.WriteLine(p.X + " " + p.Y);
• The method must be shared (vb) / static (cs) }
• The operands must be parameters of the methods. }

Attributes
Attributes are keyword-like tags in which you can specify additional information about entities defined in .Net applications.
Attributes, which are saved with an assembly's metadata, annotate programming elements such as types, fields, methods, and
properties.

An attribute is an additional piece of information associated with a type or its members. This information can be used either by
the IDE or language, compiler or the runtime. E.g.: Default values of the properties.

Syntax for attaching an Attribute to a class or any of its member:

<AttName ( )> Class CA <AttName (“p1”, “p2”)> Class CA <AttName (P1: = “p1”, P2: = “p2”)> Class CA
….. ….. …..
End Class End Class End Class

2
Deccansoft Software Services – MS.NET Unsafe Code, Operator Overloading, Reflection,Attributes & Config file

Reflection
The classes in the System.Reflection namespace, together with System.Type, allow you to obtain information about loaded
assemblies and the types defined within them, such as classes, interfaces, and value types. You can also use reflection to create
type instances at run time, and to invoke and access them.

Using reflection API, we can find all the details of any type i.e. we can retrieve information like the methods, the parameters of
the method, return type of method, properties, property datatypes etc. This information is actually fetched from the Metadata of
the assembly.

For a given class we would have one instance of type “Type” managed by CLR, irrespective of the number of instances of the
class created.

using System.Reflection
//To get the list of properties of the class Test //To get the list of methods of the class Test
Type tp; Type tp;
tp = typeof(Test); tp = typeof(Test);
string names = " "; MethodInfo []arMI;
foreach (PropertyInfo pi in tp.GetProperties()) arMI = tp.GetMethods();
names += pi.Name + "\n"; string str = "";
Console.WriteLine(names); foreach (MethodInfo mi in arMI)
str += mi.Name + " " + mi.GetParameters().Length;
Console.WriteLine(str);
Object ob = Activator.CreateInstance(tp);
string methodName = "Foo";
MethodInfo miFoo = tp.GetMethod(methodName);
object[] arArgs = { 2, "B" };
miFoo.Invoke(ob, arArgs);

//In VB.NET equivalent of typeof is GetType


Dim tp as System.Type = GetType(Test)

To create an instance of the class without using new operator


Test t = (Test) Activator.CreateInstance(tp) //or new Test()

Configuration File: (App.Config)


It is an XML document containing all the configuration information for that application when a project in studio is built along
with the output .exe or .dll.

In Studio Project, add a file by name App.Config.


<configuration>
<appSettings>
<add key=”k1” value = “V1”>
<add key=”k2” value = “V2”>
<add key=”k3” value = “V3”>
</appSettings>
</configuration>
• RightClick on project -> Add References -> System.Configuration.dll

Private Sub btn_Click(…)


MsgBox(System.Configuration.ConfigurationManager.AppSetting(“k1”))
End Sub

When the project is compiled using VS.NET the content of App.Config file is copied into another file by name
<ApplicationName>.exe.config (or) <ApplicationName>.dll.config and its also placed in the same folder where the
<ApplicationName>.exe is generated.

Important Notes:
• Parent of all configuration files on the machine is Machine.Config. The Configuration settings present in Machine.Config
are global to all .net applications running on that machine. Its Present in the folder: <Framework
Folder>\config\Machine.config.
• Individual applications app.config file if required can override the settings in the machine.config.

3
Deccansoft Software Services – MS.NET Unsafe Code, Operator Overloading, Reflection,Attributes & Config file

Location of Framework Folder: C:\Windows\Microsoft.Net\Framework\V2.0.X.X\

4
Deccansoft Software Services – MS.NET Unsafe Code, Operator Overloading, Reflection,Attributes & Config file

DON’T XEROX THIS PAGE


Custom Attributes:
Custom attributes are user-defined attributes that provide additional information about program elements..

<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Property)> _
Public Class AuthorAttribute
Inherits System.Attribute
Public ReadOnly Name As String
Public Location As String
Public Sub New(ByVal name As String, Optional ByVal location As String = "")
Me.Name = name
Me.Location = location
End Sub
End Class

Syntax for attaching an Attribute to a class or any of its member.


<Author ( )> Class Account
…..
End Class
or
<Author (“Soni”, “Hyderabad”)> Class Account
…..
End Class
or
<Author (Name: = “Soni”, Location: = “Hyderabad”)> Class Account
…..
End Class

In Form
Dim tp As Type = GetType(Account)
Dim ob ( ) As Object
ob = tp.GetCustomAttributes (true)
If (TypeOf(ob(0) is AuthorAttribute)
Dim aa as AuthorAttribute = DirectCast(ob(0), AuthorAttribute)
MsgBox(aa.Name & “ “ & aa.Location)
End If

Partial Classes
It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a
section of the class definition, and all parts are combined when the application is compiled. There are several situations when
splitting a class definition is desirable:
• When working on large projects, spreading a class over separate files allows multiple programmers to work on it
simultaneously.
• When working with automatically generated source, code can be added to the class without having to recreate the source file.
Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code
that uses these classes without having to edit the file created by Visual Studio.
To split a class definition, use the partial keyword modifier.

5
Deccansoft Software Services – MS.NET Working with Assemblies and GAC

1. Create a new Class Library


1. Click File menu and select Project from New.
2. From New Project select Visual Basic-Windows from Project Types. From Templates select Class Library and
check the Checkbox of Create directory for solution. Write the ClassLibrary1 in the Name and write the project location
in Location i.e. D:\DemoSolution. Write the Solution Name is AssembliesWalkthrough.Click OK.
3. Write the code in the Class of Class1.vb.
Public Class Class1
Public Function SayHello(ByVal name As String) As String
Return "Hello" & " " & name & " " & "1.0"
End Function
End Class
4. Build the ClassLibrary1 ->Select Build Solution from Build menu.
2. Adding a new Windows Application to the ClassLibrary
1. Go to Solution Explorer, right click on that and Select New Project from Add.
2. Select Windows from Project Types and select Windows Application from Templates and click OK.
3. Right click on WindowsApplication1 and select the Add Reference.
4. We get Add Reference dialogBox click on Projects and select Class Library1 from Project Name. Click OK.
Note: The Projects Tab is used for adding reference to other projects in the same Solution.
5. Click View menu and click on Tool Box.
6. You will get a Tool Box on Left hand side of the form. Double Click on Button from Common Controls.
Imports ClassLibrary1
Public Class Form1
Private Sub Button1_Click(……) Handles Button1.Click
‘Create an object of ClassLibrary
Dim c As new Class1
‘calling the function of ClassLibrary1.
MsgBox(c.SayHello("Version"))
End Sub
End Class
7. Right Click on Windows Application1 and select Set as StartUp Project option.
8. Now run the project with Ctrl+F5. We get Form1 with Button1.
**We can see the included classlibrary1.dll file in Windows Application1 through D drive in this way
D:\DemoSolution\AssembliesWalkthrough\WindowsApplication1\bin\Debug path and see the dll of the Class Library.
3. Providing Strong Name to the Class Library
1. Select Solution Explorer from View menu.
2. Right Click on ClassLibrary1 and Select the Properties.
3. Click on Signing and Check the Sign the assembly Checkbox. Select <New…> for creating
a strong name key file and Click on it.
4. You get one dialogbox with the name of Create a strong Name Key. If you want to provide the password check the
Checkbox of Protect my key file with a password. Other wise do uncheck. Enter the Strong Name Key in key file name.
Click OK.
Note: VS.NET uses a tool called as SN.EXE for generating the strong name key file.
5. xyz.snk will be added in the ClassLibrary1 project.
4. Providing Version to the Class Library (Objservation)
1. Click on Application Tab and Click on Assembly Information button.
2. Assembly Information dialogbox will open. Default Version for Assembly Version will
be 1 0 0 0, keep as it is and ensure None is selected in Neutral Language. Click OK.
5 Deploying the assembly in GAC through Visual Studio 2005 Command Prompt
(A) 1. Open Visual Studio 2005 Command Prompt from Visual Studio
2. Goto the Debug folder of the Project and run the following command
GacUtil -i ClassLibrary1.dll.
**We can also uninstall an Assembly by typing GacUtil –u ClassLibrary1
**We can list the existing list by typing GacUtil –l
5 Another way to add into the Cache
(B) 1. Click on Start ===> Select Control panel from Settings Î Click-Administrative Tools===>Click-Microsoft .NET

1
Deccansoft Software Services – MS.NET Working with Assemblies and GAC

Framework 2.0 Configuration.


2. In the new Window Click Assembly Cache from My Computer and click on Add an Assembly to the Assembly
Cache.
3. You will get Add an Assembly dialogbox. In that select our ClassLibrary1.dll from ClassLibrary1 folder and click
Open. Then the assembly will be added to the GAC.
6 Goto References of WindowsApplication1 Æ Select ClassLibrary1 Æ Properties Æ LocalCopy= False

.
• The same signing key file can be used for signing of all the assemblies developed by a given developer or organization.

Walkthrough for Assemblies (Case -2)


Continue of Case -1 (For Side- by- Side Versioning)

1. Copy the dll of ClassLibrary1 from the bin folder of our project and go to
C:\Program Files\Common Files\CompanyName\ClassLibrary1\Ver 1.0\ and paste the dll and from here add the dll
into the GAC.
2. Modify the implementation in Class1 of ClassLibrary1 project.
Public Class Class1
Public Function SayHello(ByVal name As String) As String
Return "Hello " & name & " - 2.0"
End Function
End Class
3. Follow the steps of Providing Version to the Class Library from Case Study-1:Change the Assembly Version and the
File Version to 2.0.0.0 instead of 1.0.0.0
4. Rebuild the ClassLibrary1 and copy the dll of ClassLibrary1 from the bin folder of our project to
C:\Program Files\Common Files\CompanyName\ClassLibrary1\Ver 2.0\ and paste the dll and from here add the dll
into the GAC.
Note: at this time we have two versions of the same ClassLibrary present in GAC.
5. In WindowsApplication1 remove the old reference of ClassLibrary1 and add the new reference of ClassLibrary1 –
Version 1.0.0.0 (select the Dll present in “C:\Program Files\Common Files\CompanyName\ClassLibrary\Ver 1.0”)
folder
Note: Run the WindowsApplication1.exe and make sure that it’s using Version 1 of the DLL because it is build (compiled)
using Version 1 of the ClassLibrary.
6. Add to the existing solution, a new windows application (WindowsApplication2) and add reference to ClassLibrary1 -
Version 2.0.0.0 (select the Dll present in “C:\Program Files\Common Files\CompanyName\ClassLibrary\Ver 2.0”)
folder.
Note: Confirm that in both the Windows Applications ClassLibrary1 reference property Local Copy is set to false.
7. Add button1 to the Form and write the click event handler for button1 in WindowsApplication2.
Imports ClassLibrary1
Public Class Form1
Private Sub Button1_Click(……) Handles Button2.Click
‘Create an object of ClassLibrary
Dim c As new Class1
‘calling the function of ClassLibrary1.
MsgBox(c.SayHello("Version"))
End Sub
End Class
8. Right Click on WindowsApplication1 and select Set as StartUp Project option.
9. Now run the project with Ctrl+F5. We get Form1 with Button1.
10. Right Click on WindowsApplication2 and select Set as StartUp Project option and run the project with Ctrl+F5.

2
Deccansoft Software Services – MS.NET Working with Assemblies and GAC

This is called Side-by-Side Versioning and is not supported in COM.


Walkthrough for Assemblies (Case Study-3)
Continue of Case Study-2 (For Upgrading the Versions)
1.
1. Click on Start from the Desktop ==>Go to Settings => Control panel => Administrative Tools =>Microsoft .Net
FrameWork 2.0 Configuration ===>Click on My Computer => Configured Assemblies
2. Click on Configure an Assembly. You will get Configure an Assembly dialogbox.
3. Click on Choose Assembly button. Choose the Library which you want to upgrade i.e ClassLibrary1 with Version
1.0.0.0.
4. After that click on Finish button.
5. You will get a dialogbox of ClassLibrary1 Properties. Click on Binding Policy button in that write the Requested
Version 1.0.0.0 which we want to replace to New Version 2.0.0.0.
Click on Apply button and then OK button.
Note: Here you can run the WindowsApplication1.exe by directly going its bin folder and verify that its using
ClassLibrary1.DLL – Version 2.0.0.0 and not Version 1.0.0.0 using which it originally build / compiled
Explanation:
6. Go to this path through C drive C:\WINNT\Microsoft.NET\Framework\v2.0.50727\CONFIG and click on
machine.config file and open it.
7. Copy this code from machine.config file and paste it where we want the new version in this way.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ClassLibrary1" publicKeyToken="33eca4cd7b0f83c0" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Note: Because the above xml is generated in machine.config, all the applications build using Version 1.0.0.0 of
ClassLibrary1.DLL at runtime use Version 2.0.0.0.

In case only if one application has to use Version 2.0.0.0, the above xml must be placed in that applications config file and
not in machine.config.

8. Right Click on WindowsApplication1 and select New Item form Add. We get Add New Item dialog box will be
display.from that select Application Configuration File and click on Add.
9. Paste the content copied from “machine.config” file here in the “app.config” file of windows Application1 in between
<configuration> tag.
10. Right Click on Windows Application1 and select Set as StartUp Project option.
11. Now run the project with Ctrl+F5..

In this way the version will be upgraded from old version to new version. When we run the WindowsApplication1 in
which had Classlibrary1.dll is there with Version 1.0.0.0 this one will be upgraded with new Version 2.0.0.0 and shows the
information of Version 2.0.0.0.
Notes
• A ClassLibrary is a Dll file whose code can be reused in many other application. The code in Exe file cannot be reused.
• A namespace is a logical collection of classes and other types with unique names. In a given namespace all the types have
unique names and thus it is used to resolve the ambiguity in case of name conflict of different type.
• Any type when used outside the namespace, it must be qualified by its namespace, but when used by another type with in the
same namespace then it need not be qualified by the namespace.
• In VB.Net project properties we can mention a root namespace and that becomes the default Namespace for all classes &
other types in the project. Where as in C#, in project properties we can mention the default namespace but in code we have
to explicitly mention the namespace. The editor auto generates the code using default namespace.
• Namespace can never be used as a boundary for an access modifier / specifiers.
• A public method of a public class cannot have either as parameter or return any datatype which is not public.

3
Deccansoft Software Services – MS.NET Working with Assemblies and GAC

• In we can use Imports (VB) / using (CS) Namespace on top of the file so that we don’t have to qualify all the types of that
namespace in that file. In VB.NET we can do project level Imports but in CS we don’t have that facility.
• A Public Class is accessible within and outside the assembly. Where as an internal (C#) / Friend (VB) Class can be access
only by other types with in the assembly. This is the default access modifier for the class.
• Top level class can be declared as either public or internal (C#) / Friend (VB) but nested class (a class within class) can
have any access modifier.
• An internal (C#) / Friend (VB) member of a class is accessible to all other type with in the assembly and is not accessible
out side the assembly.
• A member declared as “protected internal” (CS) or “Protected Friend” (VB) is accessible to all the class with in the
assembly and only to the derived classes out side the assembly.
• A class which is accessible outside assembly but can’t be instantiated from outside the assembly is called “Public Not
Creatable “.To write such a class we have to make the constructor of the class as “internal” or friend.
In the example below class1 is Public Not Creatable.
public class Class1
{
internal Class1()
{}
}
public class Class2
{
public Class1 GetClass1Object()
{
return new Class1();
}
}
• Unlike COM component .Net Component need not be register in Win Registry.
• A top class cannot have any access modifier other than public or internal where as inner class can have any access modifier.

Types Of Assemblies
• Private Assemblies: An assembly whose local copy is maintained by each & every application referencing to it.
• Shared Assembly: An assembly whose single copy deployed in Global Assembly Cache (GAC) and used / shared by many
applications running on that machine. Such assemblies must have Strong name.
StrongName = Assembly Name +Version + Public / Private Key + Culture
• Satellite Assembly: A resource only assembly for a given culture is called as satellite assembly. They don’t have any code
but they have only resources like string tables and images.

Notes about Shared Assemblies:


• The referring application includes the strong name of the referred assembly (library) in its manifest which is auto generated
by the language compiler.
• When the application requires the library, using this strong name information the referred library is loaded. First it searches
the library in the local directory of the application but if not found it is loaded from GAC.
• A shared assembly must be first placed in any directory and from there it must be installed in GAC and from the same
location we can add the reference to the assembly in the application. We cannot add reference to the assembly directly from
GAC.
• C:\WINNT\assembly\GAC_MSIL\ClassLibrary1\1.0.0.0__9e435e4509ac3422> is the location where ClassLibrary1.DLL
will be present.

4
Deccansoft Software Services – MS.NET ADO.NET

Structured Query Language = DDL + DML + DCL + DTL

Data Defination Language = Create, Alter, Drop


Data Manipulation Language = Insert, Delete, Update, Select
Data Contro Language = Grank, Revoke
Data Transaction Language = Commit, Rollback, SavePoint

Client Process
Application

Managed DataSet
Provider

DataAdapter

Command Connection Data Reader

DB Server

Managed Provider
It is a .Net component implementing certain standard set of interfaces provided by Microsoft, for a specific type of backend.
Following are the four managed providers which are build in along with MS.NET Framework.
1. System.Data.SqlClient Manage Provider for SqlServer
2. System.Data.OracleClient- Manage Provider for Oracle
3. System.Data.Oledb -Manage Provider- Can be used for any database which has Oledb provider
4. System.Data.Odbc - Manage Provider for Odbc drivers – This can be used for all databases but it would be
slow compared to a managed provider specific to a given database.

Imporatant Objects in Managed Provider

Connection: Connection object encapsulates the functionaly of establishing a communication path over the sockets.
In .Net ,Connections are ‘Pooled’. We can make by default 100 simultaneous connections without closing but obviously we have
to close every connection Opened only then it can be reused from Pool i.e keeps the connection with the database open for the
short period of time. The Connection Pooling can be customized by setting appropriate parameters in ConnectionString of
Connection Object and this cannot be done for Oledb and ODBC managed providers.

Command: Command Object can be used for execution of any type of Sql Statement.

DataReader: When “Select” or similar type of sql statement is executed a new cursor (memory are in backend) is created on the
back end. This Cursor contains primary key or indexed key of the table on which the statement is executed for the records which
have qualified the condition in the where clause of the select statement. It also has a pointer, which is by default positioned at
BOF.
The cursor and pointers functionality is encapsulated by the “DataReader” object created in the front end. Using this object, we
can move the pointer in the forward direction getting one record from backend into the front-end at the time.
The cursor managed by Data Reader is read-only and forward-only

Goto ServerExplorer Æ Right Click on Data Connections Æ Create New Sql Server Database Æ Server Name as .\SqlExpress
Æ New Database name as MSNET
Create the following table in that database.
Emp (EmpId, EmpName, EmpSalary) table in backend.
EmpId – Int, PrimaryKey and also set Identity = True in Property of EmpId field
EmpName – Varchar(50) - AllowNull = False(Uncheck)
EmpSalary – Money
Right Click on EMP table and select Show Table Data Æ Enter some default data.
• Note: On top of file mention Imports System.Data.SqlClient
To Execute Insert / Delete / Update Statements
Private Sub btnInsert_Click(. . .) Handles btninsert.Click

1
Deccansoft Software Services – MS.NET ADO.NET

Dim con As New SqlConnection


con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=MSNET;Integrated Security=True"
‘con.ConnectionString = "Data Source=.\sqlexpress;Database=MSNET;User id=sa;Password=dss"
‘con.ConnectionString = "Server=.\sqlexpress;Database=MSNET;uid=sa;pwd=dss"
Dim cmd As New SqlCommand
cmd.CommandText = "insert into Emp(EmpName,EmpSalary)
values('" & txtName.Text.Replace("'", "''") & "'," & txtSalary.Text & ")"
Note: In and SQL Statement the value of every Varchar field must be enclosed in single quotes. But if the value itself has single
quote then it should be replaced with two single quotes.
'cmd.CommandText = "Delete from Emp where empid=" & txtId.Text
'cmd.CommandText = "Update Emp Set EmpName = ‘” & txtName.Text.Replace("'", "''") &
“’, EmpSalary=” & txtSalary.Text & “where EmpId=" & txtId.Text
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
MsgBox(cmd.ExecuteNonQuery()) ‘Used for execution of those Statements which do not return any data.
cmd.CommandText = "Select @@Identity" ‘Used for fetching the last set Identity column value on the current con.
MsgBox(“Last Inserted EmpId as set by backend “ + cmd.ExecuteScalar)
Finally
If (con.State = ConnectionState.Open) Then con.Close()
End Try
End Sub
Private Sub btnSelect_Click(. . .) Handles btnSelect.Click
Dim con As New SqlConnection
con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=MSNET1;Integrated Security=TRUE"
Dim cmd As New SqlCommand
'cmd.CommandText = "select empname from emp where empid=1"
cmd.CommandText = "select max(empid) from emp"
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
MsgBox(cmd.ExecuteScalar) ‘Used for execution of statements which return only one value.
con.Close()
End Sub

Private Sub btnGetEmp_Click(. . .) Handles btnGetemp.Click


Dim dr As SqlDataReader
dr = GetEmployees()
Dim str As String = ""
While (dr.Read()) 'Read return false if EOF is reached.
str &= dr("EmpId") & vbTab
str &= dr(1) & vbTab
Dim ind As Integer
ind = dr.GetOrdinal("EmpSalary") 'Return the Index of the column EmpSalary
If (dr.IsDBNull(ind)) Then
str &= "--" & vbCrLf
Else
str &= dr.GetDecimal(ind) & vbCrLf
End If
End While
MsgBox(str)
dr.Close() 'Here is the connection is Closed
'con.Close() 'This is not required if CommandBehavior.CloseConnection is mentioned when the statement is executed.
End Sub
Private Function GetEmployees() As SqlDataReader
Dim con As New SqlConnection()
con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=MSNETDemoDb;Integrated Security=TRUE"
Dim cmd As New SqlCommand
cmd.CommandText = "select EmpId,EmpName,EmpSalary from Emp"
cmd.CommandType = CommandType.Text
cmd.Connection = con

2
Deccansoft Software Services – MS.NET ADO.NET

Dim dr As SqlDataReader
con.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.SingleRow)
'If the CommandBehavior.CloseConnection is mentioned the Connection is automatically closed when the data reader is
closed.
Return dr
End Function
MARS
By Default we can have only one data reader opened at a time on one connection object.
If MultipleActiveResultSets=True is added to the connection string then more than one data reader can be opened at the same
time on the single connection object. It’s a new feature in Ado.net 2.0
Note: The result of one select statement should not overlap with the result of another statement.

Department(DeptId,DeptName)
Employee(EmpId,EmpName,EmpSalary,DeptId);
Note: One Department can have more than one Employee.

Private Sub btnGetDeptEmp_Click(. . .) Handles btnDeptGetEmp.Click


Dim con As New SqlConnection
con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=MSNETDemoDb;Integrated Security=TRUE;
MultipleActiveResultSets=True"
Dim cmdDept As New SqlCommand
cmdDept.CommandText = "select * from Department"
cmdDept.CommandType = CommandType.Text
cmdDept.Connection = con
Dim drDept As SqlDataReader
con.Open()
drDept = cmdDept.ExecuteReader()
Dim str As String = ""
While (drDept.Read())
Dim cmdEmp As New SqlCommand
cmdEmp.CommandText = "select * from Employee where Deptid=" & drDept("DeptId")
cmdEmp.CommandType = CommandType.Text
cmdEmp.Connection = con
Dim drEmp As SqlDataReader
drEmp = cmdEmp.ExecuteReader()
While (drEmp.Read())
str &= drDept("DeptName") & vbTab
str &= drEmp("EmpId") & vbTab
str &= drEmp(1) & vbTab
str &= drEmp.GetDecimal(2) & vbCrLf
End While
drEmp.Close()
End While
drDept.Close()
MsgBox(str)
con.Close()
End Sub
Prepared Statement : An sql Statement on its first execution is compiled, optimized and an execution plan is created for it, If
the statement is marked as prepared then for further usage this execution plan is catched by the backend so that the same plan
can be reused for the subsequent request for the same statement but different values for parameters.
Note: The prepared statement must be framed using the paramenters.

Note: In Sql Server the parameters must begin with @, In Oracle just the name is sufficient but for OleDb and Odbc Providers we
need to use “?” as place holder for all the parameters and while adding the parameters a proper order must be maintained.

Private Sub btnInsParam_Click(. . .) Handles btnInsParam.Click


Dim con As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=MSNETDemoDb;Integrated Security=TRUE")
Dim cmd As New SqlCommand
cmd.CommandText = "Insert into emp(EmpName,EmpSalary) values(@EmpName,@EmpSalary)"
cmd.CommandType = CommandType.Text
cmd.Connection = con

3
Deccansoft Software Services – MS.NET ADO.NET

'For every place holder in the sql statement a parameter object must be created and added to the parameters collection.
Dim parEmpName As New SqlParameter("@EmpName", SqlDbType.VarChar, 50)
Dim parEmpSalary As SqlParameter
parEmpSalary = cmd.CreateParameter()
parEmpSalary.ParameterName = "@EmpSalary"
parEmpSalary.SqlDbType = SqlDbType.Money
cmd.Parameters.Add(parEmpName)
cmd.Parameters.Add(parEmpSalary)

Dim dic As New Dictionary(Of String, Decimal)


dic.Add("DE1", 10000)
dic.Add("DE2", 2000)
dic.Add("DE3", 30000)
dic.Add("DE4", 40000)
'Set the value for the parameters.
con.Open()
cmd.Prepare() 'Prepares the execution plan for the sql statement.
For Each kv As KeyValuePair(Of String, Decimal) In dic
parEmpName.Value = kv.Key 'Get Name
parEmpSalary.Value = kv.Value ' Get Salary
cmd.ExecuteNonQuery()
Next
con.Close()
End Sub

Note: If the SQL Statement has to be executed only once, don’t mark is Prepared because the backend takes extra time for
preparation of execution plan and this would become overhead the plan is not reused.
Stored Procedures
It is a precompiled set of sql statement which are compiled in native form and stored in the backend. They are very fast on their
first execution also.
Advantages:
1. They are very fast in execution because they are precompiled and stored in backend in native form of that backend.
2. Reduces network trafic because they are executed in backend the data used by them is also in backend.
3. Its easy to update logic/code in them because its stored only at one place i.e in database.

Note:
As far as possible always try to execute all the sql statements using stored procedures.
Every Input Parameter must have a value before the command is executed. If required we can set the to DbNull.Value.
In SQL-Server every out parameter is also input.

Sample Stored Procedure


Create Procedure GetSalary(@Id int,@Sal money out) AS
Begin
Select @Sal=EmpSalary from Emp where EmpId=@Id
End
---------------------------------------------------------------------------------------
Private Sub btnSPDemo_Click(. . .) Handles btnSPDemo.Click
Dim con As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=MSNetDemoDb;Integrated Security=TRUE")
Dim cmd As New SqlCommand
cmd.CommandText = "GetSalary"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
Dim parId As New SqlParameter("@Id", SqlDbType.Int)
Dim parSal As New SqlParameter("@Sal", SqlDbType.Money)
parSal.Direction = ParameterDirection.Output
parSal.Value = DBNull.Value
cmd.Parameters.Add(parId)
cmd.Parameters.Add(parSal)
con.Open()
parId.Value = txtId.Text
cmd.ExecuteNonQuery()
MsgBox(parSal.Value)
4
Deccansoft Software Services – MS.NET ADO.NET

con.Close()
End Sub

Create Procedure GetEmployees AS


Begin
Select Count(*) from Emp
Select EmpId, EmpName, EmpSalary from Emp
End

Private Sub btnGetEmployeesStoredProcedure_Click(. . .) Handles btnInsParam.Click


Dim con As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=MSDemoDb;Integrated Security=TRUE")
Dim cmd As New SqlCommand
cmd.CommandText = "GetEmployees"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
con.Open()
dr = cmd.ExecuteReader()
dr.Read() ' Moves to the only record in the first cursor created by execution of the stored procedure.
MsgBox(dr(0)) ' Shows the total number of records in the Emp table.- i.e result of “Select Count(*) from Emp”
dr.NextResult() ' Moves the Pointer to the second cursor. i.e “Select EmpId,EmpName,EmpSalary from Emp”
Dim str As String = ""
While (dr.Read())
str &= dr("EmpId") & vbTab
str &= dr(1) & vbTab
Dim ind As Integer
ind = dr.GetOrdinal("EmpSalary") ‘Return the Index of the column EmpSalary
If (dr.IsDBNull(ind)) Then
str &= "--" & vbCrLf
Else
str &= dr.GetDecimal(ind) & vbCrLf
End If
End While
MsgBox(str)
dr.Close()
con.Close()
End Sub

Note: If required cmd.CommandText can have more than one sql statement separated by “;”. This feature may or may not be
supported by a give database.

Using Factory class for writing Provider Independent code. (only in 2.0)
Dim con As IDbConnection
Dim fac As System.Data.Common.DbProviderFactory
fac = System.Data.Common.DbProviderFactories.GetFactory( “System.Data.SqlClient")
con = fac.CreateConnection()
con.ConnectionString = ""
Dim cmd As IDbCommand
cmd = con.CreateCommand()
Dim par As IDataParameter
par = cmd.CreateParameter()
Dim dr As IDataReader
dr = cmd.ExecuteReader

Transaction: Its a group of Sql Statements to de executed as one unit to modify the state of database.
ACID (Atomacity, Consistancy, Isolation and Durability)

Private Sub btnTransaction_Click(. . .) Handles btnTransaction.Click


Dim con As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=MSNetDemoDb;Integrated Security=TRUE")
Dim cmd1, cmd2 As SqlCommand
cmd1 = New SqlCommand()
cmd2 = New SqlCommand()
cmd1.Connection = con

5
Deccansoft Software Services – MS.NET ADO.NET

cmd2.Connection = con
cmd1.CommandText = "Insert into Emp(EmpName,EmpSalary) Values(‘A1’,10000)"
cmd2.CommandText = "Update Emp set EmpSalary = EmpSalary = 1000"
con.Open()
Dim trans As SqlTransaction
trans = con.BeginTransaction() 'Starts a new transaction over the connection
cmd1.Transaction = trans 'All the commands belonging to the transaction must have their Transaction property set.
cmd2.Transaction = trans
Try
cmd1.ExecuteNonQuery()
cmd2.ExecuteNonQuery() 'This will fail
trans.Commit() ' If no exception is thrown the transaction is commited.
Catch ex As Exception
MsgBox(ex.Message)
trans.Rollback() 'If exception is thrown the transaction is rolledback.
End Try
con.Close()
End Sub
While Practising:
On Execution of above code by default we will get an error and the transaction is rolledback and then Execute the above code
replacing “=” with “+” in the update statement and see that transaction is now commited and a record is inserted in Emp table.
Note: Once a transaction has begin over a connection, a command without its transaction property set cannot be executed on that
connection unless either the transaction is committed or rolledback.
How Generic code can be written – Example for execution of Stored Procedure.
Class DBUtil
Public Shared Function ExecuteSPNonQuery(ByVal connectionString As String, ByVal spName As String, ByVal
ParamArray params() As SqlParameter) As Integer
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(spName, con)
cmd.CommandType = CommandType.StoredProcedure
For Each p As SqlParameter In params
cmd.Parameters.Add(p)
Next
Try
con.Open()
Return cmd.ExecuteNonQuery()
Finally
con.Close()
End Try
End Function
End Class
Private Sub btnExecuteSP_Click(. . .) Handles btnExecuteSP.Click
Dim cs As String = "server=.\sqlexpress;database=msnet;integrated security=true"
Dim spName As String = "GetSalary"
Dim pId As SqlParameter = New SqlParameter("@Id", SqlDbType.Int)
pId.Direction = ParameterDirection.Input
Dim pSalary As SqlParameter = New SqlParameter("@sal", SqlDbType.Money)
pSalary.Direction = ParameterDirection.Output
pId.Value = CInt(txtId.Text)
DBUtil.ExecuteSPNonQuery(cs, spName, pId, pSalary)
If (Not IsDBNull(pSalary.Value)) Then txtSalary.Text = pSalary.Value
End Sub
Async Execution of Command
Dim dr As SqlDataReader
Dim cmd As New SqlCommand
Dim ar As IAsyncResult = cmd.BeginExecuteReader() ‘Begins the Asynchronous call
MessageBox.Show(“Continuee….”)
If (ar.IsCompleted) Then ‘Checks if the return value is available.
dr = cmd.EndExecuteReader(ar) ‘Ends the Asynchronous call and provides the return value.
End If
End Sub

6
Deccansoft Software Services – MS.NET ADO.NET – Dataset
DataSet:
• Its a DISCONNECTED object model for working with the data fetched from database.
• It is completely independent of the database i.e we can fetch the data from different database into the same same dataset
instance.
• Because of it is a disconnected model it reduces the number of trips to the server and thus it reduces the load on server.
This increases server’s scalability. (Scalability is the measure of server capability to handle increasing number of clients)
• Because the data is available locally, client doesn’t have a make a round trip to the server for every requirement, This
reduces network traffic and also client applications performance is improved.
• Because it is disconnected, it can be used for exchanging of data between distributed objects.
• Because it can easily interoperate with XML it can be used for communication between objects even if they are seperated
by firewall.
DataSet
DataTable
DataColumn
DataRow
Constraint
DataRelation

DataAdapter has following four Command Properties:


SelectCommand / InsertCommand / UpdateCommand / DeleteCommand

da.Fill(ds, “Emp”)
1. Opens the connection of SelectCommand of DataAdapter referenced by “da” (only if the connection is not already
open)
2. Executes the SelectCommand over that connection.
3. Fetches all the data at one time into frontend and creates a DataTable by name “Emp” (provided its not already
exiting) and new DataTable is added to Tables collection of DataSet referenced by “ds”.
4. For every record fetched from backend it creates a DataRow object and this row is either Added or Merged in the
DataTable Rows Collection.
5. Closes the connection (provided it has opened it).

da.Update(ds, “Emp”)
• Based on the state of each DataRow in the mentioned DataTable, the appropriate command of the DataAdapter
referenced by “da” is executed.
• If the state of the Row is “Deleted” then DeleteCommand is executed. If the state of the Row is “Modified” then the
“UpdateCommand” is executed and if the state of the row is “New” then the “InsertCommand” is executed.
• Based on the row being executed and the SourceColumn and SourceVersion property values of Parameter, the value
for the parameters of the command in execution are set.

SourceColumn property of SqlParameter:


It is the name of DataColumn in DataTable to which a Parameter is binded to i.e., while executing the command the value of the
parameter is taken from the mentioned column of the DataRow for which the command is being executed.
SourceVersion property of SqlParameter:
It decides which version (Current or Original) value of the column for a given row should be assigned to the parameter.

Dealing with Concurrency:-


When the changes made to the dataset by one user are updated in the database, the data in the dataset of the other users becomes
Dirty and such data should not be updated unless the dataset is refeshed and latest data is modified and updated because if
change are made to the dirty data and updated in database, it may override the change made by other users with out looking it
those changes.
Solution: The UpdateCommand and DeleteCommand CommantText must be modified to inlcude all the columns so that the
Original Values of Column are compared with the values in the database fields.
Ex: Update Emp Set EmpName=@EName, EmpSalay=@ESal
where EmpId=@OrgEId and EmpName=@OrgEName and EmpSalary=@OrgESalary

When DataAdapter#Update is executed and if it executes ethier UpdateCommand or DeleteCommand and finds the number of
records effected by execution of this command is zero then it throws “DBConcurrencyException”.

1
Deccansoft Software Services – MS.NET ADO.NET – Dataset

Dim con As New SqlConnection("server=localhost\sqlexpress;user id=sa;password=dss;database=MSNET")


Dim dt As DataTable
Dim WithEvents daEmp As SqlDataAdapter
Dim ds As DataSet
Dim dr As DataRow
Private Sub DataSetForm_Load(. . .) Handles MyBase.Load
daEmp = New SqlDataAdapter("Select EmpId, EmpName, EmpSalary from Emp", con)
MsgBox(daEmp.SelectCommand.CommandText)
ds = New DataSet()
RefreshEmpIdDropDown()
gvEmp.DataSource = ds.Tables(0)
''''''Insert Command'''''''
Dim cmdInsert As SqlCommand
Dim p(2) As SqlParameter
cmdInsert = New SqlCommand("insert into Emp(EmpName,EmpSalary) values (@ename,@esalary)", con)
p(0) = cmdInsert.Parameters.Add("@ename", SqlDbType.VarChar, 20)
p(0).SourceColumn = "EmpName"
p(0).SourceVersion = DataRowVersion.Current
p(1) = cmdInsert.Parameters.Add("@esalary", SqlDbType.Float)
p(1).SourceColumn = "Empsalary"
p(1).SourceVersion = DataRowVersion.Current
daEmp.InsertCommand = cmdInsert
''''''''Delete Command''''''''
Dim cmdDelete As SqlCommand
cmdDelete = New SqlCommand("Delete from emp where empid=@orgeid", con)
Dim par As SqlParameter
par = cmdDelete.Parameters.Add("@orgeid", SqlDbType.Int)
par.SourceColumn = "Empid"
par.SourceVersion = DataRowVersion.Original
daEmp.DeleteCommand = cmdDelete
'''''''Update Command'''''''''
Dim cmdUpdate As SqlCommand
cmdUpdate = New SqlCommand("Update Emp set empname=@ename, empsalary=@esalary where empid=@orgeid", con)
p(0) = cmdUpdate.Parameters.Add("@orgeid", SqlDbType.Int)
p(0).SourceColumn = "Empid"
p(0).SourceVersion = DataRowVersion.Original
p(1) = cmdUpdate.Parameters.Add("@ename", SqlDbType.VarChar, 20)
p(1).SourceColumn = "EmpName"
p(1).SourceVersion = DataRowVersion.Current
p(2) = cmdUpdate.Parameters.Add("@esalary", SqlDbType.Float)
p(2).SourceColumn = "EmpSalary"
p(2).SourceVersion = DataRowVersion.Current
daEmp.UpdateCommand = cmdUpdate

'Dim sb As New SqlCommandBuilder(daEmp)


'sb.ConflictOption = ConflictOption.CompareAllSearchableValues ‘ or OverwriteChanges
‘The above statement automatically frames the InsertCommand, UpdateCommand and DeleteCommand properties of the
‘mentioned DataAdapter (daEmp) and it does so based on the columns mentioned in the SelectCommand Properties of the
‘DataAdapter. Also for this to work the database table mentioned in the Select statement of SelectCommand must have the
‘PrimaryKey defined in the backend (database).
2
Deccansoft Software Services – MS.NET ADO.NET – Dataset
'MsgBox(sb.GetInsertCommand().CommandText)
'MsgBox(sb.GetUpdateCommand().CommandText)
'MsgBox(sb.GetDeleteCommand().CommandText)

'For Handling events of DataAdapter in C# do the following here…..


'daEmp.RowUpdated += new SqlRowUpdatedEventHandler(da_RowUpdated);
'daEmp.RowUpdating += new SqlRowUpdatingEventHandler(da_RowUpdating);
End Sub
Private Sub RefreshEmpIdDropDown()
ds.Clear()
daEmp.Fill(ds, "Emp")
dt = ds.Tables("Emp")
cmbId.Items.Clear()
For Each dr As DataRow In dt.Rows
cmbId.Items.Add(dr("EmpId"))
Next
cmbId.SelectedIndex = 0
End Sub
Private Sub cmbId_SelectedIndexChanged(. . .) Handles cmbId.SelectedIndexChanged
Dim drs() As DataRow
drs = dt.Select("empid=" & cmbId.Text) ‘This method return the array of datarow based on the condition given
as the first parameter.
dr = drs(0) ‘Because we are filtering based on the EmpId which is PrimaryKey we will have only 1 record.
‘dt.PrimaryKey = New DataColumn(){dt.Columns(“EmpId”)}
'dr = dt.Rows.Find(cmbId.Text) ‘Can be used only if the EmpId is maked as PrimaryKey in DataTable
txtName.Text = dr("EmpName").ToString()
If (dr.IsNull("EmpSalary")) Then
txtSalary.Text = ""
Else
txtSalary.Text = dr("EmpSalary").ToString()
End If
End Sub
Private Sub btnNew_Click(. . .) Handles btnAdd.Click
Dim dr As DataRow
dr = dt.NewRow()
‘dr = New DataRow() 'Is invalid because the constructor of the class is Protected
dr("Empname") = txtName.Text ‘EmpId is AutoIncrement thus it need not be set.
dr("EmpSalary") = CSng(txtSalary.Text)
dt.Rows.Add(dr)
End Sub
Private Sub btnModify_Click(. . .) Handles btnEdit.Click
Dim dr As DataRow
Dim drs() As DataRow
drs = dt.Select("empid=" & cmbId.Text)
dr = drs(0)
dr("Empname") = txtName.Text
dr("EmpSalary") = CSng(txtSalary.Text)
End Sub
Private Sub btnDelete_Click(. . .) Handles btnDelete.Click
Dim drs() As DataRow
drs = dt.Select("empid=" & cmbId.Text)
dr = drs(0)
Dim res As DialogResult
res = MessageBox.Show("Do you really want to delete the Row", "Delete", MessageBoxButtons.YesNo)
If (res = Windows.Forms.DialogResult.Yes) Then
MessageBox.Show("Before Delete: " & dt.Rows.Count)
dr.Delete() 'This only marks the row as deleted but the row is still there in Rows collection of DataTable
MessageBox.Show("After Delete: " & dt.Rows.Count)
cmbId.Items.RemoveAt(cmbId.SelectedIndex)
If (cmbId.Items.Count <> 0) Then cmbId.SelectedIndex = 0
End Sub
Private Sub btnUpdateDatabase_Click(. . .) Handles btnUpdateDatabase.Click
Try
3
Deccansoft Software Services – MS.NET ADO.NET – Dataset
daEmp.Update(ds, "Emp") 'This method executes the Insert, Update or Delete Commands of the dataadapter using
the data from a given DataRow and the command executed depends upon the RowState property of the datarow.
RefreshEmpIdDropDown()
Catch exp As DBConcurrencyException ‘Please refer to the notes at the beginning of this handout.
MsgBox(exp.Message)
dt.Clear()
daEmp.Fill(ds, "Emp")
RefreshEmpIdDropDown()
End Try
End Sub
‘Event Handler of DataAdapter.
Private Sub daEmp_RowUpdated(ByVal sender As Object, ByVal e As SqlRowUpdatedEventArgs)
Handles daEmp.RowUpdated
If (e.StatementType = StatementType.Insert) Then
Dim con As SqlConnection
con = e.Command.Connection ‘As the connection is already open it is not required to reopen the connection.
Dim cmd As New SqlCommand("Select @@Identity", con)
e.Row("EmpId") = cmd.ExecuteScalar() – EmpId of the record inserted is fetched and assigned to the current
rows EmpId field.
Dim index As Integer
index = cmbId.Items.Add(e.Row("EmpId"))
cmbId.SelectedIndex = index
End If
End Sub

Private Sub daEmp_RowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs)


Handles daEmp.RowUpdating
If e.StatementType == StatementType.Update Then
If e.Row("EmpSalary") > 50000 Then
MsgBox("Salary Cannot Be more than 50000")
e.Status = UpdateStatus.SkipCurrentRow
e.Row.RejectChanges()
End If
End If
End Sub
Table Mapping : Used for giving DataTable and Column Names which can be different from backend table and field names.
Note: The backend column names are Case Sensitive.
Private Sub btnTableMapping_Click(. . .) Handles btnTableMapping.Click
Dim dtmEmp As New Data.Common.DataTableMapping("Emp", "Employee")
daEmp.TableMappings.Add(dtmEmp)
Dim dcm As New Data.Common.DataColumnMapping("EmpId", "ID")
dtmEmp.ColumnMappings.Add(dcm)
dtmEmp.ColumnMappings.Add("EmpName", "Name")
dtmEmp.ColumnMappings.Add("EmpSalary", "Salary")
dt.Clear()
daEmp.Fill(ds, "Emp")
gvEmp.DataSource = ds.Tables("Employee")
End Sub
DataRelation:
Dim con As SqlConnection
Dim ds As New DataSet()
con = New SqlConnection("server=localhost\sqlexpress;user id=sa;password=dss;database=MSNET")
Dim daDept As New SqlDataAdapter("select * from Dept", con)
Dim daEmp As New SqlDataAdapter("select * from Emp", con)
daDept.Fill(ds, "Dept")
daEmp.Fill(ds, "Emp")
Dim dr As DataRelation
dr = New DataRelation("Dept_Emp", --Relationship Name
ds.Tables("Dept").Columns("DeptId"), // -- Parent Table column reference
ds.Tables("Emp").Columns("DeptId"),// - Child Table column refenrece
True) //– if true it also creates a Foreign Key Constraint in Child Table.
ds.Relations.Add(dr)

4
Deccansoft Software Services – MS.NET ADO.NET – Dataset
Dim drDept As DataRow
drDept = ds.Tables("Dept").Select("DeptId=1")(0) ‘Gets the reference to the Dept with DeptId=1
Dim drsEmps As DataRow()
drsEmps = drDept.GetChildRows("Dept_Emp") ‘Dept_Emp is the DataRelation name
Dim drEmp As DataRow
Dim str As String = ""
For Each drEmp In drsEmps
str &= drEmp(0) & vbTab & drEmp(1) & vbCrLf
Next
MsgBox(str)
MsgBox(ds.Tables("Emp").Rows(0).GetParentRow("Dept_Emp")("DeptName"))
– Gets the name of the Department of the first emp in the Emp DataTable Employee.
DataView: One DataTable can have multiple DataViews and each DataView can be Sorted and Filtered independently.
Dim dt As DataTable
dt = ds.Tables("Emp")
Dim dvEmp As DataView
dvEmp = dt.DefaultView ‘ or dv = New DataView(dt)
dvEmp.RowFilter = "EmpSalary > 2000"
Dim str As String = ""
Dim drv As DataRowView
For Each drv in dvEmp
str &= drv(0) & " " & drv("EmpSalary") & vbCrLf
Next
MsgBox(str)
dvEmp.Sort = "EmpId"
MsgBox(dvEmp.Find(2)) 'Index of the row with EmpId=2
dvEmp.Sort = "EmpName,EmpSalary" ‘Sorted first using EmpName and if name is same sorts using EmpSalary
MsgBox(dvEmp.Find(New Object() {"E1", 1000}))‘Gets index of row with EmpName=’E1” & EmpSalary=1000
dvEmp.Sort = "EmpName"
Dim drvs() As DataRowView = dvEmp.FindRows("E1") ‘Gets the DataRowView of the row having EmpName=’E1’

Note: One DataView cannot be using multiple DataTables. Between DataTable and DataView its One to Many relationship.

Creating DataSet Programmatically

Dim ds As New DataSet


Dim dt As New DataTable("Table")
Dim dcNumber As New DataColumn("Number", GetType(Integer))
Dim dcStar As New DataColumn("*", GetType(String))
dcStar.DefaultValue = "*"
Dim dcCounter As New DataColumn("Counter", GetType(String))
dcCounter.AutoIncrement = True
Dim dcEquals As New DataColumn("=", GetType(String))
dcEquals.DefaultValue = "="
Dim dcResult As New DataColumn("Result", GetType(String))
dcResult.Expression = "Number * Counter"
ds.Tables.Add(dt)
dt.Columns.Add(dcNumber)
dt.Columns.Add(dcStar)
dt.Columns.Add(dcCounter)
dt.Columns.Add(dcEquals)
dt.Columns.Add(dcResult)
For i As Integer = 1 To 10
Dim dr As DataRow
dr = dt.NewRow()
dr("Number") = Integer.Parse(txtNumber.Text)
dt.Rows.Add(dr)
Next
gvTable.DataSource = ds.Tables(0)

Set Primary Key for a DataTable Programmatically


ds.Tables("Emp").PrimaryKey = New DataColumn(){ ds.Tables(“Emp”).Columns(“EmpId”) }
The datatype of PrimaryKey is DataColumn array because in some cases it might be composite key(multiple cols)

5
Deccansoft Software Services – MS.NET ADO.NET – Dataset
Adding ForeignKey Programmatically
Dim fk As ForeignKeyConstraint
fk = New ForeignKeyConstraint(ds.Tables("Dept").Columns("DeptID"), -- Primary Key
ds.Tables("Emp").Columns("DeptID")) – Foreign Key
fk.DeleteRule = Rule.SetNull – if Dept is deleted then all the emps in Emp table DeptId is set to Null.
fk.UpdateRule = Rule.Cascade –‘If DeptId is modified in the Dept table it automatically changes in Emp table.
ds.Tables("Emp").Constraints.Add(fk)
Note: Any kind schema changes make to the dataset or datatable connot be reflected in backend.
To Compute second last max salary
Dim maxSalary As Decimal
maxSalary = ds.Tables("Emp").Compute("max(empsalary)", Nothing)
MsgBox(ds.Tables("Emp").Compute("max(empsalary)", "EmpSalary<" & maxSalary))

TypedDataSet: It’s a class inherited from DataSet and is generated for a specific table in database schema.
• Right Click on Project Æ Add New Item Æ Select DataSet from the List (EmpDataSet.xsd)
• Drag and Drop the Table from the Server Explorer.
• Right Click on the Table and Select Configure. Change the Method Names if required. You can also add more methods to
the TableAdapter.
To Fill DataSet and Show all retrieve all data To Add a New Row to the DataTable
Dim taEmp As New Dim taEmp As New
EmpDataSetTableAdapters.EmpTableAdapter EmpDataSetTableAdapters.EmpTableAdapter
Dim ds As New EmpDataSet Dim dt As EmpDataSet.EmpDataTable
taEmp.Fill(ds.Emp) dt = taEmp.GetData()
Dim s As String = "" Dim dr As EmpDataSet.EmpRow
For Each dr As EmpDataSet.EmpRow In ds.Emp.Rows dr = dt.NewEmpRow
s &= dr.EmpId & vbTab dr.EmpName = "EName1"
s &= dr.EmpName & vbTab dr.EmpSalary = 10000
If (Not dr.IsEmpSalaryNull) Then dt.Rows.Add(dr)
s &= dr.EmpSalary & vbCrLf taEmp.Update(dt)
End If End Sub
Next
MsgBox(s)
End Sub
Bulk Copy
Dim sb As New SqlBulkCopy("Connection String of Destination")
sb.DestinationTableName = "DestTablename"
sb.ColumnMappings.Add("SourceColumnName","DestColName") ‘Do this for all the columns of the table
‘sb.WriteToServer(dr) ‘Read on DataReader and copy to new table
‘sb.WriteToServer(dt) ‘Read from DataTable and copy to new table
‘sb.WriteToServer(drs) ‘Read from Array of DataRow and copy to new table

6
Deccansoft Software Services – MS.NET IO Streams
• In a stream of text, one unit of data is one character i.e. at a time we read or write a character or an array of characters. Here
we should know the type of encoding before we can read characters from text stream. Based on the type of encoding used
number of bytes a character takes is decided and this remains fixed through out the stream.
• In a stream of Binary, the unit of data is not fixed as it can be of any type. The order of writing to the stream must be known
so that in the same order we can read data from the stream.

Types of Streams:
• Basic Stream:
• Character / Text Stream
• Binary Stream

Stream (abstract class)


FileStream / MemoryStream / BufferedStream are inherited from Stream

Stream and its inherited classes provide us data in the form of bytes. This object is used as the core on which another wrapper
object has to be created based on the format of the data in that stream.

FileStream used to read from, write to, and manipulate file-related operations. FileStream objects support random access to files
using the Seek method. Seek allows the read/write position to be moved to any position within the file.
A MemoryStream is a non-buffered stream whose encapsulated data is directly accessible in memory. This stream has no
backing store and might be useful as a temporary buffer. It uses byte array for managing data in memory.

A BufferedStream is a Stream that adds buffering to another Stream such as a NetworkStream. (FileStream already has buffering
internally, and a MemoryStream does not need buffering.) A BufferedStream can be composed around some types of streams in
order to improve read and write performance.

Character Stream is divided into


TextReader (abstract class) Text Writer (abstract class)
StringReader StringWriter
StreamReader StreamWriter

StreamReader reads characters from Streams, using Encoding to convert characters to and from bytes. It has a constructor that
attempts to ascertain what the correct Encoding for a given Stream is, based on the presence of an Encoding-specific preamble,
such as a byte order mark.
StreamWriter writes characters to Streams, using Encoding to convert characters to bytes. It reads characters from Strings. It
allows you to treat Strings with the same API, so your output can be either a Stream in any encoding or a String.

StringReader and StringWriter manage data in a char array or string.

Binary Streams: Binary Reader & Binary Writer


BinaryReader and BinaryWriter read and write encoded strings and primitive data types from and to a basic stream (Stream
category of objects).
The three standard I/O streams are
• Console.In -- TextReader - By default reads from Keyboard
• Console.Out -- TextWriter - By default writes to Monitor
• Console.Error – TextWriter - By default writes to Monitor
Methods of TextReader: Read, ReadBlock, ReadLine & ReadToEnd.
Methods of TextWriter: Write and WriteLine
Module Echo1 Module Echo2
Public Sub Main() Public Sub Main()
Dim data As Integer Dim n As Integer
While True Dim c(10) As Char
data = Console.In.Read() n = Console.In.Read (c, 0, c.Length)
If (data = -1) Then ‘n = Console.In.ReadBlock(c, 0, c.Length)
Exit While Dim i As Integer
End If For i = 0 To c.Length - 1
Console.Out.Write(ChrW(data)) Console.Out.WriteLine("C({0}) = {1}", i, AscW(c(i)))
End While Next
End Sub Console.WriteLine("Actual No of Chars Read : " & n)
End Module End Sub
End Module
1
Deccansoft Software Services – MS.NET IO Streams
Note: In .NET we cannot directly read any particular type of data from the keyboard. We have to read keyboard input as string
and convert to appropriate type.

Module KeyboardInputIOADD
Public Sub Main()
Dim n1, n2 As Integer
Dim str As String
Console.Out.Write("Enter the Value of First Number : ")
str = Console.In.ReadLine()
n1 = CInt(str)
Console.Out.Write("Enter the Value of Second Number : ")
str = Console.In.ReadLine()
n2 = CInt(str)
Console.WriteLine("Sum : " & (n1 + n2))
End Sub
End Module

Module FileToConsole ‘Works good only for language with Module KeyBoardToFile ‘Works good only for language
one byte charset with one byte charset
Sub Main(ByVal args() As String) Sub Main(ByVal args() As String)
Dim fs As FileStream Dim fs As FileStream
Try Try
fs = New FileStream(args(0), FileMode.Open, fs = New FileStream(args(0), FileMode.CreateNew,
FileAccess.Read, FileShare.None) FileAccess.Write)
Catch e As FileNotFoundException Catch err As IOException
Console.Write("File not found") Console.WriteLine("File Already Exists")
Return Return
End Try End Try
Dim data As Integer Dim data As Integer
While (True) While True
data = fs.ReadByte() data = Console.In.Read()
If (data = -1) Then If (data = -1) Then
Exit While Exit While
End If End If
Console.Write(ChrW(data)) fs.WriteByte(CByte(data))
End While End While
Console.Out.Flush() fs.Close()
fs.Dispose() End Sub
End Sub End Module
End Module
The above programs works good only with One Byte characterset i.e ASCII character set.

Module UnicodeTextWriting Module UnicodeTextReading


Sub Main() Sub Main()
Dim fs As New Dim fs As New FileStream("c:\unicode.txt",FileMode.Open)
FileStream("c:\unicode.txt",FileMode.OpenOrCreate) Dim sr As New StreamReader(fs,
‘if file is there it opens else creates a new file System.Text.Encoding.Unicode)
Dim sw As New StreamWriter(fs, Console.Write(sr.ReadToEnd())
System.Text.Encoding.Unicode) sr.Close()
sw.Write("Hello") End Sub
sw.Close() End Module
End Sub
End Module

Module KeyBoardToTextFile Module ReverseFileContent


Sub Main(ByVal args() As String) Sub Main(ByVal args As String())
Dim sw As StreamWriter Dim fs As New FileStream(args(0),
Try FileMode.Open, FileAccess.ReadWrite)
Dim fs As New FileStream(args(0), Dim sr As New StreamReader(fs)
FileMode.CreateNew, FileAccess.Write) Dim strOriginal As String
sw = New StreamWriter(fs, strOriginal = sr.ReadToEnd()
System.Text.Encoding.Unicode) Dim sb As New System.Text.StringBuilder(strOriginal.Length)
Catch err As IOException For i As Integer = strOriginal.Length - 1 To 0 Step -1

2
Deccansoft Software Services – MS.NET IO Streams
Console.WriteLine("File Already Exists") sb.Append(strOriginal(i))
Return Next
End Try fs.Seek(0, SeekOrigin.Begin)
Dim data As Integer Dim sw As New StreamWriter(fs)
While True sw.Write(sb.ToString())
data = Console.Read() sw.Flush()
If (data = -1) Then sw.Close()
Exit While sr.Close()
End If End Sub
sw.Write(ChrW(data)) End Module
End While
sw.Close()
End Sub
End Module

Module BinaryReaderWriter Module Split


Sub Main() Sub Main(ByVal args() As String)
Dim file As System.IO.FileStream Dim fs As FileStream
file = New FileStream("d:\demo.dat", fs = New FileStream(args(0), FileMode.Open)
FileMode.OpenOrCreate, FileAccess.ReadWrite) Dim name, ext As String
Dim n As Integer = 65 name = fs.Name
Dim m As Long = 54321 Dim fi As New FileInfo(name)
Dim str As String = "ABCDEFG" 'name = fi.Name()
Dim bsw As New BinaryWriter(file, ext = fi.Extension
Text.Encoding.Unicode) Dim ind As Integer
bsw.Write(n) ind = name.LastIndexOf(".")
bsw.Write(str) name = name.Substring(0, ind)
bsw.Write(m) Dim size As Integer
size = CInt(args(1))
file.Seek(0, SeekOrigin.Begin) Dim data(size) As Byte
Dim i As Integer
Dim bsr As New BinaryReader(file, While (True)
Text.Encoding.Unicode) i += 1
n = bsr.ReadInt32() Dim n As Integer
str = bsr.ReadString() n = fs.Read(data, 0, data.Length - 1)
m = CLng(bsr.ReadDouble()) - This would read junk data If (n = 0) Then Exit Sub
because while writing it was written using LONG format. Dim newfile As New FileStream(name & i & ext,
Console.WriteLine(n & " " & str & " " & m) FileMode.Create)
file.Close() newfile.Write(data, 0, n)
End Sub newfile.Close()
End Module End While
End Sub
End Module
(While running first command line argument is path of file
to split, second argument is size each new file.)

Public Class Student


Public Id As Integer
Public Name As String
Public Sub SaveAsText(ByVal filename As String)
Dim fs As New FileStream(filename, FileMode.OpenOrCreate)
Dim sw As New StreamWriter(fs, System.Text.Encoding.Unicode)
sw.WriteLine(Id)
sw.WriteLine(Name)
sw.Close() : fs.Close()
End Sub
Public Shared Function LoadFromText(ByVal filename As String) As Student
Dim fs As New FileStream(filename, FileMode.Open)
Dim sr As New StreamReader(fs, System.Text.Encoding.Unicode)
Dim s As New Student()
s.Id = Integer.Parse(sr.ReadLine())
s.Name = sr.ReadLine()
sr.Close() : fs.Close()

3
Deccansoft Software Services – MS.NET IO Streams
Return s
End Function
Public Sub SaveAsBinary(ByVal filename As String)
Dim fs As New FileStream(filename, FileMode.OpenOrCreate)
Dim bw As New BinaryWriter(fs, System.Text.Encoding.Unicode)
bw.Write(Id)
bw.Write(Name)
bw.Close()
fs.Close()
End Sub
Public Shared Function LoadFromBinary(ByVal filename As String) As Student
Dim fs As New FileStream(filename, FileMode.Open)
Dim br As New BinaryReader(fs, System.Text.Encoding.Unicode)
Dim s As New Student
s.Id = br.ReadInt32()
s.Name = br.ReadString()
br.Close() : fs.Close()
Return s
End Function
End Class

Public Class StudentForm


Dim s As Student
Private Sub btnCreate_Click(. . .) Handles btnCreate.Click
s = New Student()
s.Id = Integer.Parse(txtId.Text)
s.Name = txtName.Text
End Sub
Private Sub btnGet_Click(. . .) Handles btnGet.Click
txtId.Text = s.Id.ToString()
txtName.Text = s.Name
End Sub
Private Sub btnSave_Click(. . .) Handles btnSave.Click
If (btnBinary.Checked) Then
s.SaveAsBinary("c:\student_binary.dat")
Else
s.SaveAsText("c:\student_text.txt")
End If
End Sub
Private Sub btnLoad_Click(. . .) Handles btnLoad.Click
If (btnBinary.Checked) Then
s = Student.LoadFromBinary("c:\student_binary.dat")
Else
s = Student.LoadFromText("c:\student_text.txt")
End If
End Sub
End Class
Working with File System
In IO we have four more class which is used for managing the file system on the machine. They are
1. File provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of a
FileStream
2. Directory provides static methods for creating, moving, and enumerating through directories and subdirectories.
3. FileInfo provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation
of FileStream objects. This class cannot be inherited.
4. DirectoryInfo provides instance methods for creating, moving, and enumerating through directories and subdirectories.
So generally, when we want to perform more than one operation, we go for FileInfo and DirectoryInfo. But for a given path if we
want to perform only one operation we go for File and Directory.

Module FileDirectory
Public Sub Main()
Dim args() As String
args = System.Environment.GetCommandLineArgs()
‘args(0) is the path of exe being executed.

4
Deccansoft Software Services – MS.NET IO Streams
‘args(1) is the first command line argument.
Dim dir As DirectoryInfo
dir = New DirectoryInfo(args(1))
If (Not dir.Exists) Then
Console.WriteLine("Directory Not Existing")
Return
End If
Dim arFileInfo As FileInfo() = dir.GetFiles(“*.txt”)
For Each objFileInfo As FileInfo In arFileInfo
Console.WriteLine(objFileInfo.FullName)
Next
For Each objDirectoryInfo As DirectoryInfo In dir.GetDirectories(“D*”)
Console.WriteLine(objDirectoryInfo.FullName)
Next
End Sub
End Module
Serialization & Deserialization
Serialization: It is a process in which the objects state can be converted to a form in which it can be either persisted or
transported.
There are two types of serialization: Binary Serialization & XML Serialization
• In Binary serialization all the Instance data members except the ones declared as <NonSerialized()> are serialized.
• Static/Shared members of the class are not serialized.
• In XML serialization only the Public Instance members of a Public Class can be serialized.

Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Xml.Serialization
<Serializable()> _ <Serializable()> _
Public Class MyLine Public Class MyPoint
Public From As MyPoint Public X As Integer
Public [To] As MyPoint <NonSerialized()> Public Y As Integer
End Class End Class
Module SaveLine Module LoadLine
Sub Main() Sub Main()
Dim line As New MyLine() Dim line As MyLine
Dim p1 As New MyPoint Dim bf As New BinaryFormatter
p1.X = 10 : p1.Y = 20 Dim fs As New IO.FileStream("c:\line.dat", FileMode.Open)
Dim p2 As New MyPoint line = DirectCast(bf.Deserialize(fs), MyLine)
p2.X = 15 : p2.Y = 25 It creates a new object based on the input from the stream
line.From = p1 : line.To = p2 (filestream) referenced by “fs” and also sets the state of it.
Dim bf As New BinaryFormatter() Console.WriteLine(line.From.X & " " & line.From.Y)
Dim fs As New IO.FileStream("c:\line.dat", Console.WriteLine(line.To.X & " " & line.To.Y)
IO.FileMode.OpenOrCreate) fs.Close()
bf.Serialize(fs, line) End Sub
The state of the object referred by “line” is converted to End Module
binary stream and written to the stream (filestream)
referenced by “fs”
fs.Close()
End Sub
End Module
Module WritePointXML Module ReadPointXML
Sub Main() Sub Main()
Dim p As New MyPoint Dim p As MyPoint
p.X = 10 Dim xs As New XmlSerializer(GetType(MyPoint))
p.Y = 20 Dim fs As New IO.FileStream("c:\point.xml",
Dim xs As New XmlSerializer(GetType(MyPoint)) IO.FileMode.Open)
Dim fs As New IO.FileStream("c:\point.xml", p = CType(xs.Deserialize(fs), MyPoint)
IO.FileMode.OpenOrCreate) Console.WriteLine(p.X & " " & p.Y)
xs.Serialize(fs, p) fs.Close()
fs.Close() End Sub
End Sub End Module
End Module

5
Deccansoft Software Services – MS.NET XML

XML is an universal specifation / standard by W3C providing context / description and structure to the otherwise
ordinary text data.
XML can be used for exchanging information between objects developed in different programming language and
running on discreate platforms and even if the objects are separed by the firewall.

XML Schema Document / DTD

Yes Is Yes
XML Instance Well
valid Further processing
Document formed

No No

Error
Rules for an XML DOC to be Well Formed
• It should have only one root element tags in XML .These tags are called ‘Root Tags’.
• Tags are case sensitive and hance the opening and closing tag case must match.
• Every tag must be closed. ex:<br></br> or <br/>
• Tags must not overlap each other.
• The value of attributes must be encosed in quotes. Ex: RollNo=”104”

An HTML document which is well formed is called XHTML

An XML Schema document contains declarations of various XML constructs including elements attridutes etc.
This declaration define the syntax and the restrictions while using them in an instance document.

Structure of XML Document (HAS A – Relationship)


Document
Processing Instruction.
Comments.
Root Element.
Element.
Processing Instruction (Iinstruction either to the parser or to the application processing the xml)
<?xml version=”1.0” encoding=”UTF-8”?>
<?document format=”rft”?>
Comment.
Text.
CData Section. - <![CDATA[a<b]]> -Unparsed Charater Data.
Entity References. (&lt; / &gt; / &quot; / &amp; / &apos;)
Student.xml Students.xml
<Students> <Students>
<Student RollNo="101"> <Student RollNo="101">
<Name>S1</Name> <Name>
<Result>Passed</Result> <FirstName>AA</FirstName>
</Student> <LastName>XX</LastName>
<Student RollNo="104"> </Name>
<Name>S4</Name> <Result>Passed</Result>
<Result>Failed</Result> </Student>
</Student> <Student RollNo="102">
<Student RollNo="105"> <Name>
<Name>S5</Name> <FirstName>BB</FirstName>
<Result>Passed</Result> <LastName>YY</LastName>
</Student> </Name>
</Students> <Result>Failed</Result>
</Student>

1
Deccansoft Software Services – MS.NET XML

</Students>
XML DOM (Document Object Model)
• XML DOM is a collection of objects created using the various constructs in an XML document.There objects
are arranged in a “Tree” like structure
• Using DOM we can read the data from XML document and can also we can create an XML document from
scratch or edit an existing document.
• XMLNode is the parent class of all XML classes represending various constructs in an XML document.

XML DOM
Imports System.Xml
Dim doc As XmlDocument
Dim enStuds As XmlElement
Private Sub btnShow_Click(. . .) Handles btnShow.Click
doc = New XmlDocument
'doc.PreserveWhitespace = True
doc.Load("..\..\Student.xml")
Dim enStuds As XmlElement
enStuds = doc.DocumentElement
Dim enStud As XmlElement
enStud = CType(enStuds.ChildNodes(1), XmlElement)
MsgBox(enStud.GetAttribute("RollNo"))
Dim anRollNo As XmlAttribute
anRollNo = enStud.Attributes("RollNo")
MsgBox(anRollNo.Value)
Dim enName As XmlElement
enName = CType(enStud.FirstChild, XmlElement)
Dim tnName As XmlText
tnName = CType(enName.FirstChild, XmlText)
MsgBox(enStuds.OuterXml)
End Sub

Private Sub Form1_Load(. . .) Handles MyBase.Load


doc = New XmlDocument()
'doc.PreserveWhitespace = True
doc.Load("..\..\Student.xml")
enStuds = doc.DocumentElement
Dim enStud As XmlElement
For Each enStud In enStuds.ChildNodes
cmbId.Items.Add(enStud.GetAttribute("RollNo"))
Next
End Sub

Private Sub cmbId_SelectedIndexChanged(. . .)


Dim enStud As XmlElement
enStud = CType(enStuds.ChildNodes(cmbId.SelectedIndex), XmlElement)
txtName.Text = enStud.FirstChild.FirstChild.Value
If (enStud.LastChild.FirstChild.Value = "Passed") Then
rbnPassed.Checked = True
Else
rbnFailed.Checked = True
End If
End Sub

Private Sub btnNames_Click(. . .) Handles btnNames.Click


Dim nlNames As XmlNodeList
nlNames = doc.GetElementsByTagName("Name")

2
Deccansoft Software Services – MS.NET XML

Dim s As String = ""


For Each enName As XmlNode In nlNames
If (enName.NextSibling.FirstChild.Value = "Passed") Then
s &= enName.FirstChild.Value & vbCrLf
End If
Next
MsgBox(s)
End Sub

Private Sub btnAdd_Click(. . .) Handles btnAdd.Click


Dim enStud, enName, enResult As XmlElement
Dim tnName, tnResult As XmlText
Dim anId As XmlAttribute
enStud = doc.CreateElement("Student")
enName = doc.CreateElement("Name")
enResult = doc.CreateElement("Result")
tnName = doc.CreateTextNode(txtName.Text)
If (rbnPassed.Checked) Then
tnResult = doc.CreateTextNode("Passed")
Else
tnResult = doc.CreateTextNode("Failed")
End If
anId = doc.CreateAttribute("RollNo")
anId.Value = cmbId.Text
enStuds.AppendChild(enStud)
enStud.AppendChild(enName)
enStud.AppendChild(enResult)
enName.AppendChild(tnName)
enResult.AppendChild(tnResult)
enStud.SetAttributeNode(anId)
doc.Save("..\..\Student.xml")
cmbId.Items.Add(cmbId.Text)
End Sub

Private Sub btnRemove_Click(. . .) Handles btnRemove.Click


Dim enStud As XmlElement
enStud = CType(enStuds.ChildNodes(cmbId.SelectedIndex), XmlElement)
enStud.ParentNode.RemoveChild(enStud)
doc.Save("../Student.xml")
cmbId.Items.Remove(cmbId.Text)
End Sub

Working with Dataset


TYPES OF XML ELEMENTS:
Complex Type: An element which has either attribute or child elements is a complex type of element.
SimpleType: If it is not complex then it is a simple type of element
All attributes are always simple type

When the XML document is loaded into DataSet then every ComplexType is loaded Data Table and the simple
types with in it are loaded as DataColumns.
If one complex type is nested with in the other complex type then a DataRelation object is created using those two
Complex types / DataTables. If they do not have any common field between them, then a column in the format of
<parenttable>_Id is added to both the Datatables.

Ex: Student(RollNo,Name,Student_Id) - DataTable


Name(FirstName,LastName,Student_Id) – DataTable
Student_Name – DataRelation
3
Deccansoft Software Services – MS.NET XML

Private Sub btnStudentDS_Click(. . .) Handles btnSimpleDS.Click


Dim ds As New DataSet()
'Dim strXML As String
'strXML = ""
'ds.ReadXml(New IO.StringReader(strXML))
ds.ReadXml("../../Student.xml")
Dim dr As DataRow
dr = ds.Tables("Student").NewRow
dr("RollNo") = cmbId.Text
dr("Name") = txtName.Text
If (rbnPassed.Checked) Then
dr("Result") = "Passed"
Else
dr("Result") = "Failed"
End If
ds.Tables("Student").Rows.Add(dr)
ds.WriteXml("../../Student.xml")
End Sub

Private Sub btnStudentsDS_Click(. . .) Handles btnDS.Click


Dim ds As New DataSet()
ds.ReadXml("..\..\Students.xml")
Dim drStud As DataRow
Dim s As String = ""
For Each drStud In ds.Tables("Student").Rows
Dim name As String
Dim drsName As DataRow()
drsName = drStud.GetChildRows("Student_Name")
name = CStr(drsName(0)("FirstName")) & vbTab & CStr(drsName(0)("LastName"))
s &= CStr(drStud("RollNo")) & vbTab & name & vbTab & CStr(drStud("Result")) & vbCrLf
Next
MsgBox(s)
End Sub
XML DataDocument
Private Sub btnXmlDataDocument_Click(. . .) XMLDataDocument is used to construct an XML
Dim ds As New DataSet() Dom Tree from the data already existing in DataSet.
ds.ReadXml(“..\..\Students.xml") It’s a class Inherited fromXMLDocument, thus this has
Dim dd As New XmlDataDocument(ds) all the properties and methods which the
MsgBox(dd.DocumentElement.OuterXml) XMLDocument class has.
End Sub

XMLTextWriter and XMLTextReader


XMLTextReader and XMLTextWriter are strem based and can be used for situation in which an XML Document
needs to be parsed only once. This way of parsing the document is very fast when compared to DOM API as the
complete document doesn’t have to loaded into memory at once. This is also because XMLTextReader uses Linear
Parsing and Forward Only Parsing.

Private Sub btnXmlTextWriter_Click(. . .) Handles btnXmlTextWriter.Click


Dim tw As New XmlTextWriter("c:\demo.xml", Nothing)
tw.WriteStartDocument()
tw.WriteStartElement("Root")
tw.WriteAttributeString("att", "attvalue")
tw.WriteElementString("E1", "TextInE1")
tw.WriteEndElement()

4
Deccansoft Software Services – MS.NET XML

tw.WriteEndDocument()
tw.Close()
End Sub
Private Sub btnTextReader_Click(. . .) Handles btnTextReader.Click
Dim xr As New XmlTextReader("../../Student.xml")
Dim strNames As String = ""
Dim strCurrentName As String = ""
While (xr.Read())
If (xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "Name") Then
xr.Read()
strCurrentName = xr.Value
ElseIf (xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "Result") Then
xr.Read()
If (xr.Value = "Passed") Then
strNames &= strCurrentName & vbCrLf
End If
End If
End While
MsgBox(strNames)
End Sub
XPath
XPath is a specification for framing of Path so that we can get the reference to nodes in XMLDocument based on
their values.

Private Sub btnGetAllNodes_Click(. . .) Sample XPaths:


Dim nl As XmlNodeList • E1/E2: All occurences of E2 which are immidiate child of E1.
nl = doc.SelectNodes(txtPath.Text) • E1/*/E2: All occurences of E2 which are grand children of E1 or
Dim str As String = "" the ancistor of E1.
For Each n As XmlNode In nl • E1//E2: All occurances of E2 which has E1 as ancestor.
str &= n.Name & " - " • //E1: All occurances of E1 any where in the document.
& n.OuterXml & vbCrLf • E1[1]: First occurance of E1.
Next • E1[last()]: Last occurance of E1.
MsgBox(str)
• E1[E2]: All occurance of E1 which has E2 as child.
End Sub
• E1[E2 and E3 ]: All occurance of E1 which has both E2 and E3 as
child.
Arithemetic Operators: +, - , *, div
• E1[E2 or E3 ]: All occurance of E1 which has either E2 or E3 as
Logical Operators: and, or, not
child.
• E1[not(E2)]: All occurance of E1 where E2 is not a Child.
• E1/@A1: All attributes by name A1 of E1 elements.
• E1/@*: All attributes of all occurances element E1.
• E1/[@A1=’1’]: All occurances of E1 where attributes A1=1.
• E1/text(): Returns all the text nodes under E1.
• E1/comment(): Returns all comment nodes under E1

5
Two types of Build

Build Conf Debugging Speed Machine Out Folder Files


Debug Y Slow Development bin\debug (.exe or .dll) and .pdb
Release N Fast Production bin\release .exe or .dll
(* .pdb contains Debugging Symbols )
To switch between Build / Release Æ Build Æ Configuration Manager > Debug / Release

Run in Debug mode with break points - With Debugging (F5).


Without Debugging (Crtl + F5) – Break points will not work in this mode.

List of Debugging Window:


1. Quick Watch Window: Right Click on Expression and Select Quick Watch.
a. Can change the value of variable but not expression.
b. Can View the datatype of variable or expression.
c. We can add the variable or expression to Watch Window.
2. Watch:
a. Can change the value of variable but not expression.
b. Can View the datatype of variable or expression.
3. Auto:
a. We can view lists of all variables used in the current context of execution.
b. Can change the value of variable but not expression.
c. Can View the datatype of variable or expression.
4. Locals:
a. To view the lists all the variables of the current function in execution.
b. Can change the value of variable but not expression.
c. Can View the datatype of variable or expression.
5. Immediate:
a. To execute some statements in Break point mode
b. To view the value of variable using “?” ex: ?n
6. Call Stack:
a. Useful for Reverse Tracing of Functions executed.
b. Can see Parameter values also.
7. Threads:
a. To view the list of all threads. We can also see Thread’s Priority, Location, State.
b. If break point is used in a function all the threads running that function will break at the
same time. Using this window this we can continue our debugging with any one of the
threads.
8. Modules: Loaded list of dll’s will be shown. Helps to find unused dll’s. Can also see Order the
dll’s are called, Version Used, address where the Dll’s is in memory. Always a dll
uses the same location in memory which is called as base address. Advanced
programmers will take care of this Base address also to improve performance of the
application.
9. Processes: Application the Debugger attached to (Stored Procedures/ Java Script/ Manage
Code). By default it is attached to Application.
10. Memory: Used by System Programmers. Not much useful for us.
11. Disassembly: Shows assembly language code for our application.
12. Registers: Used by System Programmers. Not much useful for us.

Edit & Continue: In Break Mode we can edit the code, without stopping Debugging.

Debugging options for Break Point:


Hit Count: Can specify at what iteration the debugger should stop
Ex: In a for loop of 100 iterations we know error occurs at 91 iteration. Then we can
specify Hit Count for the debugger as 91. Then it will stop at 91st iteration.

Condition: We can specify a condition for the debugger to stop at a Break Point.
Ex: If n1 = = 0 Stop at this Break Point

Thread:

Filter:

Exception Handling Debug > Exception > Search for needed Exception

Put Tick Mark on -- Break when Exception thrown.


This will help to know where Exception raised exactly. In other way to find where exactly
Exception raised we can remove Try Block, but with this we can know without removing Try
Block if Try Block already mentioned in the code.

Steps for attaching an already running application to the Debugger

Debug > Attach Process > Select the needed application from the list > Click on Attach

Switch to Normal mode Æ Debug > Stop Debugging


By dong this even after stopping debugging the application we can continue with the execution.
Deccansoft Software Services – MS.NET MultiThreading
• Thread: The path of execution of instructions within a process is called a thread. A program with multiple
simultaneous paths of execution is said to be multithreaded application.
• A running instance of an application is a process. Every process has its own memory address space and it is in that
all the data and the instructions of that process reside.
Types of Scheduling:
Preemptive: OS can un-schedule a thread even if it is not completed its task.
Non-Preemptive: OS cannot unschedule the thread when it is not completed. A Thread should either unschedule itself of
should complete its task.

Scheduling is done based on the priority of the thread. A thread with highest priority is executed in the processor. If
multiple threads with exist same priority then a time slice is assigned to each thread.

In MS-Windows execution of the thread is based on: Preemptive Scheduling Æ Priority Æ Time slice

Also, in all versions of MS Windows, to avoid starvation low priority threads are also executed in the processor but the
number of times they execute is very less.

Ready

Sleep Suspended
Running

Blocked Dead Wait

1. Running: A thread is said to be in running state when it is in processor executing one of its instruction.
2. Ready: Thread is waiting for processor to be allocated to it. This is the only entry point to running state. When the
tread is unscheduled it returns from running to ready state.
3. Dead: After the execution of all the instructions in the thread it goes to dead state. Dead threads cannot be revived.
4. Sleeping: While sleeping the thread doesn’t perform any task. A thread goes to sleeping state by itself and for a
predefined time. A sleeping thread when interrupted throws “ThreadInterruptedException”. All the
resources/locks that the thread is holding are blocked when a thread is in sleeping state.
5. Suspended: A thread can suspend itself or by another thread. The suspended thread cannot resume by itself. A
thread can go to suspended state for an indefinite time.
6. Blocked: A thread is said to be in blocked state when the resources are not available to it and it is waiting for them.
The thread automatically resumes once the resource is made available.
7. Waiting: The thread before it goes to waiting state releases its resources / locks it has blocked. A waiting thread has
to be pulsed by another thread only then it can resume and goes to ready state.
In .Net every thread has two objects associated with it.
1. An Object of type System.Threading.Threads and it is responsible for managing the lifetime and the states of the
thread.
2. A custom object, it is responsible for providing the instructions and data which the thread has to manage during its
lifetime.
using System; class Program
using System.Threading; {
class Demo static void Main(string[] args)
{ {
public string Message; Console.WriteLine("Main method begins");
public int Interval = 1000; Demo d = new Demo();
public void Run() Thread t = new Thread(
{ new ThreadStart(d.Run));
for (int i = 0; i < 5; i++) //while(true) t.Name = "T";
{ d.Message = "Hi";
Console.WriteLine(Thread.CurrentThread.Name d.Interval = 1000;
+ " : " + Message); t.Start();
Thread.Sleep(Interval); Demo d1 = new Demo();
} Thread t1 = new Thread(
} new ThreadStart(d1.Run));
} t1.Name = "T1";
d1.Message = "Hello";
1
Deccansoft Software Services – MS.NET MultiThreading
Demo class encapsulates the instructions of thread in Run d1.Interval = 2000;
method and also encapsulates data in Message and Interval t1.Start();
data members. t.Join(2000);
if (t.IsAlive)
The process terminates when the main thread terminates. A Console.WriteLine("t is alive");
thread terminates only when the other non background t1.Join();
threads which it has created terminates. The creator doesn’t //t1.IsBackground = t.IsBackground = true;
wait for the background thread if it has to terminate. Console.WriteLine("Main method ends");
}
}

t.Join():If a thread executes t.Join() the current thread state is changed to waiting and it remains in that state until the
thread referred by “t” is terminated.
Join(3000) – The current threads waits for a max of 3000 milliseconds and then would resume automatically.
t.IsAlive() to check if the thread is dead or alive..
Thread.CurrentThread : To Get the reference of the current thread.
t.ThreadPriority = ThreadPriority.Highest.
t.Abort(): To stop the thread. When the Abort() method is called on a thread it throws ThreadAbortException
irrespective of its state. If this exception is unhandled the thread terminates, and if it is handled and if
Thread.ResetAbort() is executed the thread is not aborted and would continue normal execution.
t.IsBackground: When a thread is created as background thread, the creator thread does not wait for the background
thread to terminate or join the creator thread. The background thread is automatically aborted when the creator thread
aborts.
Priority Demo class Program
class Hello {
{ static void Main(string[] args)
public long Counter; {
public void Run() Hello h1 = new Hello();
{ Thread t1 = new Thread(new ThreadStart(h1.Run));
while(true) Hello h2 = new Hello();
Counter++; Thread t2 = new Thread(new ThreadStart(h2.Run));
} t1.Priority = ThreadPriority.AboveNormal;
} t2.Priority = ThreadPriority.BelowNormal;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
t1.Start(); t2.Start();
Thread.Sleep(2000);
t1.Abort(); t2.Abort();
double perT1 = 100.0 * h1.Counter / (h1.Counter + h2.Counter);
double perT2 = 100.0 * h2.Counter / (h1.Counter + h2.Counter);
Console.WriteLine("T1 " + perT1);
Console.WriteLine("T2 " + perT2);
}
}
Suspend-Resume / Sleep – Interrupt / Abort Demo
private void Run() Thread t;
{ int n = 0; private void btnStart_Click(. . .)
while (true) {
{ t = new Thread(new ThreadStart(Run));
try t.IsBackground = true;
{ t.Start();
n++; }
lblCounter.Text = n.ToString(); private void btnAbort_Click(. . .)
Thread.Sleep(1000); {
} t.Abort();
catch (ThreadAbortException e) }
{ private void btnSuspend_Click(. . .)
DialogResult dlgResult; {
dlgResult = MessageBox.Show("Are you sure?", t.Suspend();
"Abort", MessageBoxButtons.YesNo); }
if (dlgResult == DialogResult.No) private void btnResume_Click(. . .)
Thread.ResetAbort(); {
} t.Resume();
catch (ThreadInterruptedException e) }

2
Deccansoft Software Services – MS.NET MultiThreading
{ private void btnInterrupt_Click(. . .)
n = 0; {
} t.Interrupt();
} }
} private void btnThreadState_Click(. . .)
{
MessageBox.Show(t.ThreadState.ToString());
}
public static void Main() //To make form as the
startup object.
{
Application.Run(new DemoForm());
}

In Project Properties: Change Output Type to Widnows Application and Startup Object to DemoForm

Thread Synchronization
Critical Section is a block of code which can be executed by only one thread at any given instance of time.
If there are two threads executing on same shared object then both the threads can execute some method on the object at
the same point of time and if they then change the state of the object, it may result in ambiguity of data causing Threads
De-Synchronization. To avoid this we block the object.
In C# In VB Every object in .NET has a special type of resource called as Monitor. A thread
lock(ob) SyncLock(ob) trying to enter the lock block would have to acquire the monitor of the specified
{ object and only if it can do so it would be executing the locked block other wise
} End SyncLock it will have to wait for the monitor.

Thread safe class: An class whose objects state is not desynchronized when it is being used by more then one thread at
the same time. Even if the class is not thread safe we can make it thread safe by executing its method in a lock block.
Locking degrades throughput i.e. the rate at which the output is generated.
class Shared class Demo
{ {
int n; Shared s;
public int Incr() public Demo(Shared s)
{ {
lock (this) this.s = s;
{ }
n++; public void Run()
Thread.Sleep(1000); {
return n; //lock (s) //To be used of the Demo.Incr is not coded
} //{ // for Thread Safety
} Console.WriteLine(s.Incr());
//Alternate way of writing same as above. //}
public int Incr() }
{ }
try class SyncDemo
{ {
Monitor.Enter(this); public static void Main()
n++; {
Thread.Sleep(1000); Shared s = new Shared();
return n; Demo d1 = new Demo(s);
} Demo d2 = new Demo(s);
finally Thread t1 = new Thread(new ThreadStart(d1.Run));
{ Thread t2 = new Thread(new ThreadStart(d2.Run));
Monitor.Exit(this); t1.Start();t2.Start();
} }
} }
}
Mutex: It is a synchronization resource managed by the OS. Thus it can be used for synchronizing threads running in
different processes. Monitor is a .Net specific object and is local to a given process and thus it cannot be used for
synchronizing threads running in different processes.
• If two or more threads have to be synchronized using Mutex, then either they should all refer to either the same
Mutex object or if the objects are different then all the Mutex objects must have same name.

3
Deccansoft Software Services – MS.NET MultiThreading
m.WaitOne(): Check for the availability of mutex. If available acquires it and would continue execution other the current
thread state is changed to waiting for mutex.
m.ReleseMutex().
Mutex m = new Mutex(false,”test”);
If the first parameter is true, the first thread object will be the owner of the mutex. False will prevent the thread object to
acquire the mutex till WaitOne() method is executed.
public class MutexDemo
{
//Even though Mutex object is different for each thread all of them
//use the same Mutex of the OS, because they have same name.
Mutex m= new Mutex(false,"Test"); //Test is the name of the Mutex.
//static Mutex m = new Mutex() //Because its static its shared by all threads within the process.
public void Run()
{
try
{
m.WaitOne();
Console.WriteLine(Thread.CurrentThread.Name);
Thread.Sleep(1000);
//Replace above line with code to be executed by only one thread at a time
}
finally
{
m.ReleaseMutex();
}
}
public static void Main()
{
Console.WriteLine("Main Started");
for(int i=0;i<20;i++)
{
MutexDemo d = new MutexDemo();
Thread t = new Thread(new ThreadStart(d.Run));
t.Start();
}
Console.WriteLine("Main Ended");
}
}
To allow only once instance of the application to run at a time.
public static void Main()
{
Mutex m = new Mutex(false, "M1");
bool firstInstance = m.WaitOne(0,true);
if (firstInstance)
{
Application.Run(new Form2());
m.ReleaseMutex();
}
else
MessageBox.Show("Instance already running");
}
Semaphores: Semophore is used for synchronizing threads (within and in different process) and it works exactly like
Mutex but has a facility to allow more than one thread (but a pre defined count) to execute a given block at the same
time. Mutex can be treated as a special case of semaphore where count = 1.
class Demo1 class SemaphoreDemo
{ {
//static Semaphore s = new Semaphore(3,3); public static void Main()
Semaphore s = new Semaphore(3,3, “Test”); {
public void Run() for (int i = 0; i < 15; i++)
{ {
Console.WriteLine(Thread.CurrentThread.Name Demo1 d = new Demo1();
+ " Started"); Thread t = new Thread(
s.WaitOne(); new ThreadStart(d.Run));

4
Deccansoft Software Services – MS.NET MultiThreading
Thread.Sleep(1000); t.Name = "T" + i;
s.Release(); t.Start();
Console.WriteLine(Thread.CurrentThread.Name }
+ " Ended"); Console.WriteLine("Main method ends");
} }
} }

5
Deccansoft Software Service – MS.NET Windows Service
• A Windows Service is an application that does not have a user interface and runs in the background of the Operating
System. It commonly runs without human intervention and can be automatically started when the computer starts up and
it stops when the computer is shutdown.
• A Windows Services can start even without a user login in. Even if the login session is changed by the user of the system
the windows service would continue to execute.
• A Windows Service can use the identity of any particularly configured user for accessing the resources of the machine.
This is irrespective of the currently logged in user.

Examples: Internet Information Server (IIS), SQL, Oracle etc. Windows Services were formerly known as NT Services.
The List below is the methods in Service Base Class which we may want to override in Custom Windows Service Class.
OnStart / OnStop / OnPause / OnContinue / OnShutDown / OnPowerEvent / OnSessionChange / OnCustomCommand

Steps to build a Windows Service


Go to File -> New Project -> Highlight Windows Service in the adjacent window

Protected Overrides Sub OnStart(ByVal args() As String) Protected Overrides Sub OnContinue()
Dim sw As New StreamWriter("c:\DemoService.txt", Dim sw As New StreamWriter("c:\DemoService.txt",
True) True)
sw.WriteLine("In Start: " & Now.ToString()) sw.WriteLine("In Continue: " & Now.ToString())
For Each s As String In args sw.Close()
sw.WriteLine(s) End Sub
Next
sw.Close()
End Sub
Protected Overrides Sub OnStop() Protected Overrides Sub OnPause()
Dim sw As New StreamWriter("c:\DemoService.txt", Dim sw As New StreamWriter("c:\DemoService.txt",
True) True)
sw.WriteLine("In Stop: " & Now.ToString()) sw.WriteLine("In Pause: " & Now.ToString())
sw.Close() sw.Close()
End Sub End Sub

Similarly override the other methods as needed.


Now go to Design View of the Service -> Properties Window & change the ServiceName and other properties as needed
Right click Design View -> Select Add Installer. Build the Windows Service Project.

Note: A windows service cannot be executed using Ctrl + F5 i.e. by running the exe file, instead it must be installed as a
windows service using the utility program called as Installutil.exe

Installing / Deploying a windows Service in the OS.


Go to command prompt through Visual Studio Icon.
Goto debug directory of the WindowsService project where you have saved the file on the computer.

ex: D:\DemoSol\..\bin\Debug> installutil.exe –i Windowservice1.exe

After typing the command & when u press enter , a new Dialog Box will appear asking for you to type in the user name and
password.
Give the username as “.\Administrator” and appropriate password.
Note: the username must be preseded by machine name or “.”.
Launching a Windows Service
Go to Control Panel -> Administrative Tools -> Services.

Note: For any change to be made to the windows service , first it has to be stopped and then rebuild after making the
changes. You don’t have to deply the service again.

Providing input to windows service by passing command line agruments


Select the windows services -> properties window and set the cmd line arguments in “ Start Parameters “ text box.
The Command Line Arguments mentioned in Properties of Windows Servvice are not stored parmanently and we have to set
them each and every time we start a service. Thus a better way of provind input data to the Windows Service can be “.config”
file “<appSettings>”.
1
Deccansoft Software Service – MS.NET Windows Service

To Automatically Start a Windows Service with OS:


Goto ProjectInstaller Design Window Æ ServiceInstaller1 Æ Properties (F4)
Æ Starup Type = Automatic / Manual / Disabled
Æ DisplayName
Æ Description.
Service Controller:
It is a windows application (with GUI) for controlling or managing or providing input to a Windows Service

Steps for developing Service Controller


1. To the existing Solution Explorer, add a new Windows Application project (DemoServiceController).
2. Go to ToolBoxÆComponent TabÆdrag & drop Service Controller.
3. Go to the properties of service controller and set the name to sc.
4. In Properties also set the ServiceName (Scroll to last to find recently developed service)

Private Sub btnStart_Click(. . .) Private Sub btnStop_Click(. . .)


sc.Refresh() sc.Refresh()
Dim args() As String = {"One", "Two", "Three"} sc.Stop()
sc.Start(args) End Sub
End Sub
Private Sub btnPause_Click(. . .) Private Sub btnContinue_Click()
sc.Refresh() sc.Refresh()
sc.Pause() sc.Continue()
End Sub End Sub
Private Sub btnGetServiceStatus_Click(. . .) Private Sub btnCustomCommand_Click(. . .)
sc.Refresh() sc.Refresh()
MsgBox(sc.Status.ToString()) sc.ExecuteCommand(CInt(txtNumber.Text))
End Sub End Sub
• To Send Custom Command to the windows service: sc.ExecuteCommand (CInt(txtNumber .Text));
To a Windows Service in the form of custom command a number can be submitted which must be in the range of 128 to
255 only.
• Before any action is submitted to the Windows Service, Service Controller must be refreshed.

To retrieve the Custom Command in a Windows Service class.


In Windows Service class Override OnCustomCommand:
Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
Dim sw As New StreamWriter("c:\DemoService.txt", True)
sw.WriteLine("In Custom Command: " & command.ToString())
sw.Close()
End Sub

Now make the Controller Application as the startup Project and Run it.

2
Deccansoft Software Services – MS.NET Window Forms

Control Properties: Dock, Anchor

Label Properties: Text, Image, AutoSize, UseMnemonic

LinkLabel Properties: Text, LinkVisited, LinkColor, VisitedLinkColor, ActiveLinkColor, DisableLinkColor

Form Properties: AcceptButton, CancelButton, AutoScroll, ContentMenuStrip, MenuStrip, IsMdiContainer

TextBox
Properties: Text, PasswordChar, Multiline, Readonly, AutoCompleteMode, AutoCompleteSource, AutoCompleteCustomSource
Events: KeyPress, KeyDown, TextChanged, Validating
KeyPress event files only for keys with ASCII value whereas KeyDown event fires for all keys on the keyboard.

private void txtDemo_KeyPress(object sender, KeyPressEventArgs e)


{
if (e.KeyChar < ‘0’ || e.KeyChar > ‘9’)
e.Handled = true;
}
private void txtDemo_Validating(object sender, CancelEventArgs e)
{
if (txtDemo.Text == "")
{
errorProvider1.SetError(txtDemo, "The value cannot be empty");
e.Cancel = true;
}
else
errorProvider1.SetError(txtDemo, "");
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
MessageBox.Show(“Control + A is Clicked”);
}
In keyDown we cannot distinguish upper and lower case characters.

CheckBox:
Properties: Text, Checked, ThreeState, CheckState (Checked/UnChecked/Intermediate)
Events: CheckedChanged.

RadioButton: To group we have to use a common container which can be either Form or GroupBox or Panel.

ComboBox
Properties: Items, DropDownStyle (Simple/DropDownList/DropDown), Text, SelectedIndex, SelectedItem, SelectedValue,
Events: SelectedIndexChanged

class Student private void DemoForm_Load(. . .)


{ {
public int Id; cmbStudent.Items.Add(new Student(1,"S1"));
public string Name; cmbStudent.Items.Add(new Student(2,"S2"));
public Student(int id, string name) cmbStudent.Items.Add(new Student(3,"S3"));
{ cmbStudent.Items.Add(new Student(4,"S4"));
Id = id; }
Name = name; private void cmbStudent_SelectedIndexChanged(. . .)
} {
public override string ToString() Student s = (Student)cmbStudent.SelectedItem;
{ MessageBox.Show(s.Id.ToString());
return Name; }
}
}

To a ComboBox any type of object can be added. The ToString() implementation of that objects will be displayed as the Items
text in the Combobox.

1
Deccansoft Software Services – MS.NET Window Forms

DateTimePicker:
Properties: Value, MinDate, MaxDate, ShowCheckBox, Checked, ShowUpDown, Format, CustomFormat,
Events: ValueChanged

MonthCalander
Properties: CalanderDimension.Width / Height, SelectionRange.Start /.End, ShowWeekNumbers
Events: DateChanged

MaskedTextBox Properties: Text, Mask, PromptChar

PictureBox Properties: Image, SizeMode (Normal / StretchImage / AutoSize / CenterImage / Zoom)

NotifyIcon
• To the Form add NotifyIcon control and ContextMenuStrip.
• To the ContextMenuStrip add two Items (Show and Exit)
• Set Icon and ContextMenuStrip properties of NofifyIcon and handle MouseDoubleClick event.

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)


{
this.Visible = true;
}
bool exit=false; //will be set to true of menu on notifyicon is used.
private void DemoForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (exit)
return;
e.Cancel = true;
this.Visible = false;
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
exit = true;
this.Close();
}

Working with GDI


private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int w, h;
w = this.ClientSize.Width;
h = this.ClientSize.Height;
Pen p = new Pen(Color.Red);
g.DrawArc(p, w / 2 - 50, h / 2 - 40, 100, 80,0,45);
p.Dispose();
p = new Pen(Color.Green);
g.DrawRectangle(p, w / 2 - 50, h / 2 - 40, 100, 80);
g.DrawLine(p, 0, 0, w, h);
g.DrawLine(p, 0, h, w, 0);
Point[] pts = new Point[3];
pts[0].X = w / 2;
pts[0].Y = h / 2 - 50;
pts[1].X = w / 2 + 50;
pts[1].Y = h / 2 + 50;
pts[2].X = w / 2 - 50;
pts[2].Y = h / 2 + 50;
g.DrawPolygon(p, pts);
2
Deccansoft Software Services – MS.NET Window Forms

Brush br;
Color col = Color.FromArgb(50, 255, 127, 12);
col = Color.FromName("brown");//.FromKnownColor(KnownColor.InactiveCaption);
br = new SolidBrush(col);
g.FillEllipse(br, 10, 10, 100, 100);
br = new HatchBrush(HatchStyle.DashedHorizontal, Color.Yellow, Color.Red);
g.FillEllipse(br,30, 30, 50, 50);
Image img = new Bitmap("C:\\Winnt\\coffee bean.bmp");
g.DrawImage(img, 200, 0);
br = new TextureBrush(img);
g.FillRectangle(br, 80, 80, 200, 240);
g.TranslateTransform(w/2, h/2);
g.RotateTransform(-30);
g.ScaleTransform(2, 2); //Zooming
Point p1 = new Point();
Point p2 = new Point();
p1.X = 20; p1.Y = 30; p2.X = 100; p2.Y = 100;
br = new LinearGradientBrush(p1, p2, Color.Red, Color.Blue); ;
g.FillRectangle(br, 20, 30, 70, 70);
g.ResetTransform();
Font f = new Font("Arial", 24, FontStyle.Bold | FontStyle.Italic);
g.DrawString("Hello", f, br, 200, 10);
g.PageUnit = GraphicsUnit.Inch;
p = new Pen(Color.Red, 0.2F);
g.ResetTransform();
g.DrawRectangle(p, 0.5F, 0.5F, 1, 2);
}
private void FigureForm_Resize(object sender, EventArgs e)
{
Invalidate();
}
Graphics Form
Imports System.Drawing
Imports System.Windows
Imports System.Windows.Forms
Imports System.Runtime.Serialization.Formatters.Binary

<Serializable()> _
Structure Line
Public P1, P2 As Point
End Structure

Dim l As Line
Dim lstLine As New List(Of Line)

Private Sub GraphicsForm_Paint(. . .) Handles MyBase.Paint


Dim g As Graphics
g = e.Graphics
For Each l As Line In lstLine
g.DrawLine(Pens.Red, l.P1, l.P2)
Next
End Sub

Private Sub GraphicsForm_MouseDown(. . .) Handles MyBase.MouseDown


If (e.Button = MouseButtons.Left) Then
l = New Line()
l.P1 = e.Location
End If
End Sub

Private Sub GraphicsForm_MouseUp(. . .) Handles MyBase.MouseUp


If (e.Button = MouseButtons.Left) Then

3
Deccansoft Software Services – MS.NET Window Forms

l.P2 = e.Location
lstLine.Add(l)
Invalidate()
End If

Private Sub btnSave_Click(. . .) Handles btnSave.Click


Dim bf As New BinaryFormatter()
Dim fs As New IO.FileStream(Application.StartupPath & "\demo.dat", IO.FileMode.OpenOrCreate)
bf.Serialize(fs, lstLine)
fs.Close()
End Sub Private Sub btnLoad_Click(. . .) Handles btnLoad.Click
Dim bf As New BinaryFormatter()
Dim fs As New IO.FileStream(Application.StartupPath & "\demo.dat", IO.FileMode.Open)
lstLine = CType(bf.Deserialize(fs), List(Of Line))
fs.Close()
Invalidate()
End Sub
Private Sub btnClear_Click(. . .) Handles btnClear.Click
lst.Clear()
Invalidate()
End Sub
End Class
Creating Image Programmatically
Bitmap bmp = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bmp);
g.FillEllipse(Brushes.Red, 0, 0, 200, 200);
g.DrawLine(Pens.Yellow, 0, 0, 200, 200);
g.DrawLine(Pens.Yellow, 0, 200, 200, 0);
bmp.Save("c:\\demo.gif", System.Drawing.Imaging.ImageFormat.Gif);

Working with Menus and Dialogs


Two Types Of DialogBoxes:
1. Model: Until it is disposed we cannot work with the parent form.
2. Modeless: It always remains on top of the owner form but while it is open we can also work with the owner form.
Figure Form
Public Class FigureForm
Enum FigureType
Rectangle
Ellipse
End Enum
Dim fig As FigureType
Dim x, y As Integer
Dim col As Color = Color.Red
Private Sub FigureForm_Paint(. . .) Handles MyBase.Paint
Dim g As Graphics
g = e.Graphics
g.TranslateTransform(0, msMain.Height)
Dim p As New Pen(col)
If (fig = FigureType.Rectangle) Then
g.DrawRectangle(p, x, y, 100, 100)
Else
g.DrawEllipse(p, x, y, 100, 100)
End If
End Sub
Private Sub mnuRectangle_Click(. . .) Handles mnuRectangle.Click, cmnuRectangle.Click
fig = FigureType.Rectangle
Invalidate()
End Sub
Private Sub mnuEllipse_Click(. . .) Handles mnuEllipse.Click, cmnuEllipse.Click
fig = FigureType.Ellipse
Invalidate()
4
Deccansoft Software Services – MS.NET Window Forms

End Sub
Private Sub mnuExit_Click(. . .) Handles mnuExit.Click
Me.Close()
End Sub
• The MenuItem when ever has to synchronize with the Applications state, it must be done in the immediate parents Menu
Item[DropDownOpening] event handler.
Private Sub mnuFigure_DropDownOpening(. . .) Handles mnuFigure.DropDownOpening
If (fig = FigureType.Rectangle) Then
mnuRectangle.Checked = True
mnuEllipse.Checked = False
Else
mnuRectangle.Checked = False
mnuEllipse.Checked = True
End If
End Sub
‘before this code is written dialog class should be created.
Private Sub mnuPosition_Click(. . .) Handles mnuPosition.Click
Dim dlgPosition As New PositionDialog
dlgPosition.X = x
dlgPosition.Y = y
If (dlgPosition.ShowDialog() = DialogResult.OK) Then Steps for Showing the Dialog
x = dlgPosition.X • Create the instance of the Dialog Class.
y = dlgPosition.Y • Set the Dialog Properties
Invalidate() • Show the Dialog using ShowDialog Method
End If • If OK is Pressed use the dialog properties
End Sub
Private Sub mnuColor_Click(. . .) Handles mnuColor.Click
Dim dlgColor As New ColorDialog
dlgColor.Color = col
If (dlgColor.ShowDialog = DialogResult.OK) Then
col = dlgColor.Color
Invalidate()
End If
End Sub
//Following code to show modeless dialog.
Dim WithEvents dlgDemo As DemoDialog
Private Sub btnModelessDialog_Click(. . .) Handles btnModelessDialog.Click
If (dlgDemo Is Nothing) Then
dlgDemo = New DemoDialog()
dlgDemo.Owner = Me
dlgDemo.DemoText = txtDemo.Text
dlgDemo.Show()
End If
End Sub
Private Sub dlgDemo_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
Handles dlgDemo.FormClosed
dlgDemo = Nothing
End Sub
End Class

Position Dialog

Public Class PositionDialog

5
Deccansoft Software Services – MS.NET Window Forms

Public Property X() As Integer


Get
Return CInt(txtLeft.Text)
End Get Steps for Creating Modal DialogBox:
Set(ByVal value As Integer) • Add a Windows Form to the project
txtLeft.Text = value.ToString • Set the following Form Properties:
End Set FormBorderStyle – FixedDialog
End Property MinimizeBox – false
MaximiseBox – false
Public Property Y() As Integer ShowInTaskBar – false
Get StartPosition – CenterParent
Return CInt(txtTop.Text) • Design the Dialog – Place all the controls, give them proper Name and set
End Get correct TabOrder (View->TabOrder).
Set(ByVal value As Integer) • Write the Validations for the controls on the DialogBox.
txtTop.Text = value.ToString • Add the Properties to the DialogBox class so that the Control Properties
End Set are available to the form where the Dialog is used.
End Property • Handle OK Click EventHandler
Do the Form level Validations and if everything is Ok execute the
Private Sub btnOK_Click(. . .) Handles btnOK.Click
following line
Me.DialogResult = DialogResult.OK this.DialogResult=DialogResult.OK
End Sub • Handle Cancel Click EventHandler
Private Sub btnCancel_Click(. . .) Handles btnCancel.Click
this.DialogResult=DialogResult.Cancel
Me.DialogResult = DialogResult.Cancel
End Sub
End Class
DemoDialog (Modeless Dialog)
Public Class DemoDialog
Private Sub btnOK_Click(. . .) Handles btnOK.Click
btnApply_Click(Nothing, Nothing)
Me.Close()
End Sub
Private Sub btnCancel_Click(. . .) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub btnApply_Click(. . .) Handles btnApply.Click
Dim frmOwner As FigureForm
frmOwner = DirectCast(Me.Owner, FigureForm) ‘Owner is of type Form and hence the casting.
frmOwner.txtDemo.Text = Me.txtDemo.Text
End Sub
Public Property DemoText() As String
Get
Return txtdemo.text
End Get
Set(ByVal value As String)
txtDemo.text = value
End Set
End Property
End Class
ApplyButton Demo
Public Class ApplyForm
Private Sub btnApply_Click(. . .) Handles btnApply.Click
MsgBox("Changes Applied")
btnApply.Enabled = False
End Sub
Private Sub ControlChanged(ByVal sender As Object, ByVal e As EventArgs)
btnApply.Enabled = True
End Sub
Private Sub ApplyForm_Load(. . .) Handles MyBase.Load
RegisterEvents(Me)
End Sub
Private Sub RegisterEvents(ByVal parent As Control)
For Each con As Control In parent.Controls
6
Deccansoft Software Services – MS.NET Window Forms

If (TypeOf con Is TextBox) Then


Dim txt As TextBox
txt = CType(con, TextBox)
AddHandler txt.TextChanged, New EventHandler(AddressOf ControlChanged)
ElseIf (TypeOf con Is CheckBox) Then
Dim chk As CheckBox
chk = CType(con, CheckBox)
AddHandler chk.CheckedChanged, New EventHandler(AddressOf ControlChanged)
ElseIf (TypeOf con Is RadioButton) Then
Dim chk As RadioButton
chk = CType(con, RadioButton)
AddHandler chk.CheckedChanged, New EventHandler(AddressOf ControlChanged)
ElseIf (TypeOf con Is DateTimePicker) Then
Dim chk As DateTimePicker
chk = CType(con, DateTimePicker)
AddHandler chk.ValueChanged, New EventHandler(AddressOf ControlChanged)
ElseIf (TypeOf con Is Panel) Then
RegisterEvents(con)
ElseIf (TypeOf con Is TabControl) Then
Dim tc As TabControl
tc = CType(con, TabControl)
For Each tp As TabPage In tc.TabPages
RegisterEvents(tp)
Next
End If
Next
End Sub
End Class
MDI Form
For the Form set IsMDI Container -> true
Public Class MdiMainForm
Private Sub FigureToolStripMenuItem_Click(. . .)
Dim frmFigure As New FigureForm
frmFigure.MdiParent = Me
frmFigure.Show()
End Sub
Dim WithEvents frmGraphics As GraphicsForm
Private Sub GraphicsToolStripMenuItem_Click(. . .)
If (frmGraphics Is Nothing) Then
frmGraphics = New GraphicsForm()
frmGraphics.MdiParent = Me
frmGraphics.Show()
Else
frmGraphics.Activate()
End If
End Sub
Private Sub frmGraphics_FormClosed(. . .) Handles frmGraphics.FormClosed
frmGraphics = Nothing
End Sub
Private Sub CloseAllToolStripMenuItem_Click(. . .)
For Each frm As Form In Me.MdiChildren
frm.Close()
Next
End Sub
Private Sub TestingToolStripMenuItem_DropDownOpening(. . .)
If (TypeOf Me.ActiveMdiChild Is GraphicsForm) Then
ActiveForGraphicsToolStripMenuItem.Enabled = True
Else
ActiveForGraphicsToolStripMenuItem.Enabled = False
End If
End Sub
Private Sub TileHorzToolStripMenuItem_Click(. . .)

7
Deccansoft Software Services – MS.NET Window Forms

Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub TileVertToolStripMenuItem_Click(. . .)
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub

Private Sub CascadeToolStripMenuItem_Click(. . .)


Me.LayoutMdi(MdiLayout.Cascade)
End Sub
Private Sub ArrangeIconsToolStripMenuItem_Click(. . .)
Me.LayoutMdi(MdiLayout.ArrangeIcons)
End Sub
End Class
Working with Custom Controls
NumTextBox
Public Class NumTextBox
Public Event NumberPressed As EventHandler
Public Property Value() As Integer
Get
If (Me.Text = "") Then Return 0
Return CInt(Me.Text)
End Get
Set(ByVal value As Integer)
Me.Text = CStr(value)
End Set
End Property
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If (AscW(e.KeyChar) < 48 OrElse AscW(e.KeyChar) > 57) Then
e.Handled = True
Return
End If
'MyBase.OnKeyPress(e)
OnNumberPressed()
End Sub
Protected Overridable Sub OnNumberPressed()
RaiseEvent NumberPressed(Me, EventArgs.Empty)
End Sub
End Class
OKCancel Button
[DefaultEvent("OKClick"), DefaultProperty("OKText ")]
public partial class OKCancelButton : UserControl
{
public event EventHandler OKClick, CancelClick;

[Category("Appearance")] public string CancelText


[DefaultValue(“OK”)] {
[Description("This is OK Button of the control")] get
public string OKText {
{ return btnCancel.Text;
get }
{ set
return btnOK.Text; {
} btnCancel.Text = value;
set }
{ }
btnOK.Text = value; protected virtual void OnCancelClick()
} {
} if (CancelClick != null)
protected virtual void OnOKClick() CancelClick(this, EventArgs.Empty);
{ }
if (OKClick != null) private void btnCancel_Click(object sender, EventArgs e)

8
Deccansoft Software Services – MS.NET Window Forms

OKClick(this, EventArgs.Empty); {
} OnCancelClick();
private void btnOK_Click(object sender, EventArgs e) }
{
OnOKClick();
}

Adding Login Facility to the appliacation


Login Dialog
private void btnLogin_Click(object sender, EventArgs e)
{
DialogResult res = MessageBox.Show("Is user valid?", "Login", MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
this.DialogResult = DialogResult.OK;
else
MessageBox.Show("Invalid username or password! Please try again");
}
Startup Class
static class Program
{
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginDialog dlgLogin = new LoginDialog();
if (dlgLogin.ShowDialog() != DialogResult.OK)
return;
Application.Run(new MainForm());
}
}

9
Deccansoft Software Services Delegates and Events
Delegate is an object, using which one object can invoke the functionality of other object without knowing its
ClassName or MethodName.
Delegates are like function pointers in “C”, but are type safe
Example: The thread objects ‘Start’ method creates a new thread in the process and invokes the method (Run) from
our Custom class. It gets the reference to our method from delegate ‘ThreadStart’ whose instance is created using our
CustomMethod.
Delegates are used for call back implementation
Sample Application
1. Create a new ClassLibrary Project (CalculatorLibrary). Check the option CreateDirectory for Solution and give
the Solution name as DelegateDemo
2. To the ClassLibrary add the following delegate and class.

//Declares a delegate called MathOperationHandler which can hold a reference to any method whizch has int,int
as parameters and returns int.
public delegate int MathOperationHandler(int a,int b);

public class Calculator


{
private MathOperationHandler Operation;
public Calculator(MathOperationHandler op)
{
//Initialize the reference to the delegate
Operation = op;
}
public void CalculateAndPrint(int a, int b)
{
//Using the delegate reference we are invoking the method whose reference is stored in it.
Console.WriteLine(Operation(a, b));
// or Console.WriteLine(Operation.Invoke(a, b));
}
}

3. To the Solution add a New Console Application project.


4. Add the Reference to the CalculatorLibrary –
Right click on project Æ Add Reference ÆProjects ÆSelect the Library Project and click on OK.
5. Add the following code to the Console Application.
class MyMath
{
//This method is based on the declaration of MathOperationHandler Delegate
public int Add(int a, int b)
{
return a + b;
}
}
class DelegateDemo
{
public static void Main()
{
MyMath m = new MyMath();
MathOperationHandler delMathOperation;
delMathOperation = new MathOperationHandler(m.Add);
Calculator cal = new Calculator(delMathOperation);
cal.CalculateAndPrint(10,20);
}
}
6. Make DelegateDemo as the startup object and execute the application.

Note: In VB.NET following syntax should be used to create the instance of delegate (use Addressof).

Dim delMathOperation As New MathOperationHandler(AddressOf m.Add);


Note: One Delegate instance can encapsulate only one method reference.
An instance of delegate can be created, passing to the constructor, reference to the method, same as delegate signature.

1
Deccansoft Software Services Delegates and Events

Chat Application:
1 Create a new Project (WindowsApplication ) Æ To the Project add a new Class (ChatCoordinator.cs).

2 Every individual client would register and instance of following Delegate instance with the coordinator., so that
the same can be used by the coordinator for communicating the broadcasted message to all the registered clients
irrespective of their types
public delegate void MessageHandler(string message);

3 Write the Coordinator class


public class ChatCoordinator
{
//It is used for storing all the registered delegates (Handlers)
List<MessageHandler> lstHandlers = new List<MessageHandler>();

//This method is called by all clients to register their delegates


public void Register(MessageHandler handler)
{
lstHandlers.Add(handler);
}
//This method is called by the client when it has to broascast a message to all other clients.
public void BroadCast(string message)
{
// Iterate through all the delegates in the list and invoking the method of the client
foreach (MessageHandler mh in lstHandlers)
mh(message);
}
//Following code is used for making ChatCoordinator as Singleton Class.
//A class which can be instantiated only once is called singleton
private ChatCoordinator() //Private Constructor so that instance of this class cannot be created from
{} //outside the class.
private static ChatCoordinator obj;
public static ChatCoordinator GetObject()
{
if (obj == null)
obj = new ChatCoordinator();
return obj;
}
}

4 To the Project Æ Add a new Item Æ WindowsForm (ChatForm.cs)

public partial class ChatForm : Form


{
ChatCoordinator cc = ChatCoordinator.GetObject(); //Gets the reference of the ChatCoordinator (Singleton)
private void ChatForm_Load(object sender, EventArgs e)
{
//Create the instance of delegate with the method with same sinature as in delegate.
MessageHandler handler = new MessageHandler(ReceivedMessage);
cc.Register(handler);
}
private void btnSend_Click(object sender, EventArgs e)
{
cc.BroadCast(txtSend.Text);
}
//Following method is used for registering the client with the Coordinator using the instances of delegete.
//Hence this is the method called for all clients when one client Broadcasts the message.
private void ReceivedMessage(string message)
{
txtRecieve.Text = message; //Received messate is shown in the textbox.
}
}

2
Deccansoft Software Services Delegates and Events

5 To the Project Æ Add a new Item Æ WindowsForm (MainForm.cs)


public partial class MainForm : Form
{
private void btnNewChatClient_Click(object sender, EventArgs e)
{
ChatForm frmchat = new ChatForm();
frmchat.Show();
}
public static void Main()
{
Application.Run(new MainForm());
}
}
6 Make MainForm as the Starup Object and run the application.

Working with Events


Add a Windows Forms to the Project with the following interface.

Double click on Add Handler button and write the following in the Click Event handler generated:
btnDemo.Click += new EventHandler(btnDemo_Click);
Double click on Remove Handler button and write the following in the Click Event handler generated:
btnDemo.Click -= new EventHandler(btnDemo_Click);
Add the following method to the Form
void btnDemo_Click(object sender, EventArgs e)
{
MessageBox.Show("Demo");
}
Note: Do not double click on Demo button to generate the Click event handler.
When an action is performed on an object it may change its state and this change is usually broadcasted/reflected to
other objects by raising an Event.
An event can be treated as a collection of delegates and when the event is raised, its like iterating through all the
registered delegates and invoking their corresponding methods (event handlers).
Note: Delegates used for events must have Return Type ‘void’ (sub in VB)

7 In the above Program (ChatCoordinator) replace List with Event


public class ChatCoordinator
{
public event MessageHandler ReceivedMessage;
public void BroadCast(string message)
{
//Following code is used for raising the event.
if (ReceivedMessage != null)
ReceivedMessage(message);
}
}
8 In ChatForm_Load (ChatForm.cs) make the following changes
private void ChatForm_Load(object sender, EventArgs e)
{
MessageHandler handler = new MessageHandler(ReceivedMessage);
cc.ReceivedMessage += handler;
}
Note: Remove Register method and the declarion of lstHander

General Syntax for Delegates and Events.


3
Deccansoft Software Services Delegates and Events
Delegate is itself a datatype like a class and event would be a member of the class.
1 Delegate Sub <DN>(<par-list>)
delegate void <DN>(<par-list>)
2 class CA
{
Public Event <EN> as <DN> -- VB
pubic event <DN> <EN>;-- CS

3 void SomeMethod()
{
//some code………
RaiseEvent <EN>(arg-list) -- VB
If (<EN> != null) -- CS
<EN>(args-list)
//some code………
}
}
4 In any place where the above class is used / instantiated.
CA a = new CA();
AddHandler a.<EN>, New <DN>(Address of handler)
a.<EN> += new <DN>(handler) // handler is reference to the method having the same signature as delegate.

RemoveHandler a.<EN>, new <DN>(Addressof handler)


a.<EN> -= new <DN>(handler) // handler is reference to the method having the same signature as delegate.

Anonymous Methods

It’s a method without a name and is used mostly for eventhandlers.

Write the following line in Form’s Load event handler


button1.Click += new EventHandler(button1_Click)

Private void button1_Click(object sender,EventArgs e)


{
MessageBox.Show(“Button is Clicked”);
}
The above line of code can be replaced with anonymous methods in either of the below ways.
button1.Click += delegate (object sender,EventArgs e) button1.Click += delegate //No need to give parameters
{ {
MessageBox.Show(“Button is Clicked”) MessageBox.Show(“Button is Clicked”)
} }

VB.NET event handling using WithEvents / Handles

Dim WithEvents b1 as Button

Private Sub b1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles b1.Click

End Sub

Note: The name of the event hander method can be anything.


An event handler registered using Handles can be unregistered at runtime using RemoveHandler.

Design Pattern for Raising Events

public class AmountEventArgs : EventArgs public class ChatCoordinator


{ {
public readonly decimal Amount; public event MessageHandler ReceivedMessage;
public AmountEventArgs(decimal amount) public void BroadCast(string message)
{ {
4
Deccansoft Software Services Delegates and Events
this.Amount = amount; AmountEventArgs e = new AmontEventArgs(msg);
} OnReceivedMessage(e)
} }
public delegate MessageHndler protected overrides OnReceivedMessage
(object sender, AmountEventArgs e) (AmountEventArgs e)
{
if (ReceivedMessage != null)
ReceivedMessage(this,e);
}
}

5
Deccansoft Software Services – MS.NET n-Tier Arch
Stored Procedures
Create Procedure spGetAllEmps CREATE PROCEDURE spManageEmp
@sortExpression varchar(260), @ActionType tinyint,
@PKID bigint @PKEmpId bigint output,
as @EmpName varchar(50) ,
IF(@sortExpression <> '') @EmpSalary money ,
set @sortExpression = ' Order By ' + @sortExpression @DateOfBirth datetime ,
@IsActive bit
IF @PKID=-1 AS
exec('select * from Emp' + @sortExpression) if @ActionType = 1
ELSE Begin
exec('SELECT * FROM Emp WHERE PKEmpId= ' + @PKID) INSERT INTO Emp
( EmpName ,
EmpSalary ,
Create Procedure spGetEmpCount DateOfBirth ,
As IsActive
Select Count(*) from Emp )
Values
(
Create Procedure spDeleteEmpByPKIDs @EmpName ,
@PKIDs Varchar(8000) AS @EmpSalary ,
If (@PKIDs <> '') @DateOfBirth ,
exec('DELETE FROM Emp WHERE PKEmpId in (' + @PKIDs + ')') @IsActive
)
Select @PKEmpId = @@IDENTITY
End
Else if @ActionType = 2
Begin
UPDATE Emp
Set
EmpName = @EmpName ,
EmpSalary = @EmpSalary ,
DateOfBirth = @DateOfBirth ,
IsActive = @IsActive
WHERE
PKEmpId = @PKEmpId
End
Else if @ActionType = 3
Begin
DELETE FROM Emp WHERE
PKEmpId = @PKEmpId
End

Data Class
Public Class Emp
Private _Validation As String
Public Sub IsValid()
If _Validation <> "" Then
Throw New ApplicationException(_Validation)
End If
End Sub
Public Sub New()
'Setting nulls to AllowNull Fields
End Sub
Public Sub New(ByVal PKEmpId As Int64, ByVal EmpName As String,
ByVal EmpSalary As Decimal, ByVal DateOfBirth As Date, ByVal IsActive As Boolean)
_PKEmpId = PKEmpId

1
Deccansoft Software Services – MS.NET n-Tier Arch

_EmpName = EmpName
_EmpSalary = EmpSalary
_DateOfBirth = DateOfBirth
_IsActive = IsActive
End Sub
Private _PKEmpId As Int64
Public Property PKEmpId() as Int64
Get
Return( _PKEmpId )
End Get
Set(ByVal value As Int64)
If IsDBNull(Value) Then
_Validation &= "Please provide a value for PKEmpId " & vbCrLf
End If
_PKEmpId = value
End Set
End Property
Private _EmpName As String
Public Property EmpName() as String
Get
Return( _EmpName )
End Get
Set(ByVal value As String)
If IsDBNull(Value) Then
_Validation &= "Please provide a value for EmpName " & vbCrLf
End If
_EmpName = value
End Set
End Property
Private _EmpSalary As Decimal
Public Property EmpSalary() as Decimal
Get
Return( _EmpSalary )
End Get
Set(ByVal value As Decimal)
If IsDBNull(value) Then
_Validation &= "Please provide a value for EmpSalary " & vbCrLf
End If
_EmpSalary = value
End Set
End Property
Private _DateOfBirth As Date
Public Property DateOfBirth() as Date
Get
Return( _DateOfBirth )
End Get
Set(ByVal value As Date)
If IsDBNull(value) Then
_Validation &= "Please provide a value for DateOfBirth " & vbCrLf
End If
_DateOfBirth = value
End Set
End Property
Private _IsActive As Boolean
Public Property IsActive() as Boolean
Get
Return( _IsActive )

2
Deccansoft Software Services – MS.NET n-Tier Arch

End Get
Set(ByVal value As Boolean)
If IsDBNull(value) Then
_Validation &= "Please provide a value for IsActive " & vbCrLf
End If
_IsActive = value
End Set
End Property
End Class

Helper Class
Public Enum ActionType
Add = 1
Modify
Delete
End Enum
Public Class Helper
Public Shared ReadOnly Property ConnectionString() As String
Get
Return ConfigurationManager.ConnectionStrings("EmpDB").ToString()
End Get
End Property
End Class

DB Class
Imports System.Data
Imports System.Data.SqlClient

Public Class EmpDB


Public Shared Function ManageEmp(ByVal trans As SqlTransaction, ByVal objEmp As Emp,
ByVal ActionType As ActionType) As Object
Dim spName As String = "spManageEmp"
Dim pActionType As New SqlParameter("@ActionType", SqlDbType.TinyInt)
Dim pPKEmpId As SqlParameter = New SqlParameter("@PKEmpId", SqlDbType.BigInt)
Dim pEmpName As SqlParameter = New SqlParameter("@EmpName", SqlDbType.VarChar, 50)
Dim pEmpSalary As SqlParameter = New SqlParameter("@EmpSalary", SqlDbType.Money)
Dim pDateOfBirth As SqlParameter = New SqlParameter("@DateOfBirth", SqlDbType.DateTime)
Dim pIsActive As SqlParameter = New SqlParameter("@IsActive", SqlDbType.Bit)
If ActionType = ActionType.Add Then
pActionType.Value = ActionType.Add
pPKEmpId.Direction = ParameterDirection.Output
ElseIf ActionType = ActionType.Modify Then
pActionType.Value = ActionType.Modify
pPKEmpId.Value = objEmp.PKEmpId
ElseIf ActionType = ActionType.Delete Then
pActionType.Value = ActionType.Delete
pPKEmpId.Value = objEmp.PKEmpId
End If
If ActionType = ActionType.Add OrElse ActionType = ActionType.Modify Then
pEmpName.Value = objEmp.EmpName
pEmpSalary.Value = objEmp.EmpSalary
pDateOfBirth.Value = objEmp.DateOfBirth
pIsActive.Value = objEmp.IsActive
End If
Dim AffectedRows As Integer = 0
If trans Is Nothing Then
AffectedRows = SqlHelper.ExecuteNonQuery(Helper.ConnectionString, CommandType.StoredProcedure,

3
Deccansoft Software Services – MS.NET n-Tier Arch

spName, pActionType, pPKEmpId, pEmpName, pEmpSalary, pDateOfBirth, pIsActive)


Else
AffectedRows = SqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure,
spName, pActionType, pPKEmpId, pEmpName, pEmpSalary, pDateOfBirth, pIsActive)
End If
If ActionType = ActionType.Add Then
Return CType(pPKEmpId.Value, Int64)
Else
Return AffectedRows
End If
End Function

Public Shared Function GetAllEmps(ByVal sortExpression As String) As DataSet


Dim spName As String = "spGetAllEmps"
Dim psortExpression As New SqlParameter("@sortExpression", SqlDbType.VarChar, 260)
Dim pPKID As New SqlParameter("@PKId", SqlDbType.BigInt)
psortExpression.Value = sortExpression
pPKID.Value = -1 'this is needed because we use that for GetByPKID
Return SqlHelper.ExecuteDataset(Helper.ConnectionString, CommandType.StoredProcedure,
spName, psortExpression, pPKID)
End Function

Public Shared Function GetEmpByPKID(ByVal PKID As Int64) As Emp


Dim spName As String = "spGetAllEmps"
Dim psortExpression As New SqlParameter("@sortExpression", SqlDbType.VarChar, 260)
Dim pPKID As New SqlParameter("@PKID", SqlDbType.BigInt)
psortExpression.Value = ""
pPKID.Value = PKID
Dim drRow As SqlDataReader = SqlHelper.ExecuteReader(Helper.ConnectionString, CommandType.StoredProcedure,
spName, psortExpression, pPKID)
Dim objEmp As Emp = Nothing
If drRow.HasRows Then
drRow.Read()
objEmp = New Emp(CType(drRow("PKEmpId"), Int64), CType(drRow("EmpName"), String),
CType(drRow("EmpSalary"), Decimal), CType(drRow("DateOfBirth"), Date), CType(drRow("IsActive"), Boolean))
End If
drRow.Close()
Return objEmp
End Function

Public Shared Function GetEmpCount() As Integer


Dim spName As String = "spGetEmpCount"
Return CInt(SqlHelper.ExecuteScalar(Helper.ConnectionString, CommandType.StoredProcedure, spName))
End Function

Public Shared Sub DeleteEmpByPKIDs(ByVal PKIDs As String)


Dim spName As String = "spDeleteEmpByPKIDs"
Dim pPKIDs As New SqlParameter("@PKIDs", SqlDbType.Text)
pPKIDs.Value = PKIDs
SqlHelper.ExecuteNonQuery(Helper.ConnectionString, CommandType.StoredProcedure, spName, pPKIDs)
End Sub
End Class

BO Class
Public Class EmpBO
Public Function InsertEmp(ByVal objEmp As Emp) As Integer
objEmp.IsValid()

4
Deccansoft Software Services – MS.NET n-Tier Arch

Return EmpDB.ManageEmp(nothing, objEmp, ActionType.Add)


End Function
Public Sub UpdateEmp(ByVal objEmp As Emp)
EmpDB.ManageEmp(nothing, objEmp, ActionType.Modify)
End Sub
Public Sub DeleteEmp(ByVal objEmp As Emp)
EmpDB.ManageEmp(Nothing, objEmp, ActionType.Delete)
End Sub
Public Function GetAllEmps(ByVal sortExpression As String) As DataSet
return EmpDB.GetAllEmps(sortExpression)
End Function
Public Function GetEmpByPKID(ByVal PKID As Long) As Emp
Dim objEmp As Emp = EmpDB.GetEmpByPKID(PKID)
Return objEmp
End Function
Public Sub DeleteEmpByPKIDs(ByVal PKIDs As String)
EmpDB.DeleteEmpByPKIDs(PKIDs)
End Sub
End Class
App.config
<configuration>
<connectionStrings>
<add name="EmpDB" connectionString="server=.\sqlexpress;database=EmpDB;integrated security=true" />
</connectionStrings>
</configuration>

EmpForm
Private Sub BindDataToGrid()
Dim ds As DataSet
Dim objEmpBo As New EmpBO
ds = objEmpBo.GetAllEmps("")
gvEmp.DataSource = ds
gvEmp.DataMember = "Table"
End Sub
Private Sub EmpForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gvEmp.SelectionMode = DataGridViewSelectionMode.FullRowSelect
gvEmp.AutoGenerateColumns = False
gvEmp.ReadOnly = True
BindDataToGrid()
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
Dim dlgEmp As New EmpDialog

5
Deccansoft Software Services – MS.NET n-Tier Arch

If (dlgEmp.ShowDialog = Windows.Forms.DialogResult.OK) Then


Dim objEmp As New Emp(-1, dlgEmp.EmpName, dlgEmp.EmpSalary, dlgEmp.DateOfBirth, dlgEmp.IsActive)
Dim objEmpBO As New EmpBO
objEmp.EmpId = objEmpBO.InsertEmp(objEmp)
BindDataToGrid()
End If
End Sub
Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
Dim empId As Integer
empId = CInt(gvEmp.SelectedRows(0).Cells(0).Value)
Dim dlgEmp As New EmpDialog
Dim objEmpBO As New EmpBO
Dim objEmp As Emp = objEmpBO.GetEmpByPKID(empId)
dlgEmp.EmpName = objEmp.EmpName
dlgEmp.EmpSalary = objEmp.EmpSalary
dlgEmp.IsActive = objEmp.IsActive
dlgEmp.DateOfBirth = objEmp.DateOfBirth
If (dlgEmp.ShowDialog = Windows.Forms.DialogResult.OK) Then
objEmp.EmpName = dlgEmp.EmpName
objEmp.EmpSalary = dlgEmp.EmpSalary
objEmp.IsActive = dlgEmp.IsActive
objEmp.DateOfBirth = dlgEmp.DateOfBirth
objEmpBO.UpdateEmp(objEmp)
BindDataToGrid()
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim empId As Integer
empId = CInt(gvEmp.SelectedRows(0).Cells(0).Value)
Dim objEmpBO As New EmpBO
objEmpBO.DeleteEmp(empId)
BindDataToGrid()
End Sub

EmpDialog
Public Property EmpName() As String
Get
Return txtName.Text
End Get
Set(ByVal value As String)
txtName.Text = value
End Set
End Property
Public Property EmpSalary() As Decimal
Get
Return Decimal.Parse(txtSalary.Text)
End Get
Set(ByVal value As Decimal)
txtSalary.Text = value.ToString
End Set
End Property
Public Property DateOfBirth() As Date
Get
Return dtpDOB.Value
End Get
Set(ByVal value As Date)
dtpDOB.Value = value

6
Deccansoft Software Services – MS.NET n-Tier Arch

End Set
End Property
Public Property IsActive() As Boolean
Get
Return chkIsActive.Checked
End Get
Set(ByVal value As Boolean)
chkIsActive.Checked = value
End Set
End Property
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
'Validate data in controls
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub

7
Window3.xml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPFApplication1.Window3" Title="Window3" Height="383" Width="423" Background="#cccccc">
<Window.Resources>
<LinearGradientBrush x:Key="lb" StartPoint="0.5,0" EndPoint="1,0.5">
<GradientStop Color="Red" Offset="0"/>
<GradientStop Color="Green" Offset="0.5"/>
<GradientStop Color="Blue" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="title" StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="Green" Offset="0"/>
<GradientStop Color="Orange" Offset="0.5"/>
<GradientStop Color="Yellow" Offset="1"/>
</LinearGradientBrush>
<Style x:Key="body" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Yellow"/>
<Setter Property="FontSize" Value="15pt"/>
</Style>
<Style x:Key="heading" BasedOn="{StaticResource body}" TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
<XmlDataProvider Source="MyImages.xml" x:Key="MyImages" XPath="Pictures/Pic"/>
<DataTemplate DataType="Pic">
<Border Grid.RowSpan="2" BorderThickness="3" BorderBrush="Blue" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="{Binding XPath=@src}" Width="100" Height="100" Stretch="Fill"/>
<Border.LayoutTransform>
<RotateTransform Angle="5"/>
</Border.LayoutTransform>
</Border>
</DataTemplate>
</Window.Resources>
<Grid Background="{StaticResource lb}">
<Grid.Resources>
<Style TargetType="{x:Type Button}" x:Key="TopImage">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowSize="6"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
1
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Border Grid.RowSpan="2" BorderThickness="3" BorderBrush="Blue" HorizontalAlignment="Center" VerticalAlignment="Top">
<Image Source="Dell.bmp" Width="100" Height="100" Stretch="Fill"/>
<Border.LayoutTransform>
<RotateTransform Angle="5"/>
</Border.LayoutTransform>
</Border>
<TextBlock Background="{StaticResource title}" Style="{StaticResource heading}" Grid.Row="0" Grid.Column="1">
This is my First WPF Demo</TextBlock>
<ScrollViewer Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto" Margin="0,94,0,0">
<ListBox ItemsSource="{Binding Source={StaticResource MyImages}}"></ListBox>
</ScrollViewer>
<GridSplitter HorizontalAlignment="Right" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Width="5"/>
<TextBlock Style="{StaticResource body}" Grid.Row="1" Grid.Column="1" >PlaceHolder for text</TextBlock>

<TabControl Grid.Row="1" Grid.Column="1" Margin="0,30,0,0" Name="tabControl1" Foreground="#FFFFFFFF"


BorderBrush="#FF00FFFF" BorderThickness="3,3,3,3" Background="{x:Null}">
<TabItem Header="Add/Sub">
<TabItem.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFF2CD14" Offset="0"/>
<GradientStop Color="#FFF21247" Offset="1"/>
<GradientStop Color="#FF4116F5" Offset="0.389"/>
<GradientStop Color="#FFF5164B" Offset="0.74"/>
<GradientStop Color="#FF4116F5" Offset="0.389"/>
</LinearGradientBrush>
</TabItem.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="0.805346977934122*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="0">
<Label VerticalAlignment="Center" Content="N1"/>
<TextBox x:Name="txtN1" Text="100" Height="30" Width="30">
<TextBox.LayoutTransform>
2
<SkewTransform AngleX="-20" AngleY="-20" />
</TextBox.LayoutTransform>
</TextBox>
<Label VerticalAlignment="Center" Content="N2"/>
<TextBox x:Name="txtN2" Text="10" Height="30" Width="30">
<TextBox.LayoutTransform>
<SkewTransform AngleX="-20" AngleY="-20" />
</TextBox.LayoutTransform>
</TextBox>
</StackPanel>
<Button x:Name="btnAdd" Click="Button_Click" Height="23" Grid.Row="1" VerticalAlignment="Top"
RenderTransformOrigin="0.524,3.768" Foreground="#FF54E407" Content="Add">
<Button.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button x:Name="btnSub" Click="Button_Click" VerticalAlignment="Top" Height="23" Margin="0,24,0,0" Grid.Row="1"
Content="Sub"/>
<Canvas RenderTransformOrigin="0.5,0.5" x:Name="MyButton" Canvas.Left="28" Canvas.Top="108" Background="#FF000000"
Margin="75.0824266355116,90.2766666666666,63.0824266355116,54.5833333333334" Grid.Row="1">
<Canvas.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard x:Name="Timeline1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="-9"/>
<SplineDoubleKeyFrame KeyTime="00:00:03" Value="24"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="textBlock" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
</DoubleAnimationUsingKeyFrames>

3
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.892"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.889"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000"
Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="8"/>
<SplineDoubleKeyFrame KeyTime="00:00:03" Value="7"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="13"/>
<SplineDoubleKeyFrame KeyTime="00:00:03" Value="-13"/>
</DoubleAnimationUsingKeyFrames>
<PointAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MyButton"
Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)">
<SplinePointKeyFrame KeyTime="00:00:00" Value="0.7,0.75"/>
<SplinePointKeyFrame KeyTime="00:00:03" Value="0.5,0.5"/>
</PointAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF55BD4B"/>
<SplineColorKeyFrame KeyTime="00:00:03" Value="#FFF01010"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="8" AngleY="6"/>
<RotateTransform Angle="-9"/>
<TranslateTransform X="-1" Y="0"/>
</TransformGroup>
</Canvas.RenderTransform>
<Rectangle Width="147" Height="65" Fill="#FF55BD4B" Stroke="#FF000000" RadiusX="24.5" RadiusY="24.5"
4
RenderTransformOrigin="0.5,0.5" x:Name="rectangle">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Width="128" Height="47" Stroke="#FF000000" RadiusX="24.5" RadiusY="24.5" Canvas.Left="9"
Canvas.Top="9" RenderTransformOrigin="0.5,0.5" x:Name="rectangle1">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFF21010" Offset="0"/>
<GradientStop Color="#FF0A41F2" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
<TextBlock Width="46.5" Height="21" Canvas.Left="47.5" Canvas.Top="22" Text="Demo" TextWrapping="Wrap"
RenderTransformOrigin="0.5,0.5" x:Name="textBlock">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</Canvas>
</Grid>
</TabItem>
<TabItem Header="Tab2"><TabItem.Content>This is Content of Tab1</TabItem.Content></TabItem>
</TabControl>
</Grid>
</Window>
5
APP.XML

<Application x:Class="WPFApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window3.xaml">
<Application.Resources>
<Style x:Key="lblStyle">
<Setter Property="Label.Background" Value="red"/>
</Style>
<Style TargetType="{x:Type Button}" x:Key="GlowButton">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowSize="6"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
</Application>
MyImages.xml

<Pictures>
<Pic title="Blue Lace 16" src="Images/Blue Lace 16.bmp" href=""/>
<Pic title="Coffee Bean" src="Images/Coffee Bean.bmp" href=""/>
<Pic title="FeatherTexture" src="Images/FeatherTexture.bmp" href=""/>
<Pic title="Gone Fishing" src="Images/Gone Fishing.bmp" href=""/>
<Pic title="Prairie Wind" src="Images/Prairie Wind.bmp" href=""/>
<Pic title="Zapotec" src="Images/Zapotec.bmp" href=""/>
</Pictures>

Anda mungkin juga menyukai