Windows
Windows
Windows
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.
.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.
1
Deccansoft Software Services – MS.NET Overview of .NET Framework
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 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.
.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.
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 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
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.
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.
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.
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.
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)
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.
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.
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
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.
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.
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.
Operators
Arithmetic Operators: +, - , *, / (Floating Type Division), \ (Integral Division), Mod (Modulus / Remainder), ^ (Power),
+=, -=, *=, ^=, /=, \=
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
• 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.
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
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.
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.
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.
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.
1
Deccansoft Software Services C# - Language Basics
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 * , -> , [] , &
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
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)
{
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}};
• 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
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.
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.
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
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”);
}
}
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());
}
}
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.
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.
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”.
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.
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.
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.
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 }
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
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
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).
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.
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”.
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”.
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.
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”
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.
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 }
}
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.
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
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.
Note: ReadOnly field members of a class can be initialized only in the constructor of the class.
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:
• 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.
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”))
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
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.
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
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.
<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);
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
4
Deccansoft Software Services – MS.NET Unsafe Code, Operator Overloading, Reflection,Attributes & Config file
<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
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
Deccansoft Software Services – MS.NET Working with Assemblies and GAC
.
• The same signing key file can be used for signing of all the assemblies developed by a given developer or organization.
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
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.
4
Deccansoft Software Services – MS.NET ADO.NET
Client Process
Application
Managed DataSet
Provider
DataAdapter
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.
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
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.
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.
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)
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.
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)
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
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.
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
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.
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 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.
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.
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.
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
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
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.
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 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.
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
2
Deccansoft Software Services – MS.NET XML
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.
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.
5
Two types of Build
Edit & Continue: In Break Mode we can edit the code, without stopping Debugging.
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
Debug > Attach Process > Select the needed application from the list > Click on Attach
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
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
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
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
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.
Now make the Controller Application as the startup Project and Run it.
2
Deccansoft Software Services – MS.NET Window Forms
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.
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
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
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.
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)
3
Deccansoft Software Services – MS.NET Window Forms
l.P2 = e.Location
lstLine.Add(l)
Invalidate()
End If
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
5
Deccansoft Software Services – MS.NET Window Forms
7
Deccansoft Software Services – MS.NET Window Forms
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub TileVertToolStripMenuItem_Click(. . .)
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub
8
Deccansoft Software Services – MS.NET Window Forms
OKClick(this, EventArgs.Empty); {
} OnCancelClick();
private void btnOK_Click(object sender, EventArgs e) }
{
OnOKClick();
}
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);
Note: In VB.NET following syntax should be used to create the instance of delegate (use Addressof).
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);
2
Deccansoft Software Services Delegates and Events
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)
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.
Anonymous Methods
End Sub
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
3
Deccansoft Software Services – MS.NET n-Tier Arch
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
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
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>
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>