| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
 |
 |
|
Events in C# .Net:
|
|
Delegate usefulness does not just lie in the fact that it can hold the references to
functions but in the fact that it can define and use function names at runtime and not
at compile time. A large goal of design delegates is their applicability in events model
of .Net. Events are the actions of the system on user manipulations (e.g. mouse clicks,
key press, timer etc.) or any event triggered by the program. To understand the usage of
delegates for event model, the previous examples are used here. We should add to our
Figure class next things:
|
public delegate void FigureHandler(string msg);
public static event FigureHandler Inverted;
public void InvertZ()
{
m_zPos = - m_zPos;
Inverted("inverted by z-axis");
}
|
|
|
Now we have a delegate declared and event that uses this delegate's type. In every
function we should call our event. The next code snippet should explain it clearly
|
static void Main(string[] args)
{
Figure figure = new Figure(10,20,30);
Figure.Inverted+=new Test.Figure.FigureHandler(OnFigureInverted);
figure.InvertX();
figure.InvertZ();
}
private static void OnFigureInverted(string msg)
{
Console.WriteLine("Figure was {0}",msg);
}
|
|
|
So, in the main function we should create an object of figure class and attach event
handler to the method OnFigureInverted. And when we call any of invert methods the event
is fired and it calls our event handler. The application will print the following string
into the console: Figure was inverted by x-axis Figure was inverted by z-axis .
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords Events in C# .Net, c# tutorials, c# properties, c# property, c# controls, c# library,
c# samples, treeview c#, c# thread, c# dataset, c# datagrid, listview c#, c# combobox,
c# listbox, c# table, c# xml, c# component, c# forms, c# database
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|