Monday, 24 February 2014

Abstract Class C#


Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes.
Abstract class members marked as abstract must be implemented by derived classes.
The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.

For example: A class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. In C#, System.IO.FileStream is an implementation of the System.IO.Stream abstract class.


abstract class ShapesClass
{
 abstract public int Area();
}
class Square : ShapesClass
{
 int side = 0;
 
 public Square(int n)
 {
 side = n;
 }
 // Override Area method 
 public override int Area()
 {
 return side * side;
 }
}
 
class Rectangle : ShapesClass
{
 int length = 0, width=0;
 
 public Rectangle (int length, int width)
 {
 this.length = length;
 this.width = width;
 }
 // Override Area method 
 public override int Area()
 {
 return length * width;
 }
}
Abstract Class Features:
  • An abstract class cannot be instantiated.
  • An abstract class contain abstract members as well as non-abstract members.
  • An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
  • A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
  • An abstract class can be inherited from a class and one or more interfaces.
  • An Abstract class can has access modifiers like private, protected,internal with class members.But abstract members cannot have private access modifier.
  • An Abstract class can has instance variables (like constants and fields).
  • An abstract class can has constructors and destructor.
  • An abstract method is implicitly a virtual method.
  • Abstract properties behave like abstract methods.
  • An abstract class cannot be inherited by structures.
  • An abstract class cannot support multiple inheritance.
Usage:
  • Need to create multiple versions of your component since version is not a problem with abstract class. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.
  • Need to to provide default behaviors as well as common behaviors that multiple derived classes can share and override.

Boxing and Unboxing


Boxing
Implicit conversion of a value type (int, char etc.) to a reference type (object), is known as Boxing. In boxing process, a value type is being allocated on the heap rather than the stack.

Unboxing
Explicit conversion of same reference type (which is being created by boxing process); back to a value type is known as unboxing. In unboxing process, boxed value type is unboxed from the heap and assigned to a value type which is being allocated on the stack.

Real Life Example
int i = 10;
ArrayList arrlst = new ArrayList();

//ArrayList contains object type value

//So, int i is being created on heap

arrlst.Add(i); // Boxing occurs automatically

int j = (int)arrlst[0]; // Unboxing occurs

Tuesday, 4 February 2014

Hosting the MVC application using the IIS

IIS, to run the application instead of the built-in asp.net development server.




1. In the solution explorer, right click on the project and select "Properties"
2. Click on "Web" tab
3. Select "Use Local IIS Web Server" radio button
4. Notice that the Project Url is set to http://localhost/MVCDemo by default
5. Finally click on "Create Virtual Directory" button

Creating the first MVC application

Follow the steps as shown and create your hello world mvc application

1.
 Open visual studio
2. Click File > New Project
3. Select "Web" from "Installed Templates" section
4. Select ASP.NET MVC 4 Web Application(If dont have  MVC 4 means then ASP.NET MVC 3) 
5. Set Name="MVCDemo"
6. Click OK
7. Select "Empty" template. Select "Razor" as the ViewEngine. There are 2 built in view engines - Razor and ASPX. Razor is preferred by most mvc developers
8. At this point you should have an mvc application created.

Notice that in the solution explorer, you have several folders - Models, Views, Controllers etc. As the names
 suggest these folders are going to contain Models, Views, and Controllers. 

·         Model , which represents the underlying, logical structure of data in a software application and the high-level class associated with it. This object model does not contain any information about the user interface.
·         View , which is a collection of classes representing the elements in the user interface (all of the things the user can see and respond to on the screen, such as buttons, display boxes, and so forth)
·         Controller , which represents the classes connecting the model and the view, and is used to communicate between classes in the model and view.
Now let's add a controller to our project
1. Right Click on "Controllers" folder
2. Select Add > Controller
3. Set Controller Name = HomeController
4. Leave rest of the defaults and click "Add"
We should have HomeController.cs added to "Controllers" folder. 

At this point run the application by pressing CTRL+F5. Notice that you get an error as shown below.

The First type is as follows
 Right click on the Index() function and Click on "Add View"
Notice that, the view name in "Add View" dialog box matches the name of the controller action method.Select "Razor" as the view engine.Leave the rest of the defaults as is, and click "Add" button.

The Second type is as follows
The following is the function that is automatically added to HomeController class.

public ActionResult Index()
{
    return View();
}

Change the return type of Index() function from "ActionResult" to "string", and return string "Hello from MVC Application" instead of View().

public string Index()
{
    return "Hello from MVC Application";
}

Run the application and notice that, the string is rendered on the screen. When you run the application, by default it is using built-in asp.net development server. In the URL "Home" is the name of the controller and "Index" is the method within HomeController class.
 Finally your first hello world MVC application has been completed

Tuesday, 17 December 2013

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack


Solved : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

Reason:
     The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally.
Solution: 
For Response.Redirect
Response.Redirect ("nextpageURL.aspx", false);
For Server.Transfer
Use Server.Execute instead of Server.Transfer

Separate characters in a string using SPLIT and ARRAY

The SPLIT will separate the characters using a unique character or symbol like + , . @_ - etc. and store values in the ARRAY.

Example :

protected void buttonname_Click(object sender, EventArgs e)
{
string a = It-is-a-demo;

string[] b = a.split('-');  //It will split the characters after each of this special character underscore -

string split1 = b[0];  //It will be stored using the array
string split2 = b[1];  // is will be stored
string split3 = b[2];  // a will be stored
string split4 = b[3];  // demo will be stored

lbllaststring.Text = split4;  //demo will be displayed in the label
}

Saturday, 27 July 2013

How to Create the SQL Stored Procedure


Open the SQL server 2005 or 2008 and then create a database and the tables and add the fields as your requirement.Here in this example I use two fields.

ID  int
Name  Varchar(50)

Now Click the New query and the Type following Stored Procedure :

USE [DBNAME]

GO

CREATE procedure Sp_Tblname
@ID int=0,
@Name varchar(50)=null,
@Mode varchar(100)=null
as
begin

if @Mode='Insert'
Insert into Tblname(Name) values (@Name)
end

if @Mode='Update'
begin
update Tblname set Name=@Name where ID=@ID
end

if @Mode='Delete'
begin
Delete from Tblname where ID=@ID
end

if @Mode='Selectall'
begin
Select * from Tblname
end

end

This is the simple stored procedure created using sql.After completing it u can Execute it.
After this u can call these parameters and stored procedure as shown in this link http://sathyadotnet.blogspot.in/search/label/3-Tier%20Architecture.
back to top