Best Dot Net Meterial,Dot Net Meterial,Best C# Meterial,Best C sharp Meterial,mbr material,mbr c# material,Total Meterial,Total Dot Net by mbrinfo

VIEWS: 271 PAGES: 115

More Info
									Deccansoft Software Services – MS.NET

  S No Content
   01     MSNET Framework

   02     ConsoleApplication VB Examples

   03     Language Basics_c#

   04     Language Basics_VB

   05     OOPS Concepts

   06     Inheritance

   07     Interface

   08     Exception Handling

   09     Collections

   10     Miscellaneous Topics

   11     Walkthrough for Assemblies_Without Screens

   12     IOExamples

   13     Multithreading

   14     ADO DOTNET 01

   15     ADO DOTNET 02

   16     XML

   17     Windows Service

   18     N-Tier App

   19     WinForms

   20     Delegate & Events

Deccansoft Software Services – MS.NET

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
     • Preserve consistency between objects that are stored and executed locally, objects that are
          Internet-distributed but are executed locally, and objects that are stored and executed remotely on
          a server.
     • Avoid deployment and versioning conflicts. No DLL update upon deployment. Solves the DLL
          Hell Syndrome.
     • Guarantee safe execution of code, even if written by unknown semi-trusted third party. Avoid the
          slowness of scripted or interpreted languages.
     • Preserve development environment consistency between windows-based applications and web-
          based applications.

The .NET framework includes two main components:
            1. The .NET Class library
            2. The Common Language Runtime.

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

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

The most important namespaces in the .NET class library are:
• System
• System.IO
• System.Collections
• System.Threading
• System.Reflection
• System.Security
• System.Net
• System.Data
• System.XML
• System.Web
• System.Web.Services
• System.Windows.Forms
• System.Drawing
• System.Globalization
• System.Resources

Deccansoft Software Services


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

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

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

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

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

You can write IL programs directly, for example:

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

Deccansoft Software Services

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)

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

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

What is Type safety?
Defining the data types in a language to be specific and strict in their behaviour.

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

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

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

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

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

Deccansoft Software Services

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

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

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

NOTE: CLS is not a part of CLR Specification.

Few .NET Framework Supported Languages:

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

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

There are three types of JIT compilers
    • Standard JIT
    • Econo JIT = Standard JIT – Caching (this is ideal for small devices)
    • Pre JIT

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

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

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

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

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

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

Deccansoft Software Services

Garbage Collector a background thread running within the application / CLR and is responsible for destroying all the
unreferenced objects (objects which are no longer in use). When garbage collector is initiated all other threads running
within the application are temporarily suspended and hence the sooner the garbage collector finishes its job the better
would be the performance of the application.

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

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

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

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

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

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

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

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

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

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

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

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

The manifest: (also called as Assembly Metadata)
    • Establishes the assembly identity (in the form of a text name), version, culture, and digital signature (if the
       assembly is to be shared across applications).

Deccansoft Software Services

    •    Defines what files make up the assembly implementation.
    •    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

            Assembly Manifest
            (Meta Data)

            Type Meta Data

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


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.

Deccansoft Software Services

     ConsoleApplication VB Examples
Program to check the range of a datatype
Public Class Max_Min_Values
  Public Shared Sub Main()
  End Sub
End Class

Program for type-casting
Public Class Type_Casting
  Public Shared Sub Main()
     Dim n As Integer = 50
     Dim s As Short = 5
     Dim str As String = "100"
     Dim m As Integer

    'm = CInt(str)
    'm = Integer.Parse(str)
    'm = Convert.ToInt32(str)
    ' Dim bl As Boolean

    ' To check if string is convertible to integer and try parsing
    ' bl = Integer.TryParse(str, m)
    m += 1

    'str = CStr(n)
    'str = n.ToString
    str = n & ""
    str = str + " Days"
  End Sub
End Class

Program to demonstrate In-Built Ascii Function:
Public Class Ascii_Functions
  Public Shared Sub Main()
  End Sub
End Class
Program to demonstrate Boxing and UnBoxing
Public Class Boxing
  Public Shared Sub Main()
     Dim n As Integer = 65
     Dim o As Object
     o = n 'This is Boxing
     n = CInt(o) ' This is UnBoxing
  End Sub
End Class

Program to demonstrate IIF function an alternative to

Deccansoft Software Services

Ternary operator
Option Strict On
Public Class IIF_Example
  Public Shared Sub Main()
     Dim n, m, o As Integer
     n = 10
     o = CInt(IIf(m < 10, m, n))
  End Sub
End Class

Program to demonstrate GoTo statement
Public Class Label_Example
  Public Shared Sub Main()
     GoTo Five
  End Sub
End Class

Program to demonstrate Select statement
Public Class Select_Example
  Public Shared Sub Main()
     Dim n As Integer = 1
     Select Case n
       Case 1
       Case 2, 3
                  Console.WriteLine(2 & " & " & 3)
       Case Is < 6
                  Console.WriteLine("Less Than 6")
       Case 6 To 9
                  Console.WriteLine("6 To 9")
                  If (n = 7) Then Exit Select
                     Console.WriteLine("Not 7")
         Case Else
                     Console.WriteLine("Else Case")
     End Select
  End Sub
End Class

Program to demonstrate While loop
Public Class While_Example
  Public Shared Sub Main()
     Dim n As Integer = 5
     While (n > 0)
     End While

Deccansoft Software Services

    Do While (n < 5)
    Loop While (n > 0)
  End Sub
End Class

Program to demonstrate Do Until..Loop
Public Class Until_Example
  Public Shared Sub Main()
     Dim n As Integer
     Do Until (n = 5)
     Loop Until (n = 0)
  End Sub
End Class

Program to generate tables from 1 to 5 using For loop
Class table
  Public Shared Sub Main()
     Dim n, m As Integer
     For n = 1 To 10
        For m = 1 To 5
          Console.Write(m & "*" & n & "=" & n * m &
  End Sub
End Class

Program to generate a triangle using For loop
Class triangle
  Public Shared Sub Main()
     Dim n, m, o As Integer
     For n = 10 To 1 Step -1
        For o = 0 To n - 1
           Console.Write(" ")
        For m = n To 10
           Console.Write("* ")
  End Sub
End Class

Program to demonstrate Arrays
Public Class Array_Example
  Public Shared Sub Main()

Deccansoft Software Services

    Dim ar() As Integer = {1, 2, 3, 4, 5}
    For Each tmp As Integer In ar
    ReDim ar(10)
    For Each tmp As Integer In ar
    'ReDim Preserve ar(10)
    ‘For Each tmp As Integer In ar
  End Sub
End Class

Program to demonstrate Function Overloading
Public Class Functions_Example
   Public Shared Sub Main()
     Dim m, n As Integer
     Dim o As Short = 12
     m = 10 : n=5
     'Console.WriteLine(Add("Hello", "Harish"))
     'Console.WriteLine(m, n)
     'Console.WriteLine(Add(o, 5))
     Console.WriteLine(Add(b:=10, a:=50))
   End Sub
   Shared Function Add(ByVal a As Short, ByVal b As
Integer) As Integer
     Add = a + b
   End Function
   Shared Function Add(ByVal a As Integer, ByVal b As
Integer) As Integer
     Add = a + b
   End Function
   Shared Function Add(ByVal a As String, ByVal b As String)
As String
     Add = a & b
   End Function
End Class

Program to demonstrate static variable
Public Class Static_Local_Variable
  Public Shared Sub Main()
     Foo ()
     Foo ()
     Foo ()
  End Sub
  Shared Sub Foo()
     Static Dim a As Integer
     a += 1
  End Sub
End Class

Program to demonstrate Call-By-Value and Call-By-
Public Class ByRef_ByVal

Deccansoft Software Services

  Public Shared Sub Main()
    'Dim m, n As Integer
    'm = 5
    'n = 10
    '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():")
    Console.WriteLine(ar1(0) & " " & ar2(0))
    Console.WriteLine(ar1(1) & " " & ar2(1))
  End Sub
  Shared Sub foo(ByVal a As Integer, ByRef b As Integer)
    a += 1
    b += 1
  End Sub
  Shared Sub foo(ByVal a() As Integer, ByRef b() As Integer)
    ReDim a(1)
    ReDim b(1)
  End Sub
End Class

Program to demonstrate passing an array of fixed length to
a function:
Public Class Array_Sum
   Public Shared Sub Main()
     Dim a() As Integer = {1, 2, 3, 4, 5}
   End Sub
   Shared Function Add(ByVal a() As Integer) As Integer
     For Each a1 As Integer In a
        Add += a1
   End Function
End Class

Program to demonstrate passing an arrays of variable
length to a function

Public Class ParamArray_Example
   Public Shared Sub Main()
     Dim m, n, o As Integer
     n = 10
     o = 15
     Console.WriteLine(Add(m, n, o, 2, 3, 4, 5, 6, 7, 8, 9, 1))
   End Sub
   Shared Function Add(ByVal ParamArray a() As Integer) As
     For Each tmp As Integer In a
        Add += tmp
     Next End Function End Class

Deccansoft Software Services

Language Basics_c#
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
• byte, short, char types when used in an expression are automatically raised to the rank of Integer.

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

Deccansoft Software Services

     //int to string
     s = n.ToString();
     s = n + "";
     s = Convert.ToString(n);

     //long to float
     long lng = 10;
     f = lng; //Long can be casted to float
     f = (float) lng;

     object obj = n; //Boxing
     n = (int)obj; //Unboxing

     const double PI = 3.14;

      byte b1, b2, b3;
      b1 = b2 = b3 = 10;
      b1 = b2 + b3 //Compilation Error
//if byte, short or char are used in an expression they are
automatically raised to the rank of int.
      b1 = (byte) (b2 + b3);

     Console.WriteLine(3 / 2); //Result 1
     Console.WriteLine(3.0 / 2); //Result 1.5

     n = 10;
     int m = n++; //m=n; n=n+1 - Post Increment
     Console.WriteLine(n + " " + m);
     m = ++n; //n=n+1; m=n - Pre Increment
     Console.WriteLine(n + " " + m);

     //?: Conditional Operator
     int max;
     max = n > m ? n : m; //?: Operator

    //Enum Sample
     n = 2; //n is int
     WeekDays wd = WeekDays.Sat;
     wd = (WeekDays) n;
     //n = wd; //Invalid
     n = (int) wd; //In VB.NET casting is not required
enum WeekDays : int
  Sun=1, Mon, Tues, Wed, Thus, Fri, Sat
Enum is a subtype of any integral type.

MS.NET -                                                                                            Object Oriented Concepts

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

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

Note for VB.NET Programmers:
• In C# the arithmetic operator ‘/ ’ is the same for both floating point and integer datatypes and the result depends on the

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)

MS.NET -                                                                                            Object Oriented Concepts
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)

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

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

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

class Program
     static void Main(string[] args)

       int[] ar = new int[ ] {1,2,3,4};
        foreach (int n in ar)

MS.NET -                                                                                           Object Oriented Concepts
//To increase the size of existing array and also retaining data in it. i.e equivalent of Redim Preserve in VB.
          int[] temp =ar;
          ar = new int[temp.Length +1];
          Array.Copy(temp, ar, temp.length);
          foreach (int n in ar)
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[0]=new int[] {1,2,3,4};
myJaggedArray[0]=new int[] {1,2};
You can also initialize the array upon declaration like this
int[][] myJaggedArray = {new int[]{1,2,3,4,5},new int[]{1,2,3,4}, new int[]{1,2}};
int[][] myJaggedArray;
myJaggedArray = new int[][] {new int[]{1,2,3,4,5},new int[]{1,2,3,4},new int[]{1,2}};

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

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

MS.NET -                                                                                    Object Oriented Concepts

Language Basics_VB
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
Object *                 System.Object           Generic Datatype
Date          8          System.DateTime

All the above datatypes are ValueTypes but String and Object are Reference Types.

MS.NET -                                                                                             Object Oriented Concepts
Value Types directly hold the value-
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

When a value is boxed to an object type the object type cannot be used in mathematical operations

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

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

str = "65"                                                'Casting between integer and bool
'n = s 'Invalid                                           'n = bln 'Invalid
's = n 'Invalid                                           'bln = n 'Invalid
n = CInt(str)                                             n = CInt(bln) 'Explicit casting
n = Integer.Parse(str)                                    bln = CBool(n) 'Explicit casting
n = Convert.ToInt32(str)
If (Integer.TryParse(str, n)) Then                        ‘Casting any datatype to string
     Console.WriteLine("Success")                         str = n.ToString()
Else                                                      str = n & ""
     Console.WriteLine("Failed")                          str = CStr(n)
End If                                                    str = Convert.ToString(n)
Constant Declaration
Const PI As Double = 3.14
'PI = 10 'Invalid – The value of constant cannot be changed.
Note: It is recomemded to have the name of constant variable always in upper case.

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

Enum WeekDays as Integer           Dim wd As WeekDays
  Sun = 1                          wd = WeekDays.Tues
  Mon                              n=1
  Tues                             'wd = 1 ‘Invalid – Casting is needed.
  Wed                              wd = CType(n, WeekDays) 'explicit casting
  Thus                             n = wd 'implicit casting

MS.NET -                                                                                             Object Oriented Concepts
  Fri                              Console.WriteLine(wd) ‘Prints the Numeric Value
  Sat                              Console.WriteLine(wd.ToString()) ‘Print the String Value
End Enum

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

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

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

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

Dim sb As New System.Text.StringBuilder(100)                       Modified value of string must be assinged to it again otherwise
Console.WriteLine(sb.Capacity)                                     the value of it doesn’t change
For i As Integer = 0 To 50                                         str = "Demo String"
   sb.Append(i)                                                    str.Replace(" ", "-") ‘Incorrect Usage – str doesn’t change.
Next                                                               Console.WriteLine(str)
Console.WriteLine(sb.Capacity)                                     str = str.Replace(" ", "-") ‘Correct Usage
str = sb.ToString()                                                Console.WriteLine(str)

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

• /, ^= & /= Operators can be only used with float variables.
• Dividing a Integral Type with zero throws DivideByZeroException
Floating Point Division by zero is not a runtime exception but the value would be either PositiveInfinity (if numerator is greater
than zero) or NegativeInfinity (if nuderator is less than zero) or NaN(if numerator is also zero)

Dim n as Integer
Dim m as Double = -4.0 / n
If (Double.IsNaN(m)) Then Console.WriteLine(“Not a Number”)
ElseIf (Double.IsPositiveInfinity(m)) Then Console.WriteLine("Positive Infinity")
ElseIf (Double.IsNegativeInfinity(m)) Then Console.WriteLine("Negative Infinity")

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

•   Is and IsNot are used to compare two reference types
    Ex: (a1 Is a2) is true if both a1 and a2 are referencing to the same object.
    (a1 IsNot a2) is true if both a1 and a2 are not referencing to the same object.

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

MS.NET -                                                                                                Object Oriented Concepts
Relational Operators: <, >, <=, >=, <>, = (for both equal and assignment operator)

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

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

•   a = b = c will not be allowed for any datatype other than Boolean.
•   Note: Operators not supported in VB.NET: ++, --, ?:, <<. >>

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                                   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
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)
  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

With Statement

MS.NET -                                                                                           Object Oriented Concepts
                                        With (args)                             Its not supported in C#
Console.WriteLine(args.Length)           Console.WriteLine(.Length)
Console.WriteLine(args.Rank)             Console.WriteLine(.Rank)
                                        End With

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

Dim ar(X) as Integer ‘X is the variable
Dim ar(10) As Integer ‘Declared and Initialized and array of lengh 11
Dim ar(0 To 10) As Integer

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

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) ‘6
Console.WriteLine(ar.Rank) ‘2 – Gives the total number of dimensions
Console.WriteLine(ar.GetLength(0)) ‘ 3 – Gives the number of elements in 1st dimension.
Console.WriteLine(ar.GetLength(1)) ‘ 4 – Gives the number of elements in 2nd dimension.

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

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

                                                   Workiing with Methods
Types of Procedues :
1. Sub Procedures – Do not return any value
2. Function Procedures – Returns a value.
3. Property Procedures – To be covered in OOPS

MS.NET -                                                                                           Object Oriented Concepts
In VB.NET the name of the function is treated as Variable in that function and the last value assigned to that becomes the return
value of that function.

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

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

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

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

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

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

In Main Before Foo is Called           In Foo                                       In Main After Foo is returned
n1 = 10                                a = 10 11                                    n1 = 10
n2 = 10                                n1 = 10                                      n2 = 11
                                       b/n2 = 10 11

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

General Points:
   • “:” is the Statement terminator ex: a=1 : b=2
   • “_” is the Statement Continuation char
        Ex: a = b _
   • 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: “This is a ""Quoted"" word”

MS.NET -        Object Oriented Concepts

OOPS Concepts
MS.NET -                                                                                             Object Oriented Concepts


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

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

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

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

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

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

Every object oriented language should have three features
   • Encapsulation
   • Inheritance
   • Polymorphism

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

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
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
A faculty can take a form of Java Faculty or MSNET faculty and in both the forms he teaches, but what he teaches differs. Here
functionality is teach and faculty is the object.

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

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

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

MS.NET -                                                                                           Object Oriented Concepts

Code in VB:                                            Code in C#:
Public Class Account                                   class Account
        Public Id As Integer                           {
        Public Name As String                              public int Id;
        Public Balance As Decimal                          public String Name;
        Public Sub New()                                   public Decimal Balance;
              MsgBox("Object created")                     public Account()
        End Sub                                            {
        Protected Overrides Sub Finalize()                      MessageBox.Show(“Object Created”);
              MsgBox("Object Destroyed")                   }
        End Sub                                            ~Account()
End Class                                                  {
                                                                 MessageBox.Show(“Object Destroyed”);

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

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

In the AccountApplication, Change the name of the Form, “Form1” to “AccountForm”
                                        a.Id = 1
                                        The above statement can be read as: “Id” menber of an object of type Account
                                        referenced by “a” is set to 1.

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

                                          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 = ""                                               {

MS.NET -                                                                                           Object Oriented Concepts
      txtBalance.Text = ""                                                 txtId.Text = "";
    End Sub                                                                txtName.Text = "";
                                                                           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                                                            {
    Private Sub btnTemp_Click(...) Handles btnTemp.Click               }
      Dim a1 = New Account()                                           private void btntemp_Click(...)
      a = a1                                                           {
    End Sub                                                                Account a1 = new Account();
                                                                           a = a1;
  Private Sub btnGetGeneration_Click(...) Handles btnGC.Click          }
    MsgBox(GC.GetGeneration(a))                                        private void btnGetGeneration_Click(...)
  End Sub                                                              {
End Class                                                                  MessageBox.Show(GC.GetGeneration(a).ToString());

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

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

MS.NET -                                                                                           Object Oriented Concepts
  End If                                                                 this.Balance -= Amount
End Sub                                                          }

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

Private Sub btnDeposit_Click( . . . )                                Private Sub btnWithraw_Click( . . . )
    a.Deposit(CDec(txtAmount.Text))                                     a.Withdraw(CDec(txtAmount.Text))
End Sub                                                              End Sub

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

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

Syntax in VB:                                                                Syntax in C#:
Private _property name as <Datatype>                                         Private <Datatype> _<propertyname>;
Public [ReadOnly/WriteOnly] Property <property name> as <Datatype>           public <Datatype> <PropertyName>
         [Private] Get                                                       {
                  Return _<property name>                                             [private] get
         End Get                                                                      {
         [Private] Set (ByVal Value as < Datatype >)                                           return _propertyname;
                  _<property name> = Value                                            }
         End Set                                                                      [private] set
End Property.                                                                         {
                                                                                               _propertyname = value;

Deccansoft Software Services – MS.NET
1. Let every private member of a class begins with underscore ( _ ).
2. Property in a class mostly wraps around the field member of the same class and thus they can be used for giving restricted
and valid access to the field member.

Add the properties for Name, Balance and Id with following conditions:
     • Name should accept less than or equal 8 characters only
     • Balance should be ReadOnly.
     • Id should be set only once.
Edit the code in the Account Class as below.
Code in VB:                                                     Code in C#:
Private _Id As Integer                                          private int _Id
Private _Name As String                                         private string _Name
Private _Balance As Decimal                                     private decimal _Balance
'Balance Property                                               public decimal Balance
Public ReadOnly Property Balance() As Decimal                   {
      Get                                                           get
        Return _Balance                                             {
      End Get                                                             return _Balance;
End Property                                                        }
Public Property Name() As String                                }
      Get                                                       public String _Name
        Return _Name                                            {
      End Get                                                       get
      Set(ByVal Value As String)                                    {
        If (Value.Length > 8) Then                                        return _Name;
           Throw New ApplicationException("Name cannot              }
be                                                                  set
                                            >8                      {
characters")                                                                   if (value.Length > 8)
        Else                                                                         throw new ApplicationException("Name
           _Name = Value                                                                              cannot be > 8
        End If                                                            characters");
      End Set                                                                 _Name = value;
End Property                                                        }
Private _idAlreadySet As Boolean                                }
Public Property Id() As Integer                                 private bool idAlreadySet;
      Get                                                       public int Id
        Return _Id                                              {
      End Get                                                       get
      Set(ByVal value As Integer)                                   {
        If (idAlreadySet) Then                                            return _Id;
           Throw New ApplicationException("Id is already            }
set")                                                               set
        End If                                                      {
        _Id = Value                                                       if (idAlreadySet)
        idAlreadySet = True                                                  throw new ApplicationException("Id is already
      End Set                                                   set");
End Property                                                            _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

Deccansoft Software Services – MS.NET
     Properties, then such a aclass is called “Fully Encapsulated Class”.

                                              Constructors and Destructors
•   A constructor is a member method of a class which is automatically executed as soon as the object is created.
•   In VB.NET a constructor is always named as “New” and is a sub procedure
•   In CS it has same name as the class and doesn’t have a return type not even void.
•   A constructor without a parameter is called default constructor.
•   Parameterized constructor is a constructor in which the parameters are defined.
•   Copy Constructor: A constructor with object reference of the same class as the parameter is called a copy constructor.
    When we want to create a new object with is the duplicate of the existing object we use a copy constructor.
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
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.
In VB: Using Me.New() or MyClass.New() as the first statement in a constructor we can call another constructor of same
In C#: We have to put “this” after the declaration of the constructor.

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

In AccountForm make the following Changes:
Private Sub btnCreate_Click(. . .) Handles btnCreate.Click
     Dim id As Integer = Integer.Parse(txtId.Text)

Deccansoft Software Services – MS.NET
    Dim name As String = txtName.Text
    Dim initialBalance As Decimal = Decimal.Parse(txtBalance.Text)
    a = New Account(id,name, initialBalance)
End Sub

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

Syntax in VB:                                    Syntax in C#: Note: A destructor in C# cannot have any access modifiers
Protected Overrides Sub Finalize()               ~<class name>()
End Sub                                          { }.
3. 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 object.
         Public Sub Dispose () ‘ or Close()
                  ‘Write code here to release the resources.
         End Sub

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

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

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

Series of events that occur when the first object is created:
 1. Class is loaded
 2. Shared members are loaded and allocated memory
 3. Shared constructor is executed
 4. Instance members are loaded and allocated memory and Initialized
 5. Instance constructor is executed.

Code in VB: (Make Id as AutoIncrement Field)                    Code in C#:
Public Shared MinBalance as Integer = 500                       public static int MinBalance = 500;
Private Shared _PrevId as Integer                               public static int _PrevId
Private _Id As Integer                                          private int Id;
Public Sub New()                                                public Account()
    _PrevId += 1                                                {
    _Id = _PrevId                                                   _PrevId += 1;
End Sub                                                             _Id = _PrevId;
Public ReadOnly Property Id() As Integer                        }
  Get                                                           public int Id
    Return _Id                                                  {
  End Get                                                          get { return _Id }
End Property                                                    }
Note: Because “Id” is now auto increment field, remove the id parameter from the constructor.
In Withdraw method replace the constant value 500 with the shared member “MinBalance”

Deccansoft Software Services – MS.NET
In Form: Add btnGetMB and btnSetMB buttons and txtMB textbox

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

Shared Method: A method which does not have anything to do with the state of the object can be marked as Shared method.
Note: All methods and properties (procedures) of the class irrespective of whether they are shared or not are allocated memory
only once.
Shared Version                                                   Instance Version
Public Shared Function                                           Public Function GetAccountWithLessBalance(ByVal a As
            GetAccountWithLessBalance(ByVal a1 As                            Account) As Account
            Account, ByVal a2 As Account) As Account                   If (Me.Balance < a.Balance) Then
     If (a1.Balance < a2.Balance) Then                                    Return Me
        Return a1                                                      Else
     Else                                                                 Return a
        Return a2                                                      End If
     End If                                                      End Function
End Function
• A shared method 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. But 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
• 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.

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
2. An Static / Shared method ( ex: GetObject) can be added to the class to create the instance and return the reference to the
    same whenever required.

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                                                            }

•   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()

Deccansoft Software Services – MS.NET
•    A VB.NET module is compiled as a class with all members declared as static.

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

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

  class Program
     static void Main(String[] args)
          CB b;
          b = new CB(1,2,3,4);
    • The protected member of parent class cannot be accessed 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 they are 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 contructor 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

Deccansoft Software Services – MS.NET
   •   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

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

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

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

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

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

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)
b = DirectCast(a,CB)

The DirectCast method is much faster in performance but can be used only if the datatype of the object is exactly is known and
used otherwise it throws an exception. i.e The DirectCast would throw exception of a is not referring to object of type “CB” and
even if referring to object of any of its subclass.

b = TryCast(a,CB) ---Equivalent of as operator in CS
if “a” is referring to an object of Type “CB” or any of its subclass then the reference is assigned to “a” otherwise it assigns
“Nothing” to “a” (It doesn’t throw InvalidCastException)

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

                                              Static and Dynamic Binding
Virtual methods are dynamically binded i.e., the actual method called would be decided at runtime and the decision is based on
the object to which the reference variable invoking the method is referring to.

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

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

Deccansoft Software Services – MS.NET

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

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

CA:              ------                                                                 virtual

CB:       new             virtual new                          new           override       new virtual      override sealed

CC:    new         new             override       new        new     override       override           new         new

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

CB b = new
              Static      Dynamic       Dynamic     Static         Dynamic      Dynamic           Dynamic    Dynamic       Dynamic
              CB          CB            CC          CB             CB           CC                CC         CB            CB

Deccansoft Software Services – MS.NET
CA a= new
             Static Static          Static        Dynamic       Dynamic Dynamic             Dynamic     Dynamic        Dynamic
             CA       CA            CA            CA            CB           CC             CA          CA             CB
 • 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 if a method of parent class is overloaded in child class then it may be declared as “Overloads”.

                                            Abstract Classes and Methods
•    An abstract method is a method in which declaration is provided but implementation is not.
•    A class with one or more methods declared as abstract must be declared as abstract and cannot be instantiated because its
     behaviour is incomplete.
•    A class that inherits the abstract class must override all the abstract members of the parent class, if not it should also be
     declared as abstract.
•    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 an 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

Deccansoft Software Services – MS.NET
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                 ---

Deccansoft Software Services – MS.NET
                                                        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
• C# doesn’t support LateBinding and it is not allowed in VB.Net only if “Option Strict” is on.

                                    Passing Reference Type ByVal and ByRef

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

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

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

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

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

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

Deccansoft Software Services – MS.NET
             Demo d;
    i.e The variable of type Structure by default is initialized with default data and default value is not “null” unlike class.

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

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

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

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

Implicit / Explicit Implementation of Methods in C#:
interface IA1                  interface IA2              //Case 2
{                              {                          class CA : IA1, IA2
   void Foo1();                   void Foo1();            {
   void Foo2();                   void Foo3();               void IA1.Foo1()//Explicitly Implemented
}                              }                             {

Deccansoft Software Services – MS.NET
// Case1                                                             Console.WriteLine("In IA1.Foo1");
class CA : IA1, IA2 {                                              }
   public void Foo1() //Implicitly Implemented                     void IA2.Foo1() //Explicitly Implemented
   {                                                               {
     Console.WriteLine("In Foo1");                                   Console.WriteLine("In IA2.Foo1");
   }                                                               }
   public void Foo2() //Implicitly Implemented                     public void Foo2() //Implicitly Implemented
   {                                                               {
     Console.WriteLine("In Foo2");                                   Console.WriteLine("In IA1.Foo2");
   }                                                               }
   public void Foo3() //Implicitly Implemented                     public void Foo3() //Implicitly Implemented
   {                                                               {
     Console.WriteLine("In Foo3");                                   Console.WriteLine("In IA2.Foo3");
   }                                                               }
}                                                              }

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

Deccansoft Software Services – MS.NET
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.
      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 implementation method must be public and an 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.

                                                 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.

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
CA a = new CA();
a.Foo() //Ambigious call as both CA1 and CA2 qualify for execution.

class CA : IA1, IA2 //Valid - .Net supports Multile Interface Inheritance.
In this case we have to compulsory implement in the methods of Interface in the class and thus we don’t have any ambiguity.

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

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


Deccansoft Software Services – MS.NET

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
• 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
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.                                        }
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”)

Deccansoft Software Services – MS.NET
                                               End Try
                                               Console. WriteLine (“f7”)
                                             End Sub
To test the above program replace Console.WriteLine with Throw New Exception

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

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

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

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

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

Deccansoft Software Services – MS.NET
Using Statement Example
using (SqlConnection con = new SqlConnection ())                 SqlConnection con;
{                                                                try
   con.Open ();                                                  {
}                                                                        con = new SqlConnection();
When the using block ends it automatically calls the Close or            con.Open()
Dispose method of the connection object                          }
                                                                         if (con.State = ConnectionState.Open)

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


                  IList                                                 IDictionary

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

• 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
• Represents a collection of key/value pairs that are organized based on the hash code of the key.
• Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual
   Basic), but a value can be.
• The capacity of a Hashtable is the number of elements the Hashtable can hold. The default initial capacity for a Hashtable
   is 0. As elements are added to a Hashtable, the capacity is automatically increased as required through reallocation with the
   default capacity on adding first item set to 4.
• In HashTable Key must be unique but value need not be unique.

‘Demo of ArrayList                                              ‘Demo of HashTable
Dim al As New ArrayList()                                       Dim ht As New Hashtable()
Console.WriteLine(al.Capacity)                                  ht.Add(101 "Student1")
al.Add("s1")                                                    ht.Add(102, "Student2")
al.Add("s2")                                                    ht.Add(103, "Student3")
al.Add("s3")                                                    ht.Add(104, "Student4")
al.Insert(2, "s1.5")                                            ht.Remove(102)
al.Remove("s2")                                                 For Each de As DictionaryEntry In ht
al.RemoveAt(1)                                                      Dim key As Object = de.Key

Deccansoft Software Services – MS.NET
Console.WriteLine(al.Count())                                      Dim value As Object = de.Value
Console.WriteLine(al.Capacity)                                     Console.WriteLine(key & " " & value)
Console.WriteLine(al.Contains("s1"))                            Next
For Each str As String In al                                   Console.WriteLine(ht.Count)
   Console.WriteLine(str)                                      Console.WriteLine(ht.ContainsKey(104))
Next                                                           Console.WriteLine(ht.ContainsValue(“Student5”))
Dim s as String                                                Dim s as String
s = CType(al(1),String) ‘ or CStr(al(1))                       s = CType(ht(101),String) ‘ or CStr(ht(101))
Console.WriteLine(“Item with index 1 : “ & s)                  Console.WriteLine(“Item with index 1 : “ & s)

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

Deccansoft Software Services – MS.NET
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                                  Console.WriteLine(“Before Sorting”)
IComparable.CompareTo                                                       For Each d As Demo In alDemo
     Dim d As Demo = CType(obj, Demo)                                         Console.Write(d.N.ToString() & " ")
     If (Me.N < d.N) Then                                                   Next
        Return -1 ‘the current object comes before the parameter            alDemo.Sort() ‘Here the Demo object must
object                                                                                     ‘ implement IComparable
     ElseIf (Me.N > d.N) Then                                               Console.WriteLine("Sorted:")
        Return 1 ‘the current object comes after the parameter              Console.WriteLine(“After Sorting”)
object                                                                      For Each d As Demo In alDemo
     Else                                                                     Console.Write(d + " ")
        Return 0                                                            Next
     End If                                                           ‘For converting elements in the array list to array.
  End Function                                                             Dim arDemo() as Demo
‘ToString is the method in object class and the default                    Dim ar as Array = al.ToArray(GetType(Demo))
implementaiton return the class name. We can override and return           arDemo = CType(ar,Demo())
any other value.                                                       'To Add an array/collection to ArrayList
  Protected Overrides Function ToString()                                   Dim alNew As New ArrayList()
     Return N.ToString()                                                    alNew.AddRange(arDemo)
  End Function                                                        ‘Using enumerator which is internally used by for
End Class                                                             each statement also.
                                                                          Dim en as IEnemerator
•   ArrayList collection can sort only those items in collection           en = al.GetEnumerator( )
    whose class has implemented the interface “IComparable”               While(en.MoveNext())
                                                                              Dim d as Demo = CType(en.Current,Demo)
                                                                          End While
                                                                      ‘To Get a particular item reference from the
                                                                      collection based on its index.
                                                                         Dim d1 as Demo = CType(al(2), Demo)
                                                                         Console.WriteLine(“Item with index 2 : “ & d1)
                                                                         End Sub
                                                                      End Class

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

Deccansoft Software Services – MS.NET
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

Deccansoft Software Services – MS.NET
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 Stack5);                                     {
      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

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

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 Object = kv.Key
Console.WriteLine(al.Count())                                     Dim value As Object = kv.Value
Console.WriteLine(al.Capacity)                                    Console.WriteLine(key & " " & value)
Console.WriteLine(al.Contains("s1"))                          Next
For Each s As String In al                                    Console.WriteLine(dic.Count)
    Console.WriteLine(s)                                      Console.WriteLine(dic.ContainsKey(104))
Next                                                          Console.WriteLine(dic.ContainsValue(“Student5”))

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

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

Deccansoft Software Services – MS.NET
                                              Writing Custom Collection Class
                            IDictionary                                    IList

                            DictionaryBase                                 CollectionBase

                            AccountCollection                              AccountCollection

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

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

Deccansoft Software Services – MS.NET
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”.

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

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

You can use the unsafe modifier in the declaration of a type or a member. The scope of the unsafe context extends from the
parameter list to the end of the method, so pointers can also be used in the parameter list
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.
         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};
                             Test t = new Test(10);
                             fixed(int *pA=ar)

Deccansoft Software Services – MS.NET
                                          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: +, -, *, /, %, &, |, ^, <<, >>, ==, !=, >, <, >=, <=

                                                                             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                        return p;
String                                                                          }
     Return p.X & ", " & p.Y                                                    public static implicit operator string(Point p)
   End Operator                                                                 {
   Public Shared Narrowing Operator CType(ByVal s As String)                      return p.X + ", " + p.Y;
                                                          As                    }
Point                                                                           public static explicit operator Point(string s)
     Dim p As New Point()                                                       {
     p.X = Integer.Parse(s.Split(","c)(0))                                        Point p = new Point();
     p.Y = Integer.Parse(s.Split(","c)(1))                                        p.X = int.Parse(s.Split(',')[0]);
     Return p                                                                     p.Y = int.Parse(s.Split(',')[1]);
   End Operator                                                                   return p;
  Public Shared Sub Main()                                                      public static void Main()
    Dim p1 As Point = New Point(1, 2)                                           {
    Dim p2 As Point = New Point(3, 4)                                             Point p1 = new Point(1, 2);
    Dim p3 As Point = p1 + p2                                                     Point p2 = new Point(3, 4);
    Dim s As String = p3 'Implicit Casting from point to string                   Point p3 = p1 + p2;
    Console.WriteLine(s)                                                          string s = p3; //Implicit Casting from point to
    Dim p As Point = CType("10,2", Point) ‘Explicit Casting                  string
Console.WriteLine(p.X & " " & p.Y)                                                Console.WriteLine(s);
  End Sub                                                                         Point p = (Point)"10,2"; //Explicit Casting
End Class                                                                         Console.WriteLine(p.X + " " + p.Y);

Deccansoft Software Services – MS.NET

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

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

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

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

Deccansoft Software Services – MS.NET
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();
//In VB.NET equivalent of typeof is GetType
Dim tp as System.Type = GetType(Test)

To create an instance of the class without using new operator
Test t = Activator.CreateInstance(tp) //or new Test()

                                           Configuration File: (App.Config)
It is an XML document containing all the configuration information for that application when a project in studio is built along
with the output .exe or .dll.

In Studio Project, add a file by name App.Config.
         <add key=”k1” value = “V1”>
         <add key=”k2” value = “V2”>
         <add key=”k3” value = “V3”>
• RightClick on project -> Add References -> System.Configuration.dll
Imports System.Configuration
Private Sub btn_Click(…)
End Sub

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

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

Deccansoft Software Services – MS.NET
Location of Framework Folder: C:\Windows\Microsoft.Net\Framework\V2.0.X.X\

Deccansoft Software Services – MS.NET

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

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

Syntax for attaching an Attribute to a class or any of its member.
<Author ( )> Class Account
End Class
<Author (“Soni”, “Hyderabad”)> Class Account
End Class
<Author (Name: = “Soni”, Location: = “Hyderabad”)> Class Account
End Class

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

                                                      Partial Classes
It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file
contains a section of the class definition, and all parts are combined when the application is compiled. There are several
situations when splitting a class definition is desirable:
•   When working on large projects, spreading a class over separate files allows multiple programmers to work on it
•   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.

Deccansoft Software Services – MS.NET

Walkthrough for Assemblies_Without Screens

      Create a new Class Library
      1. Click File menu and select Project from New.
      2. From New Project select Visual Basic-Windows from Project Types. From Templates select Class Library and
      check the Checkbox of Create directory for solution. Write the ClassLibrary1 in the Name and write the project location
      in Location i.e. D:\DemoSolution. Write the Solution Name is AssembliesWalkthrough.Click OK.
      3. Write the code in the Class of Class1.vb.
           Public Class Class1
              Public Function SayHello(ByVal name As String) As String
                 Return "Hello" & " " & name & " " & "1.0"
              End Function
           End Class
      4. Build the ClassLibrary1 ->Select Build Solution from Build menu.
2.    Providing Strong Name to the Class Library
      1. Select Solution Explorer from View menu.
      2. Right Click on ClassLibrary1 and Select the Properties.
      3. Click on Signing and Check the Sign the assembly Checkbox. Select <New…> for creating
         a strong name key file and Click on it.
      4. You get one dialogbox with the name of Create a strong Name Key. If you want to provide the password check the
      Checkbox of Protect my key file with a password. Other wise do uncheck. Enter the Strong Name Key in key file name.
      Click OK.
      Note: VS.NET uses a tool called as SN.EXE for generating the strong name key file.
          5. xyz.snk will be added in the ClassLibrary1 project.
      Providing Version to the Class Library
      1. Click on Application Tab and Click on Assembly Information button.
      2. Assembly Information dialogbox will open. Default Version for Assembly Version will
      be 1 0 0 0, keep as it is and ensure None is selected in Neutral Language. Click OK.
4     Deploying the assembly in GAC through Visual Studio 2005 Command Prompt
(A)   1. Open Visual Studio 2005 Command Prompt from Visual Studio
      2. Goto the Debug folder of the Project and run the following command
         GacUtil -i ClassLibrary1.dll.
      **We can also uninstall Assembly by typing GacUtil –u ClassLibrary1.dll
4     Another way to add into the Cache
(B)   1. Click on Start ===> Select Control panel from Settings   Click-Administrative   Tools===>Click-Microsoft .NET
      Framework 2.0 Configuration.
      2. In the new Window Click Assembly Cache from My Computer and click on Add an Assembly to the Assembly
      3. You will get Add an Assembly dialogbox. In that select our ClassLibrary1.dll from ClassLibrary1 folder and click
      Open. Then the assembly will be added to the GAC.

5.    Adding a new Windows Application to the ClassLibrary

      1.   Go to Solution Explorer, right click on that and Select New Project from Add.
      2.   Select Windows from Project Types and select Windows Application from Templates and click OK.
      3.   Right click on WindowsApplication1 and select the Add Reference.
      4.   We get Add Reference dialogBox click on Projects and select Class Library1 from Project Name. Click OK.

Deccansoft Software Services – MS.NET
         Note: The Projects Tab is used for adding reference to other projects in the same Solution.
     5.  Click View menu and click on Tool Box.
     6.  You will get a Tool Box on Left hand side of the form. Double Click on Button from Common Controls.
         Imports ClassLibrary1
         Public Class Form1
            Private Sub Button1_Click(……) Handles Button1.Click
               ‘Create an object of ClassLibrary
               Dim c As new Class1
               ‘calling the function of ClassLibrary1.
            End Sub
          End Class
     7. Right Click on Windows Application1 and select Set as StartUp Project option.
     8. Now run the project with Ctrl+F5. We get Form1 with Button1.
     **We can see the included classlibrary1.dll file in Windows Application1 through D drive in this way
     D:\DemoSolution\AssembliesWalkthrough\WindowsApplication1\bin\Debug path and see the dll of the Class Library.

•   Goto references of windows Application1, Select ClassLibrary1 Properties, set local copy property to false.
•   The same signing key file can be used for signing of all the assemblies developed by a given developer or organization.

                                      Walkthrough for Assemblies (Case Study-2)
                                Continue of Case Study-1 (For Side- by- Side Versioning)

     1. Copy the dll of ClassLibrary1 from the bin folder of our project and go to
     C:\Program Files\Common Files\CompanyName\ClassLibrary\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 instead of
     4. Rebuild the ClassLibrary1 and copy the dll of ClassLibrary1 from the bin folder of our project to
     C:\Program Files\Common Files\CompanyName\ClassLibrary\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 (select the Dll present in “C:\Program Files\Common Files\CompanyName\ClassLibrary\Ver 1.0”)
     6. Add to the existing solution, a new windows application (WindowsApplication2) and add reference to ClassLibrary1 -
     Version (select the Dll present in “C:\Program Files\Common Files\CompanyName\ClassLibrary\Ver 2.0”)
     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.
             End Sub
             End Class
     8. Right Click on WindowsApplication1 and select Set as StartUp Project option.

Deccansoft Software Services – MS.NET
    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.

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

Deccansoft Software Services – MS.NET
                                       Walkthrough for Assemblies (Case Study-3)
                                  Continue of Case Study-2 (For Upgrading the Versions)
      1. Click on Start from the Desktop ==>Go to Settings => Control panel => Administrative Tools =>Microsoft .Net
      FrameWork 2.0 Configuration ===>Click on My Computer => Configured Assemblies
      2. Click on Configure an Assembly. You will get Configure an Assembly dialogbox.
      3. Click on Choose Assembly button. Choose the Library which you want to upgrade i.e ClassLibrary1 with Version
      4. After that click on Finish button.
      5. You will get a dialogbox of ClassLibrary1 Properties. Click on Binding Policy button in that write the Requested
      Version which we want to replace to New Version
      Click on Apply button and then OK button.
      6. Go to this path through C drive C:\WINNT\Microsoft.NET\Framework\v2.0.50727\CONFIG and click on
      machine.config file and open it.
      7. Copy this code from machine.config file and paste it where we want the new version in this way.
                  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                     <assemblyIdentity name="ClassLibrary1" publicKeyToken="33eca4cd7b0f83c0" />
                     <bindingRedirect oldVersion="" newVersion="" />
      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 this one will be upgraded with new Version and shows the
      information of Version
•    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.
•    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.

Deccansoft Software Services – MS.NET
•     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

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

Notes about Shared Assemblies:
• The referring application includes the strong name of the referred assembly (library) in its manifest which is auto generated
    by the language compiler.
• When the application requires the library, using this strong name information the referred library is loaded. First it searches
    the library in the local directory of the application but if not found it is loaded from GAC.
• A shared assembly must be first placed in any directory and from their it must be installed in GAC and from the same
    location we can add the reference to the assembly in the application. We cannot add reference to the assembly directly
    from GAC.

Deccansoft Software Services – MS.NET

System.IO is the Namespace for all I/O related classes.

There are three types of streams:
• Basic Stream
• Character / Text Stream
• Binary Stream

Stream (abstract class)
  FileStream / MemoryStream /       BufferedStream are inherited from Stream

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

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

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.
In a text file one unit of data is always one character, where as in binary file one unit of data is not fixed and depends of the
type of data storing in a file.

Character Stream is divided into
TextReader                  Text Writer
    StringReader                 StringWriter
    StreamReader                 StreamWriter

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

Binary Streams : Binary Reader & Binary Writer
BinaryReader and BinaryWriter read and write encoded strings and primitive data types from and to

The three standard I/O streams are
    • Console.In --       TextReader
    • Console.Out -- TextWriter
    • Console.Error – TextWriter
Methods of TextReader: Read, ReadBlock ReadLine & ReadToEnd:
Methods of TextWriter: Write and WriteLine

Deccansoft Software Services – MS.NET

Module Echo1                                       Module Echo2
  Public Sub Main()                                  Public Sub Main()
    Dim data As Integer                                 Dim n As Integer
    While True                                          Dim c(10) As Char
       data = Console.In.Read()                         n = Console.In.Read (c, 0, c.Length)
       If (data = -1) Then                              Dim i As Integer
          Exit While                                    For i = 0 To c.Length - 1
       End If                                               Console.Out.WriteLine("C({0}) = {1}", i, AscW(c(i)))
       Console.Out.Write(ChrW(data))                    Next
    End While                                           Console.WriteLine("Actual No of Chars Read : " & n)
  End Sub                                            End Sub
End Module                                         End Module
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 one byte charset
  Sub Main(ByVal args() As String)
    Dim fs As FileStream
       fs = New FileStream(args(0), FileMode.Open, FileAccess.Read, FileShare.None)
    Catch e As FileNotFoundException
       Console.Write("File not found")
    End Try
    Dim data As Integer
    While (True)
       data = fs.ReadByte()
       If (data = -1) Then
          Exit While
       End If
    End While
  End Sub
End Module
Module KeyBoardToFile ‘Works good only for language with one byte charset
  Sub Main(ByVal args() As String)
    Dim fs As FileStream
       fs = New FileStream(args(0), FileMode.CreateNew, FileAccess.Write)
    Catch err As IOException
       Console.WriteLine("File Already Exists")
    End Try
    Dim data As Integer

Deccansoft Software Services – MS.NET
    While True
       data = Console.In.Read()
       If (data = -1) Then
          Exit While
       End If
    End While
  End Sub
End Module
Module UnicodeTextWriting
  Sub Main()
    Dim fs As New FileStream("c:\unicode.txt",FileMode.OpenOrCreate) ‘ if file is there it opens else creates a new file
    Dim sw As New StreamWriter(fs, System.Text.Encoding.Unicode)
  End Sub
End Module
Module UnicodeTextReading
  Sub Main()
    Dim fs As New FileStream("c:\unicode.txt",FileMode.Open)
    Dim sr As New StreamReader(fs, System.Text.Encoding.Unicode)
  End Sub
End Module
Module KeyBoardToTextFile ‘Works good for all languages
  Sub Main(ByVal args() As String)
    Dim sw As StreamWriter
       Dim fs As New FileStream(args(0), FileMode.CreateNew, FileAccess.Write)
       sw = New StreamWriter(fs)
    Catch err As IOException
       Console.WriteLine("File Already Exists")
    End Try
    Dim data As Integer
    While True
       data = Console.Read()
       If (data = -1) Then
          Exit While
       End If
    End While
  End Sub
End Module
Module BinaryReaderWriter
  Sub Main()
    Dim file As System.IO.FileStream
    file = New FileStream("d:\demo.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite)
    Dim n As Integer = 65
    Dim m As Long = 54321
    Dim str As String = "ABCDEFG"
    Dim bsw As New BinaryWriter(file, Text.Encoding.Unicode)

Deccansoft Software Services – MS.NET

    file.Seek(0, SeekOrigin.Begin)

    Dim bsr As New BinaryReader(file, Text.Encoding.Unicode)
    n = bsr.ReadInt32()
    str = bsr.ReadString()
    m = CLng(bsr.ReadDouble())

    Console.WriteLine(n & " " & str & " " & m)
  End Sub
End Module
Module Split
  Sub Main(ByVal args() As String)
    Dim fs As FileStream
    fs = New FileStream(args(0), FileMode.Open)
    Dim name, ext As String
    name = fs.Name
    Dim fi As New FileInfo(name)
    'name = fi.Name()
    ext = fi.Extension
    Dim ind As Integer
    ind = name.LastIndexOf(".")
    name = name.Substring(0, ind)
    Dim size As Integer
    size = CInt(args(1))
    Dim data(size) As Byte
    Dim i As Integer
    While (True)
       i += 1
       Dim n As Integer
       n = fs.Read(data, 0, data.Length - 1)
       If (n = 0) Then Exit Sub
       Dim newfile As New FileStream(name & i & ext, FileMode.Create)
       newfile.Write(data, 0, n)
    End While
  End Sub
End Module
Module ReverseFileContent
  Sub Main(ByVal args As String())
    Dim fs As New FileStream(args(0), FileMode.Open, FileAccess.ReadWrite)
    Dim sr As New StreamReader(fs)
    Dim strOriginal As String
    strOriginal = sr.ReadToEnd()
    Dim sb As New System.Text.StringBuilder(strOriginal.Length)
    For i As Integer = strOriginal.Length - 1 To 0 Step -1
    fs.Seek(0, SeekOrigin.Begin)
    Dim sw As New StreamWriter(fs)
  End Sub

Deccansoft Software Services – MS.NET
End Module

                                                Working with File System
In IO we have four more class which is used for managing the file system on the machine. They are
1. File provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of a
2. Directory provides static methods for creating, moving, and enumerating through directories and subdirectories. The
3. FileInfo provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the
     creation of FileStream objects. This class cannot be inherited.
4. DirectoryInfo provides instance methods for creating, moving, and enumerating through directories and subdirectories.

So generally, when we want to perform more than one operation, we go for FileInfo and DirectoryInfo. But for a given path if
we want to perform only one operation we go for File and Directory.

Module FileDirectory
 Public Sub Main()
   Dim args() As String
   args = System.Environment.GetCommandLineArgs()

     Dim dir As DirectoryInfo
     dir = New DirectoryInfo(args(1))
     If (Not dir.Exists) Then
        Console.WriteLine("Directory Not Existing")
     End If

     Dim arFileInfo As FileInfo() = dir.GetFiles(“*.txt”)
     For Each objFileInfo As FileInfo In arFileInfo

    For Each objDirectoryInfo As DirectoryInfo In dir.GetDirectories(“D*”)
  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

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()

Deccansoft Software Services – MS.NET
    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",
    Dim p2 As New MyPoint                                          IO.FileMode.Open)
    p2.X = 15 : p2.Y = 25                                               line = DirectCast(bf.Deserialize(fs), MyLine)
    line.From = p1 : line.To = p2                                       Console.WriteLine(line.From.X & " " & line.From.Y)
    Dim bf As New BinaryFormatter                                       Console.WriteLine(line.To.X & " " & line.To.Y)
    Dim fs As New IO.FileStream("c:\line.dat",                          fs.Close()
            IO.FileMode.OpenOrCreate)                                End Sub
    bf.Serialize(fs, line)                                         End Module
  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

•   Thread: The path of execution of instructions within a process is called a thread. A program with multiple simultaneous
    paths of execution is said to be multithreaded application.
•   A running instance of an application is a process. Every process has its own memory address space and it is in that all the
    data and the instructions of that process reside.

Types of Scheduling:
Preemptive: OS can unschedule a thread even if it is not completed its task.
Non-Preemptive: OS cannot unschedule the thread when it is not completed. A Thread should either unschedule itself of
should complete its task.

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

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

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

Deccansoft Software Services – MS.NET


                    Sleep                                      Suspended

               Blocked                     Dead                Wait

Running: A thread is said to be in running state when it is in processor executing one of its instruction.
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.
Dead: After the execution of all the instructions in the thread it goes to dead state. Dead threads cannot be revived.
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.
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.
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.
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";
Demo class encapsulates the instructions of thread in Run               d1.Interval = 2000;
method and also encapslates message and interval data                   t1.Start();
members.                                                                t.Join(2000);
                                                                        if (t.IsAlive)
                                                                           Console.WriteLine("t is alive");
                                                                        //t1.IsBackground = t.IsBackground = true;
                                                                        Console.WriteLine("Main method ends");

Deccansoft Software Services – MS.NET
The process terminates when the main thread terminates. A thread terminates only when the other non background threads
which it has created terminates. The creator doesn’t wait for the non background thread if it has to terminate.

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.Thread.Priority = 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();
                                  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)                            }
         {                                                               private void btnInterrupt_Click(. . .)

Deccansoft Software Services – MS.NET
              n = 0;                                                    {
          }                                                               t.Interrupt();
     }                                                                  }
}                                                                       private void btnThreadState_Click(. . .)
public static void Main() //To make form as the startup object.         {
{                                                                       MessageBox.Show(t.ThreadState.ToString());
      Application.Run(new 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.
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.

Deccansoft Software Services – MS.NET
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()
                              //Replace above line with code to be executed by only one thread at a time
          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));
                    Console.WriteLine("Main Ended");
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 the number of threads allowed in a block would be only one.
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));
      Thread.Sleep(1000);                                                         t.Name = "T" + i;
      s.Release();                                                                t.Start();
      Console.WriteLine(Thread.CurrentThread.Name                              }
                                                  + " Ended");                 Console.WriteLine("Main method ends");
   }                                                                         }
}                                                                         }

Deccansoft Software Services – MS.NET


                              Client Process

                                   Managed                     DataSet


                                         Command             Connection           Data Reader

                                                             DB Server

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

Imporatant Objects in Managed Provider

Connection: Connection object encapsulates the functionaly of establishing a communication path over the sockets.
In .Net ,Connections are ‘Pooled’ we can make by default 100 simultaneous connections max without closing but obviously we
have to close every connection Opened only then it can be reused ‘Pooled’ means keeps the connection for the short period of
time. The Connection Pooling can be customized by setting appropriate parameters in ConnectionString of Connection Object.

Command: Command Object can be used for execution of any type of Sql Statement.
When “Select” or similar type of sql statement is executed a new cursor is created on the back end. This Cursor contains
primary key or indexed key of the table on which the statement is executed. There is also 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) and EmpSalary – Money
• Note: On top of file mention Imports System.Data.SqlClient
To Execute Insert / Delete / Update Statements
  Private Sub btnInsert_Click(. . .) Handles btninsert.Click
    Dim con As New SqlConnection
    con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=MSNET;Integrated Security=True"

Deccansoft Software Services – MS.NET
   ‘con.ConnectionString = "Data Source=.\sqlexpress;Database=MSNET;User id=sa;Password=dss"
   ‘con.ConnectionString = "Server=.\sqlexpress;Database=MSNET;uid=sa;pwd=dss"
   Dim cmd As New SqlCommand
   cmd.CommandText = "Insert into Emp (EmpName,EmpSalary)
                                    values('” & txtName.Text.Replace(“’”,”’’”) & “',” & txtSalary.Text & “)"
   'cmd.CommandText = "Delete from Emp where empid=" & txtId.Text
   'cmd.CommandText = "Update Emp Set EmpName = ‘” & txtName.Text & “’, EmpSalary=” & txtSalary.Text &
                                                                      “where EmpId=" & txtId.Text
   cmd.CommandType = CommandType.Text
   cmd.Connection = con
      MsgBox(cmd.ExecuteNonQuery()) ‘Used for execution of those Statements which do not return any data.
      cmd.CommandText = "Select @@Identity" ‘Used for fetching the last set Identity column value on the current con.
      MsgBox(“Last Inserted EmpId as set by backend “ + cmd.ExecuteScalar)
     If (con.State = ConnectionState.Open) Then con.Close()
   End Try
  End Sub
 Private Sub btnSelect_Click(. . .) Handles btnSelect.Click
   Dim con As New SqlConnection
   con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=MSNET1;Integrated Security=TRUE"
   Dim cmd As New SqlCommand
   'cmd.CommandText = "select empname from emp where empid=1"
   cmd.CommandText = "select max(empid) from emp"
   cmd.CommandType = CommandType.Text
   cmd.Connection = con
   MsgBox(cmd.ExecuteScalar) ‘Used for execution of statements which return only one value.
 End Sub

 Private Sub btnGetEmp_Click(. . .) Handles btnGetemp.Click
   Dim con As New SqlConnection
   con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=MSNET1;Integrated Security=TRUE"
   Dim cmd As New SqlCommand
   cmd.CommandText = "select EmpId,EmpName,EmpSalary from Emp"
   cmd.CommandType = CommandType.Text
   cmd.Connection = con
   Dim dr As SqlDataReader
   dr = cmd.ExecuteReader() ‘Used for those statements which would return a set of records.
   ‘dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
   ‘If the CommandBehavior is mentioned the Connection is automatically closed when the data reader is closed.
   Dim str As String = ""
   While (dr.Read())
      str &= dr("EmpId") & vbTab
      str &= dr(1) & vbTab
      Dim ind As Integer
      ind = dr.GetOrdinal("EmpSalary") ‘Return the Index of the column EmpSalary
      If (dr.IsDBNull(ind)) Then
         str &= "--" & vbCrLf
         str &= dr.GetDecimal(ind) & vbCrLf
      End If
   End While

Deccansoft Software Services – MS.NET
     con.Close()–This is not required if CommandBehavior.CloseConnection is mentioned when the dtatement is executed.
   End Sub
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 2.0
Note: A class inherited from dataset and customized for a specific type of schema database is called typed dataset.

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

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

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

‘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 New SqlParameter("@EmpSalary", SqlDbType.Money)

Deccansoft Software Services – MS.NET

‘Set the value for the parameters.
      parEmpName.Value = txtName.Text
      parEmpSalary.Value = txtSalary.Text
      cmd.Prepare() ‘Prepares the execution plan for the sql statement.
  End Sub

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.
Sample Stored Procedure
    Create Procedure GetSalary(@Id int,@Sal money out)
           Select @Sal=EmpSalary from Emp where EmpId=@Id
    Private Sub btnSPDemo_Click(. . .) Handles btnSPDemo.Click
       Dim con As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=MSNET1;Integrated Security=TRUE")
       Dim cmd As New SqlCommand
       cmd.CommandText = "GetSalary"
       cmd.CommandType = CommandType.StoredProcedure
       cmd.Connection = con
       Dim parId As New SqlParameter("@Id", SqlDbType.Int)
       Dim parSal As New SqlParameter("@Sal", SqlDbType.Money)
       parSal.Direction = ParameterDirection.Output
       parId.Value = 1
    End Sub
    Create Procedure GetEmployees()
           Select Count(*) from Emp
           Select EmpId, EmpName, EmpSalary from Emp

Private Sub btnGetEmployeesStoredProcedure_Click(. . .) Handles btnInsParam.Click
     Dim con As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=MSNET1;Integrated Security=TRUE")
     Dim cmd As New SqlCommand
     cmd.CommandText = "GetEmployees"
     cmd.CommandType = CommandType.StoredProcedure
     cmd.Connection = con
     dr = cmd.ExecuteReader()
     dr.Read() //Moves to the only record in the first cursor created by execution of the stored procedure.
     MsgBox(dr(0)) //Shows the total number of records in the Emp table.- i.e result of “Select Count(*) from Emp”
     dr.NextResult() //Moves the Pointer to the second cursor. i.e “Select EmpId,EmpName,EmpSalary from Emp”
     Dim str As String = ""
     While (dr.Read())

Deccansoft Software Services – MS.NET
       str &= dr("EmpId") & vbTab
       str &= dr(1) & vbTab
       Dim ind As Integer
       ind = dr.GetOrdinal("EmpSalary") ‘Return the Index of the column EmpSalary
       If (dr.IsDBNull(ind)) Then
          str &= "--" & vbCrLf
          str &= dr.GetDecimal(ind) & vbCrLf
       End If
    End While
End Sub
Using Factory class for writing Provider Independent code. (only in 2.0)
    Dim con As IDbConnection
    Dim fac As System.Data.Common.DbProviderFactory
    fac = System.Data.Common.DbProviderFactories.GetFactory( “System.Data.SqlClient")
    con = fac.CreateConnection()
    con.ConnectionString = ""
    Dim cmd As IDbCommand
    cmd = con.CreateCommand()
    Dim par As IDataParameter
    par = cmd.CreateParameter()
    Dim dr As IDataReader
    dr = cmd.ExecuteReader

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

  Private Sub btnTransaction_Click(. . .) Handles btnTransaction.Click
    Dim con As New SqlConnection
    con.ConnectionString = "Server….."
    Dim cmd1, cmd2 As SqlCommand
    cmd1.Connection = con
    cmd2.Connection = con
    cmd1.CommandText = “Insert into Emp(EmpName,EmpSalary) Values(‘A1’,10000)
    cmd2.CommandText = “Update Emp set EmpSalary = EmpSalary = 1000
    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
       trans.Commit() ‘ If no exception is thrown the transaction is commited.
       trans.Rollback() ‘If exception is thrown the transaction is rolledback.
    End Try
End Sub
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)

Deccansoft Software Services – MS.NET
     Dim cmd As New SqlCommand(spName, con)
     cmd.CommandType = CommandType.StoredProcedure
     For Each p As SqlParameter In params
        Return cmd.ExecuteNonQuery()
     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

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

DataAdapter has following four Command Properties:
       SelectCommand / InsertCommand / UpdateCommand / DeleteCommand

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

Dealing with Concurrency:-
When the changes made to the dataset by one user are updated in the database. The data in the dataset of the other users
becomes Dirty and such data should not be updated unless the dataset is refeshed and latest data is modified and updated

Deccansoft Software Services – MS.NET
because if change are made to the dirty data and updated in database, it may override the change made by other users with out
looking it those changes.
Solution: The UpdateCommand and DeleteCommand CommantText must be modified to inlcude all the columns so that the
Original Values of Column are compared with the values in the database fields.
Ex: Update Emp Set EmpName=@EName, EmpSalay=@ESal
         where EmpId=@OrgEId and EmpName=@OrgEName and EmpSalary=@OrgESalary
When DataAdapter#Update is executed and if it executes ethier UpdateCommand or DeleteCommand and finds the number
of records effected by execution of this command is zero then it throws “DBConcurrencyException”.

  Dim con As New SqlConnection("server=localhost\sqlexpress;user id=sa;password=dss;database=MSNET")
  Dim dt As DataTable
  Dim WithEvents daEmp As SqlDataAdapter
  Dim ds As DataSet
  Dim dr As DataRow
  Private Sub DataSetForm_Load(. . .) Handles MyBase.Load
    daEmp = New SqlDataAdapter("Select EmpId, EmpName, EmpSalary from Emp", con)
    ds = New DataSet()
    daEmp.Fill(ds, "Emp")
    dt = ds.Tables("Emp")
    '''''''Insert Command'''''''
    Dim cmdInsert As SqlCommand
    Dim p(2) As SqlParameter
    cmdInsert = New SqlCommand("insert into Emp(EmpName,EmpSalary) values (@ename,@esalary)", con)
    p(0) = cmdInsert.Parameters.Add("@ename", SqlDbType.VarChar, 20)
    p(0).SourceColumn = "EmpName"
    p(0).SourceVersion = DataRowVersion.Current
    p(1) = cmdInsert.Parameters.Add("@esalary", SqlDbType.Float)
    p(1).SourceColumn = "EmpSalary"
    p(1).SourceVersion = DataRowVersion.Current
    daEmp.InsertCommand = cmdInsert
    ''''''''Delete Command''''''''
    Dim cmdDelete As SqlCommand
    cmdDelete = New SqlCommand("Delete from emp where empid=@orgeid", con)
    Dim par As SqlParameter
    par = cmdDelete.Parameters.Add("@orgeid", SqlDbType.Int)
    par.SourceColumn = "Empid"
    par.SourceVersion = DataRowVersion.Original
    daEmp.DeleteCommand = cmdDelete
    '''''''Update Command'''''''''
    Dim cmdUpdate As SqlCommand
    cmdUpdate = New SqlCommand("Update Emp set empname=@ename, empsalary=@esalary
                                                                                  where empid=@orgeid", con)
    p(0) = cmdUpdate.Parameters.Add("@orgeid", SqlDbType.Int)
    p(0).SourceColumn = "Empid"
    p(0).SourceVersion = DataRowVersion.Original
    p(1) = cmdUpdate.Parameters.Add("@ename", SqlDbType.VarChar, 20)
    p(1).SourceColumn = "EmpName"
    p(1).SourceVersion = DataRowVersion.Current
    p(2) = cmdUpdate.Parameters.Add("@esalary", SqlDbType.Float)
    p(2).SourceColumn = "EmpSalary"
    p(2).SourceVersion = DataRowVersion.Current
    daEmp.UpdateCommand = cmdUpdate

     'Dim sb As New SqlCommandBuilder(daEmp)

Deccansoft Software Services – MS.NET
‘The above statement automatically frames the InsertCommand, UpdateCommand and DeleteCommand properties of the
‘mentioned DataAdapter (daEmp) and it does so based on the columns mentioned in the SelectCommand Properties of the
‘DataAdapter. Also for this to work the database table mentioned in the Select statement of SelectCommand must have the
‘PrimaryKey defined in the backend (database).

      ‘For Handling events of DataAdapter in C# do the following here…..
        ‘daEmp.RowUpdated += new SqlRowUpdatedEventHandler(da_RowUpdated);
        ‘daEmp.RowUpdating += new SqlRowUpdatingEventHandler(da_RowUpdating);
End Sub
Private Sub RefreshEmpIdDropDown()
     For Each dr As DataRow In dt.Rows
     cmbId.SelectedIndex = 0
End Sub
Private Sub cmbId_SelectedIndexChanged(. . .) Handles cmbId.SelectedIndexChanged
     Dim drs() As DataRow
     drs = dt.Select("empid=" & cmbId.Text) ‘This method return the array of datarow based on the condition given
                                                 as the first parameter.
     dr = drs(0) ‘Because we are filtering based on the EmpId which is PrimaryKey we will have only 1 record.
     ‘dt.PrimaryKey = New DataColumn(){dt.Columns(“EmpId”)}
     'dr = dt.Rows.Find(cmbId.Text) ‘Can be used only if the EmpId is maked as PrimaryKey in DataTable
     txtName.Text = dr("EmpName").ToString()
     If (dr.IsNull("EmpSalary") Then
         txtSalary.Text = ""
         txtSalary.Text = dr("EmpSalary").ToString()
     End If
  End Sub
  Private Sub btnNew_Click(. . .) Handles btnAdd.Click
     Dim dr As DataRow
     dr = dt.NewRow()
     dr("Empname") = txtName.Text ‘EmpId is AutoIncrement thus it need not be set.
     dr("EmpSalary") = CSng(txtSalary.Text)
  End Sub
  Private Sub btnModify_Click(. . .) Handles btnEdit.Click
     Dim dr As DataRow
     Dim drs() As DataRow
     drs = dt.Select("empid=" & cmbId.Text)
     dr = drs(0)
     dr("Empname") = txtName.Text
     dr("EmpSalary") = CSng(txtSalary.Text)
  End Sub
  Private Sub btnDelete_Click(. . .) Handles btnDelete.Click
     Dim drs() As DataRow
     drs = dt.Select("empid=" & cmbId.Text)
     dr = drs(0)
     Dim res As MsgBoxResult
     res = MsgBox("Do you really want to delete the Row", MsgBoxStyle.YesNo, "Delete")
     If (res = MsgBoxResult.Yes) Then

Deccansoft Software Services – MS.NET
     End If
   End Sub
   Private Sub btnUpdateDatabase_Click(. . .) Handles btnUpdateDatabase.Click
        daEmp.Update(ds, "Emp"). ‘This method executes the Insert, Update or Delete Commands of the dataadapter using
the data from a given DataRow and the command executed depends upon the RowState property of the datarow.
     Catch exp As DBConcurrencyException ‘Please refer to the notes at the beginning of this handout.
        daEmp.Fill(ds, "Emp")
     End Try
   End Sub
 ‘Event Handler of DataAdapter.
   Private Sub daEmp_RowUpdated(ByVal sender As Object, ByVal e As SqlRowUpdatedEventArgs)
                                                                                      Handles daEmp.RowUpdated
     If (e.StatementType = StatementType.Insert) Then
        Dim con As SqlConnection
        con = e.Command.Connection ‘As the connection is already open it is not required to reopen the connection.
        Dim cmd As New SqlCommand("Select @@Identity", con)
        e.Row("EmpId") = cmd.ExecuteScalar() – EmpId of the record inserted is fetched and assigned to the current
                                                    rows EmpId field.
     End If
   End Sub

  Private Sub daEmp_RowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs)
                                                                            Handles daEmp.RowUpdating
    If e.StatementType <> StatementType.Delete Then
       If e.Row("EmpSalary") > 50000 Then
          MsgBox("Salary Cannot Be more than 50000")
          e.Status = UpdateStatus.SkipCurrentRow
       End If
    End If
  End Sub

Table Mapping : Used for giving DataTable and Column Names which can be different from backend table and field names.
  Dim con As New SqlConnection("server=.\sqlexpress;user id=sa;password=dss;database=Testing")
  Dim daEmp As SqlDataAdapter
  Dim ds As DataSet
  daEmp = New SqlDataAdapter("Select * from Emp;Select * from Dept", con)
  Dim dtmEmp As New Data.Common.DataTableMapping("Table", "Employee")
  Dim dcm As New Data.Common.DataColumnMapping("Empid", "ID")
  dtmEmp.ColumnMappings.Add("EmpName", "Name")
  dtmEmp.ColumnMappings.Add("EmpSalary", "Salary")

  Dim dtmDept As New Data.Common.DataTableMapping("Table1", "Department")
  dtmDept.ColumnMappings.Add("DeptId", "Id")
  dtmDept.ColumnMappings.Add("DeptName", "Name")

  ds = New DataSet()
  daEmp.Fill(ds) ‘Do not mention the DataTable name.

Deccansoft Software Services – MS.NET
  Dim dt As DataTable = ds.Tables(0)
  Dim str As String = ""
  str &= "Tablename : " & dt.TableName & vbCrLf
  For Each dc As DataColumn In dt.Columns
          str &= dc.ColumnName & vbTab
  Dim con As SqlConnection
  Dim ds As New DataSet()
  con = New SqlConnection("server=localhost\sqlexpress;user id=sa;password=dss;database=MSNET")
  Dim daDept As New SqlDataAdapter("select * from Dept", con)
  Dim daEmp As New SqlDataAdapter("select * from Emp", con)
  daDept.Fill(ds, "Dept")
  daEmp.Fill(ds, "Emp")
  Dim dr As DataRelation
  dr = New DataRelation("Dept_Emp", --Relationship Name
                           ds.Tables("Dept").Columns("DeptId"), // -- Parent Table column reference
                           ds.Tables("Emp").Columns("DeptId"),// - Child Table column refenrece
                           True) //– if true it also creates a foreign key along with DataRelation.
   Dim drDept As DataRow
   drDept = ds.Tables("Dept").Select("DeptId=" & 101)(0)
   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
   – 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 differently.
     Dim dt As DataTable
     dt = ds.Tables("Emp")
     Dim dv As DataView
     dv = dt.DefaultView ‘ or dv = New DataView(dt)
     dv.RowFilter = "EmpSalary > 2000"
     Dim str As String = ""
     Dim drv As DataRowView
     For Each drv in dv
        str &= drv(0) & " " & drv("EmpSalary") & vbCrLf
     dv.Sort = "EmpId"
     MsgBox(dv.Find(2)) 'Index of the row with EmpId=2
      dv.Sort = "EmpName,EmpSalary" ‘Sorted first using EmpName and if name is same sorts using EmpSalary
     MsgBox(dv.Find(New Object() {"E1", 1000}))‘Gets index of row with EmpName=’E1” & EmpSalary=1000
     dv.Sort = "EmpName"
     Dim drvs() As DataRowView = dv.FindRows("E1") ‘Gets the DataRowView of the row having EmpName=’E1’

Creating DataSet Programmatically
    DataSet ds = new DataSet()
    DataTable dt = new DataTable(“TableName”)
    ds.Tables.Add(dt) //Adds the table to the dataset.
    Dim dc As New DataColumn()

Deccansoft Software Services – MS.NET
     dc.ColumnName = "Amount"
     dc.DataType = GetType(Double)
     dc.Expression = "Price * Quantity" //Such columns are automatically based on other columns in the DataTable.
     dc.AutoIncrement = True : dc.AutoIncrementStep = 2 : dc.AutoIncrementSeed = 100
     dt.Columns.Add(dc) //Adds the column to the database.
Set Primary Key for a DataTable Programmatically
ds.Tables("Emp").PrimaryKey = New DataColumn(){ ds.Tables(“Emp”).Columns(“EmpId”) }
The datatype of PrimaryKey is DataColumn array because in some cases it might be composite key(multiple cols)
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.
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))
Async Execution of Command
     Dim dr As SqlDataReader
     Dim cmd As New SqlCommand
     Dim ar As IAsyncResult = cmd.BeginExecuteReader() ‘Begins the Asynchronous call
     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
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
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
End Sub

Deccansoft Software Services – MS.NET

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.

Deccansoft Software Services – MS.NET
                                                 XML Schema Document

                                              Yes         Is     Yes
          XML Instance              Well
                                                         valid                    Further processing
          Document                  formed

                                     No                  No

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

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

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

Deccansoft Software Services – MS.NET
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
    Dim enStuds As XmlElement
    enStuds = doc.DocumentElement
    Dim enStud As XmlElement
    enStud = CType(enStuds.ChildNodes(1), XmlElement)
    Dim anRollNo As XmlAttribute
    anRollNo = enStud.Attributes("RollNo")
    Dim enName As XmlElement
    enName = CType(enStud.FirstChild, XmlElement)
    Dim tnName As XmlText
    tnName = CType(enName.FirstChild, XmlText)
  End Sub

  Private Sub Form1_Load(. . .) Handles MyBase.Load
    doc = New XmlDocument()
    'doc.PreserveWhitespace = True
    enStuds = doc.DocumentElement
    Dim enStud As XmlElement
    For Each enStud In enStuds.ChildNodes
  End Sub

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

Deccansoft Software Services – MS.NET
  Private Sub btnNames_Click(. . .) Handles btnNames.Click
    Dim nlNames As XmlNodeList
    nlNames = doc.GetElementsByTagName("Name")
    Dim s As String = ""
    For Each enName As XmlNode In nlNames
       If (enName.NextSibling.FirstChild.Value = "Passed") Then
          s &= enName.FirstChild.Value & vbCrLf
       End If
  End Sub

  Private Sub btnAdd_Click(. . .) Handles btnAdd.Click
    Dim enStud, enName, enResult As XmlElement
    Dim tnName, tnResult As XmlText
    Dim anId As XmlAttribute
    enStud = doc.CreateElement("Student")
    enName = doc.CreateElement("Name")
    enResult = doc.CreateElement("Result")
    tnName = doc.CreateTextNode(txtName.Text)
    If (rbnPassed.Checked) Then
       tnResult = doc.CreateTextNode("Passed")
       tnResult = doc.CreateTextNode("Failed")
    End If
    anId = doc.CreateAttribute("RollNo")
    anId.Value = cmbId.Text
  End Sub

  Private Sub btnRemove_Click(. . .) Handles btnRemove.Click
    Dim enStud As XmlElement
    enStud = CType(enStuds.ChildNodes(cmbId.SelectedIndex), XmlElement)
  End Sub

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

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

Ex:     Student(RollNo,Name,Student_Id) - DataTable
        Name(FirstName,LastName,Student_Id) – DataTable
        Student_Name – DataRelation

Private Sub btnStudentDS_Click(. . .) Handles btnSimpleDS.Click
     Dim ds As New DataSet()
     'Dim strXML As String
     'strXML = ""
     'ds.ReadXml(New IO.StringReader(strXML))
     Dim dr As DataRow
     dr = ds.Tables("Student").NewRow
     dr("RollNo") = cmbId.Text
     dr("Name") = txtName.Text
     If (rbnPassed.Checked) Then
        dr("Result") = "Passed"
        dr("Result") = "Failed"
     End If
  End Sub

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

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

Private Sub btnXmlTextWriter_Click(. . .) Handles btnXmlTextWriter.Click
      Dim tw As New XmlTextWriter("c:\demo.xml", Nothing)
      tw.WriteAttributeString("att", "attvalue")
      tw.WriteElementString("E1", "TextInE1")
  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
           strCurrentName = xr.Value
        ElseIf (xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "Result") Then
           If (xr.Value = "Passed") Then
              strNames &= strCurrentName & vbCrLf
           End If
        End If
      End While
  End Sub
XPath is a specification for framing of Path so that we can get the reference to nodes in XMLDocument based on
their values.

 Private Sub btnGetAllNodes_Click(. . .)    Sample XPaths:
     Dim nl As XmlNodeList                  • E1/E2: All occurences of E2 which are immidiate child of E1.
     nl = doc.SelectNodes(txtPath.Text)     • E1/*/E2: All occurences of E2 which are grand children of E1 or
     Dim str As String = ""                    the ancistor of E1.
     For Each n As XmlNode In nl            • E1[1]: First occurance of E1.
        str &= n.Name & " - "               • E1[last()]: Last occurance of E1.
                 & n.OuterXml & vbCrLf      • E1[E2]: All occurance of E1 which has E2 as child.
     Next                                   • E1[E2 and E3 ]: All occurance of E1 which has both E2 and E3 as
     MsgBox(str)                               child.
 End Sub                                    • E1[E2 or E3 ]: All occurance of E1 which has either E2 or E3 as
                                            • E1[not E2]: All occurance of E1 where E2 is not a Child.
                                            • E1/@A1: All attributes by name A1 of E1 elements.
                                            • E1/@*: All attributes of element E1.

Deccansoft Software Services – MS.NET
                                          • E1/[@A1=’1’]: All occurances of E1 where attributes A1=1.
                                          • E1/text(): Returns all the text nodes under E1.

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 /

Steps to build a Windows Service
Go to File -> New Project -> Highlight Windows Service in the adjacent window
   protected override void OnStart(string[] args)
       StreamWriter sw
       sw = new StreamWriter(" c:\\windowsservice1.txt", true);
       sw.Write("Start: "+ DateTime.Now.ToString());
Similarly override the other methods as needed.

Now go to Design View of the Service -> Properties Window & change the ServiceName and other
properties as needed

Right click Design View -> Select Add Installer. Build the Windows Service Project.

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

Deccansoft Software Services – MS.NET

Installing / Deploying a windows Service in the OS.
Go to command prompt through Visual Studio Icon.
Goto debug directory of the WindowsService project where you have saved the file on the computer.

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

After typing the command & when u press enter , a new dailoguebox 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.

Launching a Windows Service
Go to Control Panel -> Administrative Tools -> Services.

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

Providing input to windows service by passing command line agruments
Select the windows services -> properties window and set the cmd line arguments in “ Start Parameters “
text box.
   protected override void OnStart(string[] args)
       StreamWriter sw
       sw = new StreamWriter(" c:\\windowsservice1.txt", true);
       sw.Write("Start: "+ DateTime.Now.ToString());
       if (args.Length > 0)
               sw.Write(“Argument “ + args[0]);

Service Controller
It is a windows application (with GUI) for controlling or managing or providing input to a Windows

Steps for developing Service Controller
   1. To the existing Solution Explorer, add a new Windows Application project.
   2. Go to tool box Component Tab drag & drop Service Controller.
   3. Go to the properties of service controller and set the name say sc.
   4. Add the appropriate buttons say Start, Stop, Pause & Continue.
   5. Make sure that the Machine name is ‘. ‘ (dot).
   6. Set the ServiceName of the Service Controller

Note: Before any action is submitted to the Windows Service, Service Controller must be refreshed.
Now make the Controller Application as the startup Project and Run it.

private void btnStart_Click(object sender, EventArgs e)

Deccansoft Software Services – MS.NET
    string[] args ={ "a1","a2" };

Sending Custom action to the windows service:
sc.ExecuteCommand (int.Parse (textBox1 .Text));
Note: To a Windows Service in the form of custom command a number can be submitted which must be
in the range of 128 to 255 only.

How to retrieve the Custom Command in a Windows Service class.
In Windows Service class Override OnCustomCommand
      StreamWriter sw;
      sw = new StreamWriter(" c:\\windowsservice1.txt", true);
      sw.WriteLine("Custom Command"+ Command.ToString());

N-Tier App
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                                         )
Create Procedure spDeleteEmpByPKIDs                                             @EmpName ,
@PKIDs Text AS                                                                  @EmpSalary ,
If (@PKIDs <> '')                                                               @DateOfBirth ,
  exec('DELETE FROM Emp WHERE PKEmpId in (' + @PKIDs + ')')                     @IsActive
                                                              Select @PKEmpId = @@IDENTITY
                                                              Else if @ActionType = 2
                                                                 UPDATE Emp
                                                                       EmpName = @EmpName ,
                                                                       EmpSalary = @EmpSalary ,
                                                                       DateOfBirth = @DateOfBirth ,
                                                                       IsActive = @IsActive

Deccansoft Software Services – MS.NET
                                          PKEmpId = @PKEmpId
                                        Else if @ActionType = 3
                                          DELETE FROM Emp WHERE
                                          PKEmpId = @PKEmpId

Deccansoft Software Services – MS.NET

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
     _EmpName = EmpName
     _EmpSalary = EmpSalary
     _DateOfBirth = DateOfBirth
     _IsActive = IsActive
  End Sub
  Private _PKEmpId As Int64
  Public Property PKEmpId() as Int64
               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
          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
               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
               Return( _DateOfBirth )
     End Get
     Set(ByVal value As Date)

Deccansoft Software Services – MS.NET
       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
             Return( _IsActive )
    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
End Enum
Public Class Helper
  Public Shared ReadOnly Property ConnectionString() As String
       Return ConfigurationManager.ConnectionStrings("EmpDB").ToString()
     End Get
  End Property
End Class

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

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

Deccansoft Software Services – MS.NET
       pDateOfBirth.Value = objEmp.DateOfBirth
       pIsActive.Value = objEmp.IsActive
    End If
    Dim AffectedRows As Integer = 0
    If trans Is Nothing Then
       AffectedRows = SqlHelper.ExecuteNonQuery(Helper.ConnectionString, CommandType.StoredProcedure,
                                  spName, pActionType, pPKEmpId, pEmpName, pEmpSalary, pDateOfBirth, pIsActive)
       AffectedRows = SqlHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure,
                                   spName, pActionType, pPKEmpId, pEmpName, pEmpSalary, pDateOfBirth,
    End If
    If ActionType = ActionType.Add Then
       Return CType(pPKEmpId.Value, Int64)
       Return AffectedRows
    End If
  End Function

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

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

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

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

Deccansoft Software Services – MS.NET
BO Class
Public Class EmpBO
  Public Function InsertEmp(ByVal objEmp As Emp) As Integer
     Return EmpDB.ManageEmp(nothing, objEmp, ActionType.Add)
  End Function
  Public Sub UpdateEmp(ByVal objEmp As Emp)
     EmpDB.ManageEmp(nothing, objEmp, ActionType.Modify)
  End Sub
  Public Sub DeleteEmp(ByVal objEmp As Emp)
     EmpDB.ManageEmp(Nothing, objEmp, ActionType.Delete)
  End Sub
  Public Function GetAllEmps(ByVal sortExpression As String) As DataSet
    return EmpDB.GetAllEmps(sortExpression)
  End Function
  Public Function GetEmpByPKID(ByVal PKID As Long) As Emp
     Dim objEmp As Emp = EmpDB.GetEmpByPKID(PKID)
     Return objEmp
  End Function
  Public Sub DeleteEmpByPKIDs(ByVal PKIDs As String)
  End Sub
End Class
  <add name="EmpDB" connectionString="server=.\sqlexpress;database=EmpDB;integrated security=true" />

 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
 End Sub
 Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
   Dim dlgEmp As New EmpDialog

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

 Public Property EmpName() As String
      Return txtName.Text
   End Get
   Set(ByVal value As String)
      txtName.Text = value
   End Set
 End Property
 Public Property EmpSalary() As Decimal
      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
      Return dtpDOB.Value
   End Get
   Set(ByVal value As Date)
      dtpDOB.Value = value
   End Set
 End Property
 Public Property IsActive() As Boolean
      Return chkIsActive.Checked

Deccansoft Software Services – MS.NET
   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

Deccansoft Software Services – MS.NET

Control Properties: Dock, Anchor

Label Properties: Text, Image, AutoSize, UseMnemonic

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

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

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

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

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.

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

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

Deccansoft Software Services – MS.NET

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.

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

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

MaskedTextBox Properties: Text, Mask, PromptChar

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

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

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

                                               Working with GDI
private void Form1_Paint(object sender, PaintEventArgs e)
   Graphics g = e.Graphics;
   int w, h;
   Point p1 = new Point();
   Point p2 = new Point();
   p1.X = 20; p1.Y = 30; p2.X = 50; p2.Y = 40;
   Pen p = new Pen(Color.Red);
   Color col = Color.Blue; // Color.FromArgb(255, 0, 0);
   Brush br; //Brush is an abstract class
   br = new SolidBrush(col);
   g.DrawRectangle(p, w / 2 - 50, h / 2 - 40, 100, 80);
   g.FillRectangle(br, 10, 10, 40, 50);
   Brush br1 = new HatchBrush(HatchStyle.DarkHorizontal, Color.Purple, Color.Green);
   Brush br2 = new LinearGradientBrush(p1, p2, Color.Indigo, col);

Deccansoft Software Services – MS.NET
  w = this.ClientSize.Width;
  h = this.ClientSize.Height;
  g.TranslateTransform(w / 2, h / 2);
  g.FillRectangle(br1, 40, 50, 60, 40);
  Image img = new Bitmap(@"c:\images\Blue Lace.bmp");
  g.DrawImage(img, 0, 0);
  Font fnt = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Italic);
  g.DrawString("Hello", fnt, br, 20, 30);
private void FigureForm_Resize(object sender, EventArgs e)
Graphics Form
Imports System.Drawing
Imports System.Windows
Imports System.Windows.Forms
Imports System.Runtime.Serialization.Formatters.Binary

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

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

  Private Sub GraphicsForm_Paint(. . .) Handles MyBase.Paint
    Dim g As Graphics
    g = e.Graphics
    For Each l As Line In lstLine
       g.DrawLine(Pens.Red, l.P1, l.P2)
  End Sub

  Private Sub GraphicsForm_MouseDown(. . .) Handles MyBase.MouseDown
    If (e.Button = MouseButtons.Left) Then
       l = New Line()
       l.P1 = e.Location
    End If
  End Sub

  Private Sub GraphicsForm_MouseUp(. . .) Handles MyBase.MouseUp
    If (e.Button = MouseButtons.Left) Then
       l.P2 = e.Location
    End If

  Private Sub btnSave_Click(. . .) Handles btnSave.Click
    Dim bf As New BinaryFormatter()
    Dim fs As New IO.FileStream(Application.StartupPath & "\demo.dat", IO.FileMode.OpenOrCreate)
    bf.Serialize(fs, lstLine)
  End Sub Private Sub btnLoad_Click(. . .) Handles btnLoad.Click
    Dim bf As New BinaryFormatter()
    Dim fs As New IO.FileStream(Application.StartupPath & "\demo.dat", IO.FileMode.Open)
    lstLine = CType(bf.Deserialize(fs), List(Of Line))

Deccansoft Software Services – MS.NET
  End Sub
  Private Sub btnClear_Click(. . .) Handles btnClear.Click
  End Sub
End Class
                                     Creating Image Programmatically
Bitmap bmp = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bmp);
g.FillEllipse(Brushes.Red, 0, 0, 200, 200);
g.DrawLine(Pens.Yellow, 0, 0, 200, 200);
g.DrawLine(Pens.Yellow, 0, 200, 200, 0);
bmp.Save("c:\\demo.gif", System.Drawing.Imaging.ImageFormat.Gif);

                                    Working with Menus and Dialogs
Two Types Of DialogBoxes:
   1. Model : Until it is disposed we cannot work with the parent form.
   2. Modeless: It always remains on top of the owner form but while it is open we can also work with the owner form.
Figure Form
Public Class FigureForm
  Enum FigureType
  End Enum
  Dim fig As FigureType
  Dim x, y As Integer
  Dim col As Color = Color.Red
  Private Sub FigureForm_Paint(. . .) Handles MyBase.Paint
     Dim g As Graphics
     g = e.Graphics
     g.TranslateTransform(0, msMain.Height)
     Dim p As New Pen(col)
     If (fig = FigureType.Rectangle) Then
        g.DrawRectangle(p, x, y, 100, 100)
        g.DrawEllipse(p, x, y, 100, 100)
     End If
  End Sub
  Private Sub mnuRectangle_Click(. . .) Handles mnuRectangle.Click
     fig = FigureType.Rectangle
  End Sub
  Private Sub mnuEllipse_Click(. . .) Handles mnuEllipse.Click
     fig = FigureType.Ellipse
  End Sub
  Private Sub mnuExit_Click(. . .) Handles mnuExit.Click
  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
        mnuRectangle.Checked = False
        mnuEllipse.Checked = True
     End If
  End Sub

Deccansoft Software Services – MS.NET
  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
     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
     End If
  End Sub
  Private Sub dlgDemo_FormClosed(ByVal sender As Object, ByVal e As
                                                                                     Handles dlgDemo.FormClosed
     dlgDemo = Nothing
  End Sub
End Class

Position Dialog                            Steps for Creating Modal DialogBox:
Public Class PositionDialog
                                               • Add a Windows Form to the project
                                               • Set the following Form Properties:
  Public Property X() As Integer
    Get                                             FormBorderStyle – FixedDialog
                                                    MinimizeBox – false
       Return CInt(txtLeft.Text)
                                                    MaximiseBox – false
    End Get
                                                    ShowInTaskBar – false
    Set(ByVal value As Integer)
                                                    StartPosition – CenterParent
       txtLeft.Text = value.ToString
    End Set                                    • Design the Dialog – Place all the controls, give them proper Name and set
  End Property                                 correct TabOrder (View->TabOrder).
                                               • Write the Validations for the controls on the DialogBox.
  Public Property Y() As Integer               • Add the Properties to the DialogBox class so that the Control Properties
    Get                                        are available to the form where the Dialog is used.
       Return CInt(txtTop.Text)                • Handle OK Click EventHandler
    End Get                                         Do the Form level Validations and if everything is Ok execute the
    Set(ByVal value As Integer)                following line
       txtTop.Text = value.ToString                 this.DialogResult=DialogResult.OK
    End Set                                    • Handle Cancel Click EventHandler
  End Property                                       this.DialogResult=DialogResult.Cancel

  Private Sub btnOK_Click(. . .) Handles btnOK.Click
    Me.DialogResult = DialogResult.OK
  End Sub
  Private Sub btnCancel_Click(. . .) Handles btnCancel.Click
    Me.DialogResult = DialogResult.Cancel
  End Sub

Deccansoft Software Services – MS.NET
End Class
DemoDialog (Modeless Dialog)
Public Class DemoDialog
  Private Sub btnOK_Click(. . .) Handles btnOK.Click
     btnApply_Click(Nothing, Nothing)
  End Sub
  Private Sub btnCancel_Click(. . .) Handles btnCancel.Click
  End Sub
  Private Sub btnApply_Click(. . .) Handles btnApply.Click
     Dim frmOwner As FigureForm
     frmOwner = DirectCast(Me.Owner, FigureForm) ‘Owner is of type Form and hence the casting.
     frmOwner.txtDemo.Text = Me.txtDemo.Text
  End Sub
  Public Property DemoText() As String
       Return txtdemo.text
     End Get
     Set(ByVal value As String)
       txtDemo.text = value
     End Set
  End Property
End Class
                                            ApplyButton Demo
Public Class ApplyForm
  Private Sub btnApply_Click(. . .) Handles btnApply.Click
     MsgBox("Changes Applied")
     btnApply.Enabled = False
  End Sub
  Private Sub ControlChanged(ByVal sender As Object, ByVal e As EventArgs)
     btnApply.Enabled = True
  End Sub
  Private Sub ApplyForm_Load(. . .) Handles MyBase.Load
  End Sub
  Private Sub RegisterEvents(ByVal parent As Control)
     For Each con As Control In parent.Controls
       If (TypeOf con Is TextBox) Then
          Dim txt As TextBox
          txt = CType(con, TextBox)
          AddHandler txt.TextChanged, New EventHandler(AddressOf ControlChanged)
       ElseIf (TypeOf con Is CheckBox) Then
          Dim chk As CheckBox
          chk = CType(con, CheckBox)
          AddHandler chk.CheckedChanged, New EventHandler(AddressOf ControlChanged)
       ElseIf (TypeOf con Is RadioButton) Then
          Dim chk As RadioButton
          chk = CType(con, RadioButton)
          AddHandler chk.CheckedChanged, New EventHandler(AddressOf ControlChanged)
       ElseIf (TypeOf con Is DateTimePicker) Then
          Dim chk As DateTimePicker
          chk = CType(con, DateTimePicker)
          AddHandler chk.ValueChanged, New EventHandler(AddressOf ControlChanged)
       ElseIf (TypeOf con Is Panel) Then
       ElseIf (TypeOf con Is TabControl) Then
          Dim tc As TabControl
          tc = CType(con, TabControl)
          For Each tp As TabPage In tc.TabPages

Deccansoft Software Services – MS.NET
      End If
  End Sub
End Class
                                                  MDI Form
    For the Form set IsMDI Container -> true
Public Class MdiMainForm
  Private Sub FigureToolStripMenuItem_Click(. . .)
     Dim frmFigure As New FigureForm
     frmFigure.MdiParent = Me
  End Sub
  Dim WithEvents frmGraphics As GraphicsForm
  Private Sub GraphicsToolStripMenuItem_Click(. . .)
     If (frmGraphics Is Nothing) Then
        frmGraphics = New GraphicsForm()
        frmGraphics.MdiParent = Me
     End If
  End Sub
  Private Sub frmGraphics_FormClosed(. . .) Handles frmGraphics.FormClosed
     frmGraphics = Nothing
  End Sub
  Private Sub CloseAllToolStripMenuItem_Click(. . .)
     For Each frm As Form In Me.MdiChildren
  End Sub
  Private Sub TestingToolStripMenuItem_DropDownOpening(. . .)
     If (TypeOf Me.ActiveMdiChild Is GraphicsForm) Then
        ActiveForGraphicsToolStripMenuItem.Enabled = True
        ActiveForGraphicsToolStripMenuItem.Enabled = False
     End If
  End Sub
  Private Sub TileHorzToolStripMenuItem_Click(. . .)
  End Sub
  Private Sub TileVertToolStripMenuItem_Click(. . .)
  End Sub

  Private Sub CascadeToolStripMenuItem_Click(. . .)
  End Sub
  Private Sub ArrangeIconsToolStripMenuItem_Click(. . .)
  End Sub
End Class
                                 Working with Custom Controls
Public Class NumTextBox
  Public Event NumberPressed As EventHandler
  Public Property Value() As Integer
       If (Me.Text = "") Then Return 0
       Return CInt(Me.Text)

Deccansoft Software Services – MS.NET
    End Get
    Set(ByVal value As Integer)
       Me.Text = CStr(value)
    End Set
  End Property
  Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
    If (AscW(e.KeyChar) < 48 OrElse AscW(e.KeyChar) > 57) Then
       e.Handled = True
    End If
  End Sub
  Protected Overridable Sub OnNumberPressed()
    RaiseEvent NumberPressed(Me, EventArgs.Empty)
  End Sub
End Class
OKCancel Button
[DefaultEvent("OKClick"), DefaultProperty("OKText ")]
public partial class OKCancelButton : UserControl
     public event EventHandler OKClick, CancelClick;

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

                             Adding Login Facility to the appliacation
Login Dialog
     private void btnLogin_Click(object sender, EventArgs e)
        DialogResult res = MessageBox.Show("Is user valid?", "Login", MessageBoxButtons.YesNo);
        if (res == DialogResult.Yes)
           this.DialogResult = DialogResult.OK;
           MessageBox.Show("Invalid username or password! Please try again");
Startup Class
  static class Program

Deccansoft Software Services – MS.NET
         public static void Main()
           LoginDialog dlgLogin = new LoginDialog();
           if (dlgLogin.ShowDialog() != DialogResult.OK)
           Application.Run(new MainForm());

Delegate & Events
Delegate is an object, using which one object can invoke the functionality of other object without knowing its ClassName or
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
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 which has int,int as
         parameters and returns int.
         public delegate int MathOperationHandler(int a,int b);

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

3.       To the Solution add a New Console Application project.
4.       Add the Reference to the CalculatorLibrary –
         Right click on project Add Reference Projects Select the Library Project and click on OK.
5.       Add the following code to the Console Application.
         class MyMath
            //This method is based on the declaration of MathOperationHandler Delegate
            public int Add(int a, int b)
               return a + b;
         class DelegateDemo

Deccansoft Software Services – MS.NET
         public static void Main()
           MyMath m = new MyMath();
           MathOperationHandler delMathOperation;
           delMathOperation = new MathOperationHandler(m.Add);
           Calculator cal = new Calculator(delMathOperation);
6.    Make DelegateDemo as the startup object and execute the application.

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

         Dim delMathOperation As New MathOperationHandler(AddressOf m.Add);
Note: One Delegate instance can encapsulate only one method reference.
An instance of delegate can be created, passing to the constructor, reference to the method, same as delegate signature.

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

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

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

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

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

     public partial class ChatForm : Form

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

5       To the Project Add a new Item WindowsForm (MainForm.cs)
            public partial class MainForm : Form
                 private void btnNewChatClient_Click(object sender, EventArgs e)
                    ChatForm frmchat = new ChatForm();
                 public static void Main()
                      Application.Run(new MainForm());
6       Make MainForm as the Starup Object and run the application.

                                                Working with Events
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 all the methods of the delegates are
Note: Delegates used for events must have Return Type ‘void’ (sub in VB)

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

                                 General Syntax for Delegates and Events.

Deccansoft Software Services – MS.NET
Delegate is itself a class and event is the member of the class.
1 Delegate Sub <DN>(<par-list>)
    delegate void <DN>(<par-list>)
2 class Demo
              Public Event <EN> as <DN> -- VB
              pubic event <DN> <EN>;-- CS

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

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

                                                  Anonymous Methods

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

Write the following line in Form’s Load event handler
button1.Click += new EventHandler(button1_Click)

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

                              VB.NET event handling using WithEvents / Handles

Dim WithEvents b1 as Button

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

End Sub

Note: The name of the event hander method can be anything.
An event handler registered using Handles can be unregistered at runtime using RemoveHandler.

                                          Design Pattern for Raising Events

public class AmountEventArgs : EventArgs                 public class ChatCoordinator
{                                                        {
         public readonly decimal Amount;                    public event MessageHandler ReceivedMessage;

Deccansoft Software Services – MS.NET
        public AmountEventArgs(decimal amount)         public void BroadCast(string message)
        {                                              {
                this.Amount = amount;                        AmountEventArgs e = new AmontEventArgs(msg);
        }                                                    OnReceivedMessage(e)
}                                                      }
public delegate MessageHndler                          protected overrides OnReceivedMessage
              (object sender, AmountEventArgs e)                                        (AmountEventArgs e)
                                                            if (ReceivedMessage != null)


To top