Zonix Website Zonix Software Solutions - These blogs are written by the software professionals working at zonix software solutions

Events and Delegates

June 14, 2008 18:40 by Admin

Visit: http://www.zonixsoft.com

Delegate

Delegates act as an intermediary between an event source and an event destination. Technically, a delegate is a reference type used to encapsulate a method with a specific signature and return type. You can encapsulate any matching method in that delegate. To be even precise delegates are similar to function pointers. They can be called as type safe function pointers. Unlike function pointers, delegates are object-oriented and type safe. A delegate class used as an intermediary between an event source and event receiver is called an event handler.  

Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value

The signature of a single cast delegate is shown below:

delegate result-type identifier ([parameters]);

where:

·       result-type: The result type, which matches the return type of the function.

·       identifier: The delegate name.

·       parameters: The Parameters, that the function takes.

There are three steps in defining and using delegates:

·       Declaration

·       Instantiation

·       Invocation

Declaration

A delegate is created with the delegate keyword, followed by a return type and the signature of the methods that can be delegated to it, as in the following:  

public delegate int MyDelegate(object obj1, object obj2);

This declaration defines a delegate named MyDelegate, which will encapsulate any method that takes two objects as parameters and that returns an int. Once the delegate is defined, you can encapsulate a member method with that delegate by instantiating the delegate, passing in a method that matches the return type and signature. The delegate can then be used to invoke that encapsulated method.  

Instantiation

In order to make use of this delegate, you need to instantiate it, to specify the method that needs to be called. 

public

void MyMethod()
{
MyDelegate a = new MyDelegate(MyDelegateMethod);
}Here MyDelegateMethod is a method that has a signature similar to that of MyDelegate.

Invocation

A delegate is used to invoke a method similar to how a method call is made.

For example:  MyDelegateMethod("This is a test invocation"); 

Types of Delegates 

There are basically two types of delegates. Single Cast delegate and Multi-Cast delegate. A single cast delegate can call only one function. A multi-cast delegate is one that can be part of a linked list. The multi-cast delegate points to the head of such a linked list. This means that when the multi-cast delegate is invoked it can call all the functions that form a part of the linked list. Assume that one has several clients who would like to receive notification when a particular event occurs. Putting all of them in a multi-cast delegate can help call all the clients when a particular event occurs.  

To support a single cast delegate the base class library includes a special class type called System.Delegate. To support multi-cast delegates the base class library includes a special class type called SystemMultiCastDelegate. 

Events

An event in is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. The most familiar use for events is in graphical user interfaces; typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button). An event is the outcome of an action. There are two important terms with respect to events. The event source and the event receiver. The object that raises the event is called event source and the object that responds to the event is called event receiver. The communication channel between an event source and an event receiver is the delegate. 

Events and Delegates

Declaring an event is directly tied to a delegate. A delegate object encapsulates a method so that it can be called anonymously. An event is a mechanism by which a client class can pass in delegates to methods that need to be invoked whenever "something happens". When it does, the delegate(s) given to it by its clients are invoked.

To declare an event in C#, use the following syntax:public delegate void testDelegate(int a);
public event testDelegate MyEvent;

Once an event is declared, it must be associated with one or more event handlers before it can be raised. An event handler is nothing but a method that is called using a delegate. Use the += operator to associate an event with an instance of a delegate that already exists.

For example:

Myform.MyEvent += new testEvent(MyMethod);An event handler may also be detached as follows:

MyForm.MyEvent -= new testEvent(MyMethod);

In C#, events may be raised by just calling them by name similar to method invocation, say MyEvent(10). 

How Event works?

Whenever an event is defined for a class, the compiler generates three methods that are used to manage the underlying delegate: 

add_<EventName> :
this is a public method that calls the static Combine method of System.Delegate in order to add another method to its internal invocation list. This method is however not used explicitly. The same effect is achieved by using the += operator as specified before.  

remove_<EventName> :
this is also a public method that calls the static Remove method of System.Delegate in order to remove a receiver from the event's invocation list. This method is also not called directly. Its job is done by the "-=" operator. 

raise_<EventName> :
a protected method that calls the compiler generated Invoke method of the delegate, in order to call each method in the invocation list


Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

New in C-Sharp 3.0

June 14, 2008 18:38 by Admin

Visit: http://www.zonixsoft.com

New in C-Sharp 3.0

New in C# 3.0
This article discusses the following major new enhancements expected in C# 3.0:
• Implicitly typed local variables
• Anonymous types
• Extension methods
• Object and collection initializers
• Lambda expressions
• Query expressions
• Expression Trees

Implicitly typed local variables:
C# 3.0 introduces a new keyword called "var". Var allows you to declare a new variable, whose type is implicitly inferred from the expression used to initialize the variable.
Syntax: var i=1;
The preceding line initializes the variable i to value 1 and gives it the type of integer. It is not an object or variant.
Anonymous types:
C# 3.0 gives you the flexibility to create an instance of a class without having to write code for the class beforehand. So, you now can write code as shown below:
        new {StudentID=1, StudentName=”XYZ”, Marks=90}
The preceding line of code, with the help of the "new" keyword, gives you a new type that has three properties: StudentID, StudentName, and Marks. Behind the scenes, the C# compiler would create a class that looks as follows:
        class __Anonymous1
        {
           private int _StudentID = 1;
           private string _StudentName = "XYZ";
           private int _Marks = 64;
           public int StudentID {get { return _StudentID; } set { _StudentID = value; }}
           public string StudentName {get { return _StudentName; } set { _StudentName = value; }}
           public int Marks {get { return _Marks; } set { _Marks = value; }}
        }

Extension methods:
                
Extension methods enable you to extend various types with additional static
methods. Extension methods can be declared only in static classes and are identified by
the keyword "this" as a modifier on the first parameter of the method.
 
The following is an example of a valid extension method:
        public static int ToInt32(this string s)
        {
           return Convert.ToInt32(s) ;
        }
Object and collection initializers:
C# 3.0 is expected to allow you to include an initializer that specifies the initial values of the members of a newly created object or collection. This enables you to combine declaration and initialization in one step.
For instance, if you defined a CoOrdinate class as follows:
public class CoOrdinate
{
     public int x;
     public int y;
}

You then could declare and initialize a CoOrdinate object using an object initializer, like this:

          var myCoOrd = new CoOrdinate{ x = 0, y= 0} ;
You should easily be able to give values to collections in a rather concise and compact manner in C# 3.0.
In C# 2.0 Code we write :

List<string> animals = new List<string>();

     animals.Add("monkey");
     animals.Add("donkey");
     animals.Add("cow");
     animals.Add("dog");
     animals.Add("cat");

 It can be written in  C# 3.0 shortened to simply.
        List<string> animals = new List<string> {"monkey", "donkey", "cow", "dog", "cat" } ;
Lambda expressions:
C#(1.x) allowed you to write code blocks in methods, which you could invoke easily using delegates.Delegates are definitely useful, and they are used throughout the framework, but in many instances you had to declare a method or a class just to use one. Thus, to give you an easier and more concise way of writing code, C# 2.0 allowed you to replace standard calls to delegates with anonymous methods.
 In C# 2.0, using anonymous methods, you could rewrite the code as follows:
        class Program
        {
           delegate void DemoDelegate();
           static void Main(string[] args)
           {
                DemoDelegate myDelegate = delegate()
                        {
                                Console.Writeline("Hiya!!");
                        };
                myDelegate();
          }
        }
The above code can now be replaced with the following code in C# 3.0:
        class Program
        {
          delegate void DemoDelegate();
          static void Main(string[] args)
          {
                DemoDelegate myDelegate = () => Console.WriteLine("Hiya!!") ;
                myDelegate();
          }
        }
       
Reference Site: http://www.developer.com/net/csharp/article.php/10918_3561756_1
 

 


Currently rated 3.7 by 3 people

  • Currently 3.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Web Parts

June 14, 2008 18:35 by Admin

Visit: http://www.zonixsoft.com

This article discuss about the Web Parts, new feature introduced in ASP.NET 2.0. Web Parts are objects which the end user can open, close or move from one zone of the page to another. Web Parts allows for personalization of page content. They allows users to move or hide the Web Parts and add new Web Parts changing the page layout.

Web Parts Modes

Modes are very powerful in that they enable user to edit Web Parts, delete the Web Parts or customize Web Parts.

m    a) Normal mode: End user cannot edit or move sections of page. Simple Browser mode.

m    b) Edit Mode: End user can edit Web Parts on the page including Web Parts title, setting custom properties.

m    c) Design Mode: End user can rearrange the order of the page Web Parts in a WebPartZone.

m    d) Catalog Mode: End user enjoys the choice to add new Web Parts in any WebPartZone on the page.

Web Part Manager:-

Web Part Manager control is server control that completely manages the state of the zones . This control doesn't have any visual interface,. You can have only one WebPartManager for each page that works with Portal Framework.

Web Part Zone:-

You can declare each web zone in one of two ways. You can use the <asp:WebPartZone> element directly in the code, or you can create the zones within the table by dragging and dropping Web Part Zone controls onto the design surface. You can place anything in zones including HTML elements, web server controls, user controls and custom controls. Any thing placed into WebPartZone can be treated as Web Part. Useful attributes of WebPartZone include LayoutOrientation attribute which controls the display of items either horizontally or vertically.

Catalog Zone:-(To add new Web Part)----Catalog Mode

The ASP.NET 2.0 Portal Framework enables an end user to add Web Parts, but you must also provide the end user with a list of items he can add. It is designed to allow for categorization of the items that can be placed on the page. Catalog Zone is also a template control. The Catalog Zone control contains a title and checkbox list of items that can be selected. The Catalog Zone control also includes a drop down list of all available Web Part Zones on the page. From here, you can place the selected Web Parts into one of the Web Part Zones available from the list.

CatalogZone Contains CatalogPart controls like DeclarativeCatalogPart, PageCatalogPart, and ImportCatalogPart.

·       PageCatalogPart: Provides a page catalog of Web Part controls that a user has closed on a Web Parts page, and that the user can add back to the page.

·       DeclarativeCatalogPart: Enables developers to add a catalog of Web Part controls to a Web page so that users can choose whether to add them to a page.

·       ImportCatalogPart: Imports Web Parts controls, so that the control can be added to a web page with pre-assigned settings.

 

Changing mode of page:-

You can use either WebPartManager class directly or through the use of WebPartManager server control, you can change the mode of page. Changing the mode allows the user to make changes to pages they are working with. All the changes (mode changes) are recorded to ASPNETDB.MDF database associated with app_data directory created exclusively for Web Parts. Using WebPartManager object, you can add new Web Parts to the page. It also enables end user to drag and drop elements around the page.

Moving Web Parts---Design Mode

We can also move WebParts from one zone to another zone. This is possible through Design mode. To move any control just hover mouse over title of the control and you can see crosshair mouse symbol. Click the left mouse button and hold the Web Part and drag it to any WebPartZone. While dragging, the control it becomes transparent and drops the control in WebPartZone.

Editing the Web Parts---Editing Mode

Another Web Part mode that allows end-user to edit the Web Parts is Edit mode. This mode enables users to modify the settings related to behavior, appearance and layout for a particular Web Part on the page. When the user change the mode to edit you can see Appearance Editor/Layout Editor appear in the EditorZone. Appearance section allows users to change title and how the title of Web Part appears. Layout section enables user to change the order in which Web Parts appears in a zone or move Web Parts from one zone to another. Behavior section enables site editors to change dynamics of how end user can modify Web Parts.

Connection Between Webparts:-

 

Web parts are also capable of exchanging data between them, using web part connections. Using connections, you can have one web part provide one or more property values that can be used by other web parts on the page.

A WebPart Connection is a mechanism for sharing or transferring data from one Web Part (called the provider) to another Web Part (called the consumer). it is the ability to expose an interface to a WebPart (Provider) that another WebPart (Consumer) can connect to and use it

 

·       Connection types

Provider :-
- Control that provides data information
- Implements a provider connection point
- Defines a call back that returns an instance of the interface
- One provider connection point can connect to any number of consumer connection points of the same type

Consumer :-
- Control that gets data
- Implements a consumer connection point
- Defines a call back that gets an instance of the interface return by provider
- One consumer connection point can connect to only one provider connection points of the same type

 

·       Connection To establish a communication channel between provider and consumer WebParts so that they can exchange required information as defined in communication contract. A connection is establish between two connection points.The ConnectionPoint base class defines an object that is associated with a consumer or provider and contains the details necessary to exchange data. The ProviderConnectionPoint is associated with the provider, and the ConsumerConnectionPoint is associated with the consumer.

 

you must specify the following required attributes in addition to the id and runat attributes:

·       ConsumerID - Indicates the ID of the consumer control in the connection.

·       ConsumerConnectionPointID - Indicates the ID of a special callback method in the consumer used to establish the connection. This attribute is required only if the consumer has more than one connection point. For details on connection points, see ConnectionPoint.

·       ProviderID - Indicates the ID of the provider control in the connection.

·       ProviderConnectionPointID - Indicates the ID of a special callback method in the provider used to establish the connection. This attribute is required only if the provider has more than one connection point.


Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

What's SharePoint Not Good For

June 14, 2008 18:29 by Admin

visit: http://www.zonixsoft.com

SharePoint is an excellent way to create data-driven web sites, in my opinion, but there are other times where I don't think it's the best choice. For example, SharePoint isn't a substitute for a code management library, such as SourceSafe. (Microsoft is working on this for Visual Studio 2008, however, so watch out!) Also, SharePoint's integration with Microsoft Office, Windows, and .NET means that users of other operating platforms (Mac, Linux) or non-Microsoft browsers may have problems using the sites.

In particular, SharePoint authentication does not seem to work with Internet Explorer for the Macintosh or Mozilla prior to Version 1.7.2. In addition, SharePoint pages look different in FireFox, Mozilla, and Netscape Navigator than they look in Internet Explorer. You can compare these differences by using different browsers to view public SharePoint sites.


Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5