This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Saturday, November 29, 2008

Constructor vs Static Constructor in c#(CSharp)

A Constructor is usually used to initialize data. However Static Constructor is used to initialize only static members. Here I am just talking about the Constructors. How they get initialized and how they behave.

Things to know about Static Constructor

It is used to initialize static data members.

Can't access anything but static members.

Can't have parameters

Can't have access modifiers like Public, Private or Protected.
Now once you understand the above points, you can appreciate the difference between Static Class and Unstatic Class

Static Class cannot be instantiated unlike the unstatic class. You should directly access its Method via the ClassName.MethodName

A Program can't tell when it is going to load static class but its definitely loaded before the call.

A Static class will always have the static constructor and its called only once since after that its in the memory for its lifetime.

A Static class can contain only static members. So all the members and functions have to be static.

A Static class is always sealed since it cannot be inherited further. Further they cannot inherit form any other class (except Object)
Let's look at a normal Constructor

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


/* Note two things

1.Here you get to instantiate the Constructor in the code wherever you like.

2.If you debug you get to goto the Constructor and see what is being done.

*/
MyExample myExample = new MyExample();
myExample.Print();
}
}

public class MyExample
{
private static int StaticVariable ;
public MyExample()
{
if (StaticVariable < 10)
{
StaticVariable = 20;
}
else
{
StaticVariable = 100;
}
}


public void Print()
{
Console.WriteLine(StaticVariable);
}
}



Now consider this second example for static class

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


/* Note the following

1.You dont get to instantiate for sure so you dont know when the constructor was called.

2.Secondly you can access your method directly.

*/

//MyExampleStatic myExampleStatic = new MyExampleStatic();
MyExampleStatic.Print();
}
}


static class MyExampleStatic
{
private static int StaticVariable;
static MyExampleStatic()
{
if (StaticVariable < 10)
{
StaticVariable = 20;
}
else
{
StaticVariable = 100;
}
}
public static void Print()
{
Console.WriteLine(StaticVariable);
}
}


The point is that static member could be used only by a static Constructor or static Function. Hope you have a Static Constructing life.

Delegates in C# (Csharp)

delegate is a type-safe object that can point to another method (or possibly multiple methods) in the application, which can be invoked at later time.

A delegate type maintains three important pices of information :

The name of the method on which it make calls.

Any argument (if any) of this method.

The return value (if any) of this method.

Defining a Delegate in C#

when you want to create a delegate in C# you make use of delegate keyword.

The name of your delegate can be whatever you desire. However, you must define the delegate to match the signature of the method it will point to. fo example the following delegate can point to any method taking two integers and returning an integer.

public delegate int DelegateName(int x, int y);

A Delegate Usage Example

namespace MyFirstDelegate

{

//This delegate can point to any method,

//taking two integers and returning an

//integer.

public delegate int MyDelegate(int x, int y);

//This class contains methods that MyDelegate will point to.

public class MyClass

{

public static int Add(int x, int y)

{

return x + y;

}

public static int Multiply(int x, int y)

{

return x * y;

}

}

class Program

{

static void Main(string[] args)

{

//Create an Instance of MyDelegate

//that points to MyClass.Add().

MyDelegate del1 = new MyDelegate(MyClass.Add);

//Invoke Add() method using the delegate.

int addResult = del1(5, 5);

Console.WriteLine("5 + 5 = {0}\n", addResult);

//Create an Instance of MyDelegate

//that points to MyClass.Multiply().

MyDelegate del2 = new MyDelegate(MyClass.Multiply);

//Invoke Multiply() method using the delegate.

int multiplyResult = del2(5, 5);

Console.WriteLine("5 X 5 = {0}", multiplyResult);

Console.ReadLine();

}

}
}


Delegate ability to Multicast

Delegate's ability to multicast means that a delegate object can maintain a list of methods to call, rather than a single method
if you want to add a method to the invocation list of a delegate object , you simply make use of the overloaded += operator, and if you want to remove a method from the invocation list you make use of the overloaded operator -= .

Note: The Multicast delegate here contain methods that return void, if you want to create a multicast delegate with return type you will get the return type of the last method in the invocation list.


A Multicast Delegate Example


namespace MyMulticastDelegate

{

//this delegate will be used to call more than one

//method at once

public delegate void MulticastDelegate(int x, int y);

//This class contains methods that MyDelegate will point to.

public class MyClass

{

public static void Add(int x, int y)

{

Console.WriteLine("You are in Add() Method");

Console.WriteLine("{0} + {1} = {2}\n", x, y, x + y);

}

public static void Multiply(int x, int y)

{

Console.WriteLine("You are in Multiply() Method");

Console.WriteLine("{0} X {1} = {2}", x, y, x * y);

}

}

class Program

{

static void Main(string[] args)

{

//Create an Instance of MulticastDelegate

//that points to MyClass.Add().

MulticastDelegate del = new MulticastDelegate(MyClass.Add);

//using the same instance of MulticastDelegate

//to call MyClass.Multibly() by adding it to it's

//invocation list.

del += new MulticastDelegate(MyClass.Multiply);

//Invoke Add() and Multiply() methods using the delegate.

//Note that these methods must have a void return vlue

Console.WriteLine("****calling Add() and Multibly() Methods.****\n\n");

del(5, 5);



//removing the Add() method from the invocation list

del -= new MulticastDelegate(MyClass.Add);

Console.WriteLine("\n\n****Add() Method removed.****\n\n");

//this will invoke the Multibly() method only.

del(5, 5);

}

}

}


Delegate Covariance

Assume you are designing a delegate that can point to methods returning a custom class type:

//Define a delegate pointing to methods returning Employee types.

public delegate Employee EmployeeDelegate();

if you were to derive a new class from Employee Type named SalesEmployee and wish to create a delegate type that can point to methods returning this class type you would be required to define an entirely new delegate to do so


//a new delegate pointing to methods returning SalesEmployee types.

public delegate SalesEmployee SalesEmployeeDelegate();

Example

namespace MyEmployeesDelegate

{

//Define a delegate pointing to methods returning Employee types.

public delegate Employee EmployeeDelegate();

//a new delegate pointing to methods returning SalesEmployee types.

public delegate SalesEmployee SalesEmployeeDelegate();

class Program

{

public static Employee GetEmployee()

{

return new Employee();

}

public static SalesEmployee GetSalesEmployee()

{

return new SalesEmployee();

}

static void Main(string[] args)

{

EmployeeDelegate empDel = new EmployeeDelegate(GetEmployee);

Employee emp = empDel();

SalesEmployeeDelegate salesEmpDel = new SalesEmployeeDelegate(GetSalesEmployee);

SalesEmployee emp2 = salesEmpDel();

}

}

public class Employee

{

protected string firstName;

protected string lastName;

protected int Age;

public Employee()

{ }

public Employee(string fName, string lName, int age)

{

this.firstName = fName;

this.lastName = lName;

this.Age = age;

}



}

public class SalesEmployee : Employee

{

protected int salesNumber;

public SalesEmployee()

{ }

public SalesEmployee(string fName, string lName, int age, int sNumber): base(fName, lName, age)

{

this.salesNumber = sNumber;

}

}

}


It would be ideal to build a single delegate type that can point to methods returning either Employee or SelesEmployee types.
Covariance allows you to build a single delegate that can point to methods returning class types related by classical inheritance.

Delegate Covariance Example

namespace DelegateCovariance

{

//Define a single delegate that may return an Employee

// or SalesEmployee

public delegate Employee EmployeeDelegate();

class Program

{

public static Employee GetEmployee()

{

return new Employee();

}

public static SalesEmployee GetSalesEmployee()

{

return new SalesEmployee();

}

static void Main(string[] args)

{

EmployeeDelegate emp = new EmployeeDelegate(GetEmployee);

Employee emp1 = emp();

EmployeeDelegate empB = new EmployeeDelegate(GetSalesEmployee);

//to obtain a derived type you must perform an explicit cast.

SalesEmployee emp2 = (SalesEmployee)empB();

}

}

public class Employee

{

protected string firstName;

protected string lastName;

protected int Age;

public Employee()

{ }

public Employee(string fName, string lName, int age)

{

this.firstName = fName;

this.lastName = lName;

this.Age = age;

}

}

public class SalesEmployee : Employee

{

protected int salesNumber;

public SalesEmployee()

{ }

public SalesEmployee(string fName, string lName, int age, int sNumber): base(fName, lName, age)

{

this.salesNumber = sNumber;

}

}

}


I hope you are now have a good idea with the creation and usage of delegates types.

Generics in C# (CSharp)




Generics are the most useful C# 2.0 language extensions, beside Anonymous methods, Iterators, Partial types And Nullable types.

What are generics?

Generics permit classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate.

Why generics?

To well know the useful of generics lets examine the following code:

public class Stack
{
object[] items;
int count;
public void Push(object item) {...}
public object Pop() {...}
}


We use the object type to store any type of data. The above simple Stack class stores its data in an object array, and its two methods, Push and Pop, use object to accept and return data. While the use of type object makes the Stack class very flexible, it is not without drawbacks. For example, it is possible to push a value of any type, such a Customer instance, onto a stack.

However, when a value is retrieved, the result of the Pop method must explicitly be cast back to the appropriate type, which is tedious to write and carries a performance penalty for run-time type checking:

Stack stack = new Stack();
stack.Push(new Customer());
Customer c = (Customer)stack.Pop();


If a value of a value type, such as an int, is passed to the Push method, it is automatically boxed. When the int is later retrieved, it must be unboxed with an explicit type cast:

Stack stack = new Stack();
stack.Push(3);
int i = (int)stack.Pop();


Such boxing and unboxing operations add performance overhead since they involve dynamic memory allocations and run-time type checks.

A further issue with the Stack class is that it is not possible to enforce the kind of data placed on a stack. Indeed, a Customer instance can be pushed on a stack and then accidentally cast it to the wrong type after it is retrieved:

Stack stack = new Stack();
stack.Push(new Customer());
string s = (string)stack.Pop();


While the code above is an improper use of the Stack class, the code is technically speaking correct and a compile-time error is not reported. The problem does not become apparent until the code is executed, at which point an InvalidCastException is thrown.

With generics those problems are all solved. HOW...?

public class Stack
{
T[] items;
int count;
public void Push(T item) {...}
public T Pop() {...}
}


When the generic class Stack is used, the actual type to substitute for T is specified. In the following example, int is given as the type argument for T:

Stack stack = new Stack();
stack.Push(3);
int x = stack.Pop();


The Stack type is called a constructed type. In the Stack type, every occurrence of T is replaced with the type argument int. When an instance of Stack is created, the native storage of the items array is an int[] rather than object[], providing substantial storage efficiency compared to the non-generic Stack. Likewise, the Push and Pop methods of a Stack operate on int values, making it a compile-time error to push values of other types onto the stack, and eliminating the need to explicitly cast values back to their original type when they're retrieved.

Generics provide strong typing, meaning for example that it is an error to push an int onto a stack of Customer objects. Just as a Stack is restricted to operate only on int values, so is Stack restricted to Customer objects, and the compiler will report errors on the last two lines of the following example:

Stack stack = new Stack();
stack.Push(new Customer());
Customer c = stack.Pop();
stack.Push(3); // Type mismatch error
int x = stack.Pop(); // Type mismatch error


It was a breif introduction to generics that will be included in the next version of C# (V 2.0) . which is available now on its beta version with Visual C# 2005 Express Edition Beta 1.

Wednesday, November 19, 2008

Currency Converter Custom Control using asp.net

Currency Converter Custom Control using asp.net

This article describes the details for constructing a custom ASP.NET 2.0 composite control used to convert one form of currency into another. The control consumes a public web service in order to calculate the exchange rate and uses the exchange rate returned from the web service to calculate the value of the exchanged currency.


DOWNLOAD THE SOURCE CODE

How to add alert Javascript coding for Gridview using asp.net

How to add alert Javascript coding for Gridview using asp.net

Sometimes we need to confirm from the client side, whether to proceed for deletion operation or not? We can use this code snippet to add that alert box to the user.

Declarations:
none


CODE:
Protected Sub gvcat_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvcat.RowDataBound

' This block of code is used to confirm the deletion of current record.
If e.Row.RowType = DataControlRowType.DataRow Then
Dim l As Object
' e.Row.Controls(4) is Delete button.
l = e.Row.Controls(4)
l.Attributes.Add("onclick", "javascript:return confirm('Are you sure to delete?')")
End If
End Sub

Creating and Calling a Web Service in Asp.Net

Creating and Calling a Web Service in Asp.Net

Demostrates how Web Service is created and called in Dot Net framework with a simple nice example.
Check it out!
Make sure to read the important info text file to follow the instructions.

CLCIKHERE TO DOWNLOAD SOURCE CODE

asp.net code to Export data grid to Excel

asp.net code to Export data grid to Excel


CLICKHERE TO DOWNLOAD SOURCE CODE

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger