Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Include Page
OPENMI:4.4 Step 4 - Implementing the MyEngineDotNetAccess
OPENMI:4.4 Step 4 - Implementing the MyEngineDotNetAccess

4.

...

The fourth step is to implement the MyEngineDotNetAccess class (Figure 10).

Image Removed
Fig 10. MyEngineDotNetAccess class
 
The MyEngineDotNetAccess has two purposes: to change the calling conventions to C# conventions and to change error messages into .NET exceptions.
The listed code below shows the Simple River example code for a MyEngineDotNetAccess class that implements the Initialize method, the performTimeStep method and the Finish method. In each of these methods the corresponding method in the MyEngineDLLAccess class is called and, if this method returns false, the error message from the engine is queried through the GetMessage method (following which an exception is created and thrown).

...

Code Block

using System; using System.Text; namespace MyOrganisation.OpenMI.MyModel {    public class MyEngineDotNetAccess    {       public void Initialize(string filePath)       {         if(\!(MyModelDLL.Initialize(filePath, ((uint) filePath.Length))))         {           CreateAndThrowException();         }       }       public void PerformTimeStep()       {         if(\!(MyModelDLL.PerformTimeStep()))         {            CreateAndThrowException();         }       }       public void Finish()       {          if(\!(MyModel.Finish()))          {              CreateAndThrowException();          }       }       private void CreateAndThrowException()       {          int numberOfMessages = 0;          numberOfMessages = MyModelDLL.GetNumberOfMessages();          string message = 'Error Message from MyModel ';          for (int i = 0; i < numberOfMessages; i++)          {             int n = i;             StringBuilder messageFromCore = new StringBuilder(' ');             MyModelDLL.GetMessage(ref n, messageFromCore, (uint) messageFromCore.Length);             message \+='; ';             message \+= messageFromCore.ToString().Trim();          }          throw new Exception(message);       } }

4.5 Step 5: Implementing the MyEngineWrapper class

...