Search This Blog

29 June, 2010

Types of Application

Before you start.........
You should be aware what is a program. What all are the key factors in a program...
So first lets start with the Basics.....

We all are creating Applications. So lets check what all are the functions performed by an Application.

The functions performed by an application can be divided into three categories

User services
Business services and
Data Services


User Services layer constitutes the front end of the solution. It is also called the Presentation Layer because it provides the interactive user interface.

The business services layer controls the enforcement of business rules on the data of an organization.
Business rules encompass those practices and activities that define the behavior of an organization.

The data services layer comprises of the data and the functions for manipulating this data.



TYPES OF APPLICATIONS


Applications may vary from Single-tier desktop applications to multi-tier applications (two, three or n-tier architecture).

SINGLE-TIER Architecture

In a single-tier architecture a single executable file handles all functions relating to the user, business and data service layers. Such an Application is also called Monolithic applications.

TWO-TIER Architecture

The tow-tier architecture divides an application into two components:-
Client - Implements the user interface
Server - Stores Data
In case for this architecture the user and data services are located separately either on same machine or in separate machine.

THREE-TIER Architecture

In case of Three-Tier architecture all the three service layers reside Separately, either on the same machine or on different machine. The user interface interacts with the Business logic. Business logic validates the data sent by the interfaces and forwards it to the database if it confirms to the requirement.

n-TIER Architecture

An n-tier application uses business objects for handling business rules and data access. It has multiple servers handling business services. This application architecture provides various advantages over other types of application architectures. The advantages include extensibility, resilience to change, maintainability and scalability of the application.

What is Object Oriented Programming?

So, Lets start from the First. Before we all start one must be aware of what is Object Oriented Programming? So do you know what is Object Oriented Programming?
Object Oriented Programming(OOP)

Object Oriented Programming was first introduced in 1960’s.The main reason behind this programming is to make the program user friendly. OOP concept is related to the real-world system (i.e concept of Classes and Objects).


What is a CLASS?
Class is just a Virtual concept of a collection of related items together which we cannot see or feel.In programming we create Classes to define collection of related variables and functions.
Class is a user defined Data Type


What is an Object?
In simple words -- Object is an Instance of a Class. We can access a Class only through its Objects.Without objects we cannot say a Class is existing.A thing which has the properties of a class is known as Objects.

For eg:- Car is a Class and Tata Indica is an Object of the class Car
Objects….


We generally identify object by its State, Behavior and Identity.
If we take a car Indica its state is its color, wheel, body, etc….

Its Behavior is moving, changing gears etc…
And the identity of the car is its Number lets say KL-7 AD 2544.

Here state and behavior may be same for many objects but the identity of object will not be same for other object.


Characteristics of OOProgramming…


The main characteristics of Object Oriented Programming are:-

Inheritance
Encapsulation
Abstraction
Polymorphism


INHERITANCE

The ability of a class (Derived Class) to derive or Inherit the properties of another class (Base Class) is called Inheritance.Inheritance helps in the reusability of code hence making the program smaller and easy to use.
Eg:- Parent child relationship. Child is Inherited from its parents


ENCAPSULATION


Process of Hiding the data from a user is called Encapsulation; also called Data Hiding.


ABSTRACTION


The process of showing essential features without giving any explanations is known as Abstraction.


POLYMORPHISM


The name Poly Morphism means Many Forms. It is the process by which we create same things with different behaviors.
Eg:- Function overloading…

Access Specifiers in C#

What is an access specifier?

An access specifier defines the scope of a class member. A class member refers to the variables and functions in a class. A program can have many classes. The programmer gets the privilege to decide the of use access specifier to implement encapsulation and abstraction in C#.

So, when we use Access Specifiers in C# they help the other class members to know how they can access the methods or variables declared/defined inside another class.

C# supports five types of access specifiers to tell the extent of visibility of a class member. They are:-

public,
private,
protected,
internal and
protected internal.

Let's now go into detail.

public:

The public access specifier in C# allows a class to expose its member variables and member functions to other functions and objects. So any member declared public can be accessed outside the class also.

Let's check an example for this..
----------------------------------------------------------------

using System;
namespace HelloWorld
{
class hello
{
public int iNum1;
}
class MyMainClass
{
static void Main(string[] args)
{
hello HelloObject=new hello();
HelloObject.iNum1=10; /* since variable iNum1 is public it can be accessed in other classes also*/

Console.WriteLine(HelloObject.iNum1);
}
}
}

----------------------------------------------------------------

private:

The private access specifier in C# is just opposite to the public access specifier. That is it allows a class to hide its member variables and member functions from other class objects and functions. So it is not visible outside the class. By default, the access specifier is private; if public, private or protected is not specified.

Let's check an example for this..
----------------------------------------------------------------

using System;
namespace HelloWorld
{
class hello
{
public int iNum1;
private int iNum2;

public hello()
{
iNum1=0;
iNum2=10;
}
}

class MyMainClass
{
static void Main(string[] args)
{
hello HelloObject=new hello();
//CORRECT METHOD
HelloObject.iNum1=10; //Here since variable iNum1 is public it can be accessed in other classes also

//WRONG METHOD
HelloObject.iNum2=20; /*This line will return an Error since the access to this variable is Private. So it cannot be accessed outside the class*/

Console.WriteLine(HelloObject.iNum1);
}
}
}

----------------------------------------------------------------

protected:

The protected access specifier in C# allows a class to hide its member variables and member functions from other class objects and functions, except the child class. This access specifier is used when we need to use Inheritance in the program.

Let's check an example for this..
----------------------------------------------------------------

using System;
namespace HelloWorld
{
class hello
{
public int iNum1;
protected int iNum2;
}

class world : hello
{
public int AddDetails()
{
iNum1=20;
iNum2=10;

return iNum1+iNum2;
}
}

class MyMainClass
{
static void Main(string[] args)
{
world worldObject=new world();

worldObject.iNum1=50; //Line 1 No Error

worldObject.iNum2=10; //Line 2 Error

Console.WriteLine(worldObject.AddDetails().ToString());

}
}
}

----------------------------------------------------------------
In the above case we have not called the object of the class hello. But we have inherited the class hello to the class world. So all the public and protected members inside the class is accessible inside the derived class also. We don't need to create objects for the base class to access the variables or methods we have in our parent class.
Now inside the Main() function we are assigning value to iNum1 and iNum2. Line 1 will not return any error because the variable iNum1 is public. So, it can be accessed from anywhere in the program, but Line2 will give an Compilation Error saying "iNum2 is inaccessible due to protection level". This is because access of the variable iNum2 is set to protected. The protected members can only be accessed inside the Child class and its parent class.


internal:

The internal access specifier in C# allows a class to expose its member variables and member functions to other function and objects. It can be accessed from any class or method defined with the application in which the member is defined. The default access specifier is internal for a class.

protected internal:

The protected internal access specifier in C# allows methods or member variables accessible to a class and its derived classes inside the same assembly or namespace within a file. These members cannot be accessed from class inside another assembly or a file.


Lets now understand the above with an example:
----------------------------------------------------------------
using System;
class Student
{
private string sAddress;
protected string sPhNo;
protected internal long iZipCode;
void Hello()
{
Console.WriteLine(“Hello World!”);
}
internal void Hello2()
{
Console.WriteLine(“Hello Student!”);
}
public void SetAddress()
{
Console.WriteLine(“Enter your Address:”)
sAddress = Console.ReadLine();
}
public void SetPhNo()
{
Console.WriteLine(“Enter your Phone Number:”)
sPhNo = Console.ReadLine();
}
public void SetZipCode()
{
Console.WriteLine(“Enter the Zip Code: “);
iZipCode =Convert.ToInt64(Console.ReadLine());
}
public void DisplayStudent()
{
Console.WriteLine(“The Address is: {0}”, sAddress);
}
}
class Display
{
static void Main(string[] args)
{
Student John = new Student();
Console.WriteLine(John.sAddress); //Error: protected members cannot be accessed
John.SetAddress(); //public members can be accessed outside the class definition
John.SetPhNo(); //public members can be accessed outside the class definition
Console.WriteLine(John.sPhNo); //error: protected internal members cannot be accessed outside the class definition
John.SetZipCode(); //public members can be accessed outside the class definition
John.DisplayStudent(); // public members can be accessed outside the class definition
Console.WriteLine(John.sAddress); //error: private members cannot be accessed outside the class definition
John.Hello(); //error: private members cannot be accessed outside the class definition
John.Hello2(); //displays Hello student!
Console.ReadLine();
}
}
----------------------------------------------------------------

C# Array

Arrays are collection of Elements having the same DataType stored in Consecutive memory locations. Arrays can only store data of the same type ie, if it’s an integer array the array can only store Integer values. It will not allow any other types to be stored.

Let’s see how we can declare an array in C#.

If you need to create a character array to store ten elements then we can declare it like this…

char[] cArr=new char[10];

To pass values to an Array at the time of declaration we can write an array like this…

char[] cArr={‘H’,’e’,’l’,’l’,’o’,’!’};

Since this is a character array we can only pass single values.

We’ll now see how we create arrays for each type..

Integer Array in C#
--------------------

int[] iArr=new int[10];
or
int[] iArr={1,2,3,4,5};

String Array in C#
-------------------
string[] sArr=new string[10];
or
string[] sArr={“Hello”,”world”};

In an array.. items are stored in index of the array. For retrieving values in an array we must mention the index. Always the first index of an array will be 0 and the last index will be the size of the index minus 1 ie if the size is 10 for an array the last index will be 9.

In the above case of the string array the values “Hello” and "World" will be stored in two index of the array. The index of the “Hello” will be 0 and “World” will be 1.
If we need to just print hello on the screen then we must have to write..

Console.WriteLine(sArr[0]);

Here in the above code sArr is the Array name and 0 is used to mention the Element in which index should be selected likewise if we need to print World then we must have to say sArr[1]..

Now we will check how we can print all items in an Array. For doing this we can use two types of loops.
The first one is for loop…

For loop to display Elements in an Array
-----------------------------------------

for(int i=0;i
{
Console.WriteLine(sArr[i]);
}

Here in the above case we have written int i=0 in the for loop and for printing we have just written it sArr[i] here we says I because its having the values from 0 to the value less than the length of the array.

So, when the loop works for the first time its value will be 0 so it prints “Hello” and when it goes for the second time the value is 1 so it prints “World” and when it goes for the third time the value is not less than 2 ie the length is two for the array and now the value of I is also two so it terminates…..

When we use for loop it have a drawback ie we must know the size of the array. For curing that we have another loop ie foreach loop in c#.

In a foreach loop its not necessary to know the size of the array…
We’ll just see one foreach loop for the same array…

Foreach loop to display items in an array..
-------------------------------------------

foreach(string s in sArr)
{
Console.WriteLine(s);
}

In the above code we are using foreach loop if just see the two loops itself we can see the difference. In a Foreach loop we don’t need to mention the array size to be printed…. When we run the foreach loop it will store the first element in string variable s and prints it and next time it will automatically gets incremented to the next element and prints the second element…. Likewise it will run till the array terminates..

Destructor in C#

C# Destructors are used to destroy an object of a Class. Destructors are automatically invoked when the Scope of the Object goes out. We represent a Destructor with a tilde(~) symbol. Destructor will also be having the Same name of the class.
If there is a class with the name "Hello" we can represent its destructor as
~Hello()


Destructors cannot be overloaded, Inhertied or have Modifiers defined to it.
we can see a Simple program..


-------------------------------------------------
using System;


namespace Destructorss
{
class One
{
//Constructor
public One()
{
Console.WriteLine("First Constructor Called");
}


//Destructor

~One()
{
Console.WriteLine("First destructor is called.");
}
}


class Two : One
{
//Constructor
public Two()
{
Console.WriteLine("Second Constructor Called");
}
//Destructor
~Two()
{
Console.WriteLine("Second destructor is called.");
}
}


class Three : Two
{
//Constructor
public Three()
{
Console.WriteLine("Third Constructor Called");
}
//Destructor
~Three()
{
Console.WriteLine("Third destructor is called.");
}
}


class MyDestructor
{
static void Main()
{
Three t = new Three();
}


}
}


/*The result of this Program will be:-
First Constructor Called
Second Constructor Called
Third Constructor Called


Third destructor is called.
Second destructor is called.
First destructor is called.
*/


-------------------------------------------------


In the above program we have constructors and Destructors. Constuctors invoke from the First to the Third while Destructors go just in the Opposite way ie it starts from Third destrucor and goes to First....