/*C# Events
______________
Events notify the program when the state of the control changes.*/
/*Declaring an Event:
Events are decalared by uding a delegate object. A delegate object encapsulates
a method so that it can be called anonymously from the application. When an event
occurs, the client application invokes the corresponding delegate for the event
to which the delegate is attached.
Before you declare an event inside a class you need to declare a delegate by
using the delegate keyword. For every event in C#, there is a corresponding
method declaration that handles it.*/
public delegate void ModifiedEventhandler();
/*The delegate type consists of the set of arguments that it passes to the method
that handles the event. Multiple events can share the same delegate. In other
words by using a single delegate, you can invoke different methods corresponding
to different events.*/
public event ModidifedEventhandler Changed;
/*The event is declared using the event keyword. The event keyword follows the
access modifiers. Events are usually public, but you can specify any access modifier.
The event is declared as a field of the delegate type. The above example displays
the declaration of the event named changed as a field of the ModifiedEventHandler
delegate type.*/
if(Changed != null)
Changed();
/*After you declare an event in a class, the class treats the event as a field of
the declared delegate. Declaring events as a field of the delegate type allows
you to call the event directly like a method. The field Changed refers to a
delegate that is called when the event occurs. The above example shows that an
event is involved after checking whether or not the event is null. You invoke
an event only when it's not null.*/
You can perform the following operations on an event that is involved
1. Compose a new delegate onto that field
2. Remove a delegate from a composed field
List.Changed += new ChangedEventHandler(ListChanged);
List.Changed -= new ChangedEventHandler(ListChanged);
/*You composed a delegate by using the + operator.You remove a delegate by using
the - operator. To receive the event invocation, the client application, the
application that raises the event, creates a delegate of the event type. This
event refers to the method that is called when the event in invoked. Then, the
client application composes the new delegate with the existing delegate in the
event. When the client application no longer receives event invocations, it
removes the delegates from the event.*/
protected virtual void onChanged()
{
if(Changed != null)
Changed();
}
public override int Add(object value)
{
int i = base.Add(value);
onChanged();
return i;
}
/*You can invoke events only within the class in which the event is decalred.
You cannot directly call the events declared in the base class from the
derived class. To call a base class event from derived class, you create a
protected method in the application that invokes the event. For Example: the
state of a list box changes whenever you add an item to the list or delete an
item from the list.*/
Search
C# Events
Get the Computer, User and domain name using Javascript
This come quite easy, you just have to initialize an ActiveX object for "WScript.Network"
Note: This works only with ActiveX in IE.
/* Jscript - ComputerName, Username & DoaminName */
var oWshNet = new ActiveXObject("WScript.Network");
var computerNmae = oWshNet.ComputerName;
var userName = oWshNet.UserName;
var domainName = oWshNet.UserDomain;
Note: This works only with ActiveX in IE.
Interface in C#
An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface, as shown in the following example. An interface can be a member of a namespace or a class and can contain signatures of Methods.properties.Indexes & Events. An interface can inherit from one or more base interfaces. When a base type list contains a base class and interfaces, the base class must come first in the list. A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.
The following example demonstrates interface implementation. In this example, the interface IPoint contains the property declaration, which is responsible for setting and getting the values of the fields.
The class Point contains the property implementation. random
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
interface ISampleInterface
{
void SampleMethod();
}
namespace Tutorial
{
class Program : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
Console.WriteLine("Method implementation.");
}
static void Main(string[] args)
{
// Declare an interface instance.
ISampleInterface obj = new Program();
// Call the member.
obj.SampleMethod();
Console.ReadLine();
}
}
}
The following example demonstrates interface implementation. In this example, the interface IPoint contains the property declaration, which is responsible for setting and getting the values of the fields.
The class Point contains the property implementation. random
using System;
using System.Collections.Generic;
using System.Text;
interface IPoint
{
// Property signatures:
int x
{
get;
set;
}
int y
{
get;
set;
}
}// interface
class Point : IPoint
{
// Fields:
private int _x;
private int _y;
// Constructor:
public Point(int x, int y)
{
_x = x;
_y = y;
}
// Property implementation:
public int x
{
get
{
return _x;
}
set
{
_x = value;
}
}
public int y
{
get
{
return _y;
}
set
{
_y = value;
}
}
}// class
namespace IndiLogiX.Tutorial
{
class Program
{
static void PrintPoint(IPoint p)
{
Console.WriteLine("x={0}, y={1}", p.x, p.y);
}
static void Main(string[] args)
{
Point p = new Point(2, 3);
Console.Write("My Point: ");
PrintPoint(p);
Console.ReadLine();
}
}
}
Subscribe to:
Posts
(
Atom
)