Friday, December 17, 2010

Quotes About Loss Of Dad

an example of events

When you learn to program then you will not avoid the mainly occupy themselves so much and look much himself to read and understand examples.
So it's really me all day long. Since I am in front of my dual studies could deal with very little programming (if you think back to my service comes on the gun again) I have searched the last six months many tutorials and read some WebCast seen, ordered thick books and scoured and examples traced.

Here is a tribute to all developers out there that reflect their knowledge, often in very understandable way and with help and advice for beginners and converters for the page is available!

But I've also always excited about bad examples that have led instead to represent a topic unnecessarily complicated and have me held back considerably in my learning speed.
Especially with the concept of Events I've had a hard time and only after a great help of a colleague on the whiteboard I understood how things work and how I use events.
And as a former teacher of mine always said "only if you can explain something, you have really understood it," I will try here my first post about programming to play back a sample of events. For this I've devised a practical example:
A milk production system. It is a rather small company with a large tank that holds about 750 liters (a very small company ;-)). The farmer knows, however, never sogenau when the tank is slowly filled, and must therefore always on the good luck Production shut down while it's on him frequently that he does not have enough milk in the tank. Now he wants to leave the monitor of a computer, and affixed a float in the tank. This measures how many liters are in the tank. Therefore, the father would not always look at the stand, but more prefer to sit at his computer and surfing. So he asks a neighbor to him because programmers to write a small program that warns the farmers if the tank has reached a level of 500 liters.

The programmer solves this problem with an event!

He writes thus a class tank with property inhaltInLiter and a class alarm .

public class tank

{private int _inhaltInLiter;

public int inhaltInLiter
{
        set
        {
            _inhaltInLiter = value ;
   

       
}}}


public class alarm
{
}

The basic framework for an event is a delegate. In our case, a very simple delegate with no parameters and returns void .

public delegate void reached borderline () Is

Next, the event will offer itself as the class tank the event it is the one established in the class tank that follows like this:


public class tank

{private int _inhaltInLiter;

public int inhaltInLiter
{
set
        {
            _inhaltInLiter = value ;
   
        }
    }

public event reached borderline limit;
}

As you can see there is the Delegate borderline achieved in the signature of the event. This delegate is also called EventHandler.

What we need to have an event trigger. Well if the event triggers should happen, too something. In our example, the class alarm issue a warning if the contents of the tank exceeds a certain value. For this, we now define the class alarm a method we call OnGrenzwertErreicht .

public class alarm

{public void OnGrenzwertErreicht ()
{
. Console WriteLine ( "contents of the tank has reached the limit to shut down production!" );
}}

Now, as the program runs now? In our main method we now need an instance of the class and the tank alarm class. Then we subscribe to the delegate borderline achieved in the event limit and tell the delegates to show what method he should reach in our case OnGrenzwert from the class alarm. Then, we test all the 500 are still with the values 300 and According to the task of the farmer should now trigger the event only if the contents of the tank 500 liters or more.

public class Program {

static void Main () {

tank t = new tank ();
Alarm a = new alarm ();
t.Grenzwert + = new reached borderline ( a.OnGrenzwertErreicht);

t.inhaltInLiter = 300;.
Console readline () ;

t.inhaltInLiter = 500;
Console ReadLine ();.
}}

But who will be tried this code determine pretty quickly that yes nothing happens! It is lacking something! The actual release of the event. According to the task of the farmer is to fire the event whenever the contents are at the 500 liters. So we modify the set method in the class tank which then looks Sun

public class Tank
{
    private int _inhaltInLiter;

    public int inhaltInLiter
    {
        set
        {
            _inhaltInLiter = value ;
if (_inhaltInLiter> = 500)
{
limit ();
}}

;}}

now fired the event as desired each time though the contents of the tank or larger than 500.
This is a deliberately very simple example, that especially is to illustrate the application of an event!
Here you can find the MSDN documentation to delegate Events and . If you look right there in the links you can find one or the other link that can help you with delegates and events.

0 comments:

Post a Comment