May 13, 2010

Using data.ExecutionContext in custom elements

When using the ExecutionContext from the IElementData object that is passed to the constructor of your custom element there is an important thing to consider. I ran into this problem just yesterday and fortunately I found the solution.

The problem is that you cannot use the data.ExecutionContext inside the constructor if you want to access for example state properties with the syntax

thisState = ((IStateProperty)row.GetProperty("MyState")).GetState(_data.ExecutionContext);

You will get a null exception when running the simulation when Simio tries to retrieve the referenced state. The problem here seems to be that at the time the element is instantiated (constructor), the object that owns the state might not, or at least not yet, be initialized.

The answer is to put any logic that refers to states of other objects into the element's initialize() method. At this time, all other objects seem to be there too.

Have a good father's day!