Tuesday 25 October 2011

C# Event Handling


Introduction to delegates:
Delegates are like the function pointers used in C++ or C.It holds the method/methods reference in an object. They are generally referred to as type safe function pointers.

Advantages:
  • Delegates encapsulates the method's call from caller
  • Use of delegate improves the performance of application
  • Can be used to call a method asynchronously
  • Delegates provide maximum flexibility at run time being type safe

Syntax:

public delegate typeofdelegate DelegateName(object obj1,object obj2)

e.g

public delegate void Compute(object obj1,object obj2);

Events and delegates work hand-in-hand to provide a program's functionality. It starts with a class that declares an event. Any class, including the same class that the event is declared in, may register/subscribe one of its methods for the event. This occurs through a delegate, which specifies the signature of the method that is registered for the event. The delegate may be one of the pre-defined .NET delegates or one you declare yourself. Whichever is appropriate, you assign the delegate to the event, which effectively registers the method that will be called when the event fires. 

Subscribing/Registering to an Event:
  • The += syntax registers a delegate with an event
  • To unregister with an event, use the -= with the same syntax.

STEPS :
  • Declaring an event   To declare an event inside a class, first a delegate type for the event must be declared, if none is already declared.
    public delegate void Mydelegate(Object sender,EventArgs e);
    The delegate type defines the set of arguments that are passed to the method that handles the event. Multiple events can share the same delegate type, so this step is only necessary if no suitable delegate type has already been declared.
    Next, the event itself is declared.
    
    
    public event EventHandler Added;
    
    An event is declared like a field of delegate type, except that the keyword event precedes the event declaration, following the modifiers. Events usually are declared public, but any accessibility modifier is allowed.
  • Invoking an event   Once a class has declared an event, it can treat that event just like a field of the indicated delegate type. The field will either be null, if no client has hooked up a delegate to the event, or else it refers to a delegate that should be called when the event is invoked. Thus, invoking an event is generally done by first checking for null and then calling the event.
    if (Added != null)
    Added(this,EventArgs.Empty);
    Invoking an event can only be done from within the class that declared the event.
  • Hooking up to an event   From outside the class that declared it, an event looks like a field, but access to that field is very restricted. The only things that can be done are:
    • Compose a new delegate onto that field.
    • Remove a delegate from a (possibly composite) field.
    This is done with the += and -= operators. To begin receiving event invocations, client code first creates a delegate of the event type that refers to the method that should be invoked from the event. Then it composes that delegate onto any other delegates that the event might be connected to using +=.
    pb.Added += sb.Handler;


/**
 *
 * @author ravikiran
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventHandling
{
public delegate void Mydelegate(Object sender,EventArgs e);
class Publisher : EventArgs
{
public event EventHandler Added;

public void OnAdded()
{
if (Added != null)

Added(this,EventArgs.Empty);
}

}
class Subscriber
{
public void Handler(object sender, EventArgs e)
{
System.Console.WriteLine("Handler of subscriber being called");
}
}

class Program
{
static void Main(string[] args)
{
Publisher pb = new Publisher();
Subscriber sb = new Subscriber();


pb.Added += sb.Handler;
pb.OnAdded();
Console.ReadKey();

}
}
}

Monday 18 July 2011

Time Machine: Use of package in java program

Time Machine: Use of package in java program: "Package statement defines a name space in which classes are stored. package cuboid3; class box{ int length,breath,height; ..."

Use of package in java programming for beginners


Packages are used in Java  to server the following:
prevent naming conflicts,
to control access,
to make searching/locating and usage of classes, interfaces, enumerations and annotations easier etc.
A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management.
Some of the existing packages in Java are::
  • java.lang - bundles the fundamental classes
  • java.io - classes for input , output functions are bundled in this package
Programmers can define their own packages to bundle group of classes/interfaces etc. It is a good practice to group related classes implemented by you so that a programmers can easily determine that the classes, interfaces, enumerations, annotations are related.
Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classed.

Creating a package:

When creating a package, you should first choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.
The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.


Package statement defines a name space in which classes are stored.



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cuboid;

/**
 *
 * @author ravikiran
 */
class box{
int length,breath,height;

double volume()
{
return (length*breath*height);
}
}



class boxs{
public void main(String[] args) {


box mybox=new box();
box yourbox=new box();

mybox.length=2;
mybox.breath=4;
mybox.height=10;

double vol;
vol=mybox.volume();
System.out.println("the volume of my box ="+vol);


yourbox.length=3;
yourbox.breath=7;
yourbox.height=5;


vol=yourbox.volume();
System.out.println("the volume of your box ="+vol);
}
}

Time Machine: read oracle magazine online

Time Machine: read oracle magazine online: "May-June 2011 : http://www.oracle.com/technetwork/issue-archive/2011/11-may/index.html July-August 2011 : http://www.oracle.com/technetw..."

Sunday 10 July 2011

I'm in LOVE !

yes ....I aM in love with my Android phone ....bcoz it has the features that makes me go fida ...!
1. Twidroid for Twitter
2. Qik for live video streaming
3. mAnalytics to check google analytics
4. eBuddy to chat with contacts on various platforms without having to sign in individually (MSN, Yahoo, AIM, gtalk, Facebook)
5. GDocs Notepad to create documents on the go and it syncs with Google Docs
6. Barcode Scanner for comparison shopping
7. Advanced Task Killer to kill applications that are not in use
8. Thinking Space, a mind mapping app for Android platform
9. NBA Game Time Lite for live scoring from the NBA
10. CNN, NYTimes, BBC and TED for news