Skip to main content

C. What Are Events In Object Oriented Programming?

Posted in

In B. What is an Object's State and Behavior?, we discussed two characteristics the vast majority of object's hold:

1. a STATE and
2. a BEHAVIOR

There is another characteristic that one most learn to fully understand Object Oriented Programming. I separated this characteristic from an object's State and Behavior, because it's not nearly as common. Nonetheless, it is a very powerful characteristic that in many ways separates an Object Oriented programming language from a none OO language such as Easy Language prior to version 9.
________________________________________________________________________
NOTE: If you are familiar with Java, C++, or C#, or any other OO language, then you have probably used events and event listeners. The event design in OOEL is very similar.
________________________________________________________________________

So what exactly are Object Events?

The most precise definition is certain object's kick off Events when their State changes.

But as always, the easiest way to explain this is through an example. Lets use TradeStation's OOEL class "Button" (this can be found in the WinForms.ell library). Now, imagine you have a window that processes the selling and buying of stocks. On this window is a Button titled "Execute".

So what do you do with a button. Well you press it to create a particular Event. When you press a button, its "state" changes from "not pressed", to "pressed". Thus, this sparks an event. In this case it would process the buying or selling of a stock. Now, each event is given a name. In this case the event a OOEL button kicks off is called "Click".

So, now that we have explained what an Event is in Object Oriented Easy Language, what happens when the event kicks off. In the example above, we want the event to kick of the buying or selling of a stock. To do this we have to assign to the event an "Event Handler". In OOEL, an event handler is nothing but a method that gets kicked off and processes any logic you have placed in the method. Again, in our example it would be the buying or selling of a stock.

The following code examples are what Object Oriented Easy Language my look like when assigning an event handler to an Event. For example's sake, we will assume we have created the window for buying and selling stock. Further, we can assume we have created a button called Execute_Button, which is titled "Execute". The user enters the stock information in the window, and uses the Execute_Button to process
the buying or selling of the stock.

{First we create a method that assigns the events. In this case we assign the event
handler "processBuyOrSell". This method gets called when the user presses the
button "Execute_Button" and the event "Click" gets kicked off}

method void assignEvents()
begin

// This says when someone clicks on the button, call the method
// "processBuyOrSell"
Execute_Button.click += processBuyOrSell;

end;

{This is the event handler method for the button - Execute_Button. It is called by the runtime system when a user presses the button and the event "Click" is kicked off.
By "runtime system" I mean it is kicked off automatically. You do need to call this method directly in the program. Again, it is called automatically when someone presses the "Execute" button.}

{Notice the parameters "elsystem.Object sender" and "elsystem.EventArgs args".
These must be there or the program will not compile. The "sender" parameter
is a handle to the object that kicked of the event. In this case it would be our button -
Execute_Button. The parameter "args" is a handle to the event that was kicked off.
in this case it will be the event "Click". Having these handles can be extremely
helpful. In this case you probably would not use them. But you can assume they
come in handy given Tradestation OOEL forces you to create them, as do many
languages when creating an event handler method }

Method void processBuyOrSell(elsystem.Object sender, elsystem.EventArgs args)
begin

//Depending on what the user has selected, the code in this method
//will perform the actual buying or selling of stocks
process buy….
or
process sell…

end;

So again, think of an Event as something that kicks off when the state of an object changes. Once you assign the event handler, and the event is kicked off, you can think of it as an Object as saying "My state has changed, please handle my event so the appropriate action can take place and the program stays in a logical state."

Once the event handler method has been assigned, the method can be placed anywhere in the program. Its order in regards to other methods has no bearing. The logic in the program is not executed from top to bottom. Instead the appropriate logic (method) is kicked off whenever and wherever the event occurs. This separates Object Oriented Easy Language from Legacy Easy Language.

Although an Object's State and Behavior are the main essence of an Object, in many ways it's Events that separate Object Oriented languages from top-down languages. These languages are simply programs with lines of code that a processed in a top-down manor. As mentioned earlier, this describes Easy Language before version 9.0. With Events and methods, now you can have your code executed in a more logical manor, and the design of the program is much more flexible. Which is one of the very reason Object Oriented concepts evolved. After all, the mother of invention is necessity.


See some powerful OOEL Indicators and Strategies "OOEL Indicators and Strategies".