Morfik CSharp Overview

This chapter will provide you will a general overview of Morfik C#. It is presumed that the user already has some familiarity with Microsoft’s C#. language.

Our focus will be on providing a quick reference for most language features and a more detailed explanation for those features which are unique to Morfik C#. It is important to note, from the start, that Morfik C# does not intend to be compatible with the formal specification of the C# language. Morfik C# is a syntax for the Morfik programming semantics which is as close to C# as permited by the underlying system. The goal of Morfik C# is to allow the C# developer working with Morfik to forgo learning the syntax of the intrinsic language constructs. Another major difference which is important to note is that Morfik C# is not case sensitive.

Through the evolution of its usage in the industry, C# has undergone a substantial amount of change and is still evolving. Morfik will continue to add features to Morfik C# striving to enhance the developer’s experience and to support new features required by the Morfik application platform.

Morfik C# Modules

Morfik C# introduces a new concept of Modules, which is generally present in the C# language with different characteristics, called a Namespace. In Morfik C# a Namespace is a single file which can contain several different classes. A Module in Morfik can have three forms, a browser side module, a server side module or a dual module. In all three cases the module is represented by a single .mmd file.

While, traditionally, the file was the only defining factor of the existence of module, in Morfik C# a formal code construct defines the module, for each side: browser and server. This construct, is called a Namespace. The simplest module that is possible to create with Morfik C# can be seen in Listing 1.

Listing 1 – Empty module in Morfik C#.

Namespace Globals

Module Duality – Browser and Server

In Morfik applications modules can be Browser side, Server side or dual. Dual modules are both Browser and Server at the same time. The Morfik IDE will show you a tabbed view of the module allowing you to edit the part of the module that you want: Browser or Server.

Referencing Modules

In order to access the classes, variables and types which are declared in other Morfik Modules it is necessary to import them. This is done in Morfik C# through the using clause as shown in Listing 2.

Listing 2 – Empty module in Morfik C# , importing SystemUtilities.

using SystemUtilities; // This line imports module SystemUtilities
Namespace Globals
  // This is an empty module

One using clause is used to declare each of the modules you wish to import at the start of a module.

It is important to have in mind that referencing a module in the Browser side of another module will not make the server side of this second module imports the same module’s server side counterpart. If this is required, it must be done manually.

Module Initialization and Finalization

Morfik modules have Initialization and Finalization sections. In Morfik C# these are implemented through two named code blocks. These blocks are called Initialization and Finalization and have a special syntax as shown in Listing 3.

Listing 3 – Module with Initialization and Finalization.

using SystemUtilities; // This line imports module SystemUtilities

Namespace Globals
    // This is a module with Initialization and 
       Finalization Sections
Initialization {
Finalization {

The code contained in the Initialization section is called when your XApp is first loaded, while the code in the Finalization is called when your application is terminated. The Finalization section is only effective on the server where you can be sure that your application will actually get finalized. While syntactically correct, a Finalization section on the browser side will never be called. On the server side of the application, however, the Finalization section will always be called on application shutdown.

It is also important to note that modules that are not statically bound on the browser side will have their Initialization sections called when they are first loaded and not at the initial page load. For more information on statically binding a module, refer to the Compiler Directives section of this chapter.

Note: We recommend that the user accept the default behavior implemented by Morfik which makes all Forms dynamically loadable and all pure code Modules statically loaded - typically these settings should not be altered manually.

Adding Comments

Morfik C# offers two different ways of creating comments:

// This a comment
/* This is another comment which can 
  span multiple lines */

There are basically two types of comments in Morfik C#: end of line and multiple line. End of line comments render all following text on the same line

into comments. Multi line comments render everything between the start and stop comment delimiters into a comment.

Note: It is best to create small comments within your code using the one line comments (to end of line) so that if you decide to comment out a section of a routine, you can do it with multi-line comments. A sample of this can be seen below:
/* I’m commenting this section out for debugging purposes
Label1.Caption = "A caption..."; //Changes the caption
In this case, the multi-line comment is not affected in any way by including a line with a single line comment.

MorfikDoc Comments

Morfik, taking its cue from work done for Microsoft’s Java platform, created an extension to normal comments that allows the developer’s comments to be integrated with the IDE’s code completion system. These special comments are a partial implementation of the JavaDoc syntax, but unlike JavaDoc, which uses its own separate compiler, MorfikDoc comments are processed by the Morfik high-level language compilers as part of Morfik background compilation process and are thus, immediately available within the IDE’s code completion system.

Listing 4 – MorfikDoc comment sample

  This is a morfikdoc comment for Test function
Void Test() {
// ...

The element that the compiler uses to identify a comment as a documentation comment is the second asterisk( * ) used in the opening of the comment. Any asterisks used in the start of any lines of MorfikDoc comments will be stripped out, so that the code in Listing 5 will have the same effect as that in Listing 4.

Listing 5 – MorfikDoc comment sample with extra asterisk

 * This is a morfikdoc comment for Test Sub
Public Void Test() {
  // ...


Morfik C# requires that you declare variables at the start of a function before the use of any action statement. This is a departure from Microsoft’s C# Specification which allows variable declaration statements to be freely intermixed with other statements within a code block.

Listing 6 – Variable declaration in Morfik C#

Public Void Test() {
  Integer X;
  Double Y;
  X = 2;
  X = X+1;
  Y = X/5;

Initialized Variables

Variables can be pre-initialized at declaration. This is done by a simple attribution in the variable declaration statement as in the following sample:

  Public Integer index = 10;

You can declare initialized variables also within sub routines as shown in Listing 7.

Note: Note that initialized variables cannot be used at the local scope level if you have chosen Delphi as your platform backend compiler. In this case you could use Typed Constants, as described later in this chapter, assuming you have Assignable typed constants turned on in your Delphi configuration.

Listing 7 – function with initialized variables.

Public Void Test() {
   Integer x = 20;
   Integer y = 30;

You can also have the equivalent to initialized variables through the use of a new language feature called Typed Constants.

Note: It is important to note that Morfik C# is case insensitive and that you cannot therefore declare two objects at the same scope level with the same name. Because of this, it is common practice to write code using "Proper" cased identifiers since in some cases they can make your code a lot easier to read. You can see this in the following examples:
integer thisfunctiongetsabithardtoread {}
integer ThisFunctionIsMuchEasierToRead {}

string thisisavariablewithalongname;
string ThisIsAVariableWithALongName;


Constants are normally used to hold literal values that are used in several different places within an application. Here is a small sample of a constant declaration:

    Public Const  myConst = 10;
    Public Const  Str = "Hello";

This sample shows the declaration of numeric and string literal constants. Constants declared in this form cannot have their values changed at runtime.

In Morfik C# you can declare a single constant in each const clause as shown in the previous example. Note that constant declarations, as variable declarations must precede the non declarative (action oriented) statements.

Typed Constants

Typed constants are a bit different from what you might expect, considering that they are constant only in name. Typed constants are declared in the following form:

      Public Const Integer MyInitVar = 20;
      Private Const Double MyDoubleVar = 2.15;

Notice that typed constants are declared with a formal type and function as initialized variables of that type.

Note: The Morfik C# compiler does not generate an error if you try to assign a value to a non-typed constant; however at compile time the backend platform compiler will generate an error.


Operators are symbols within your Morfik C# code which allow you to compare or perform logical or arithmetic operations on all data types. There are operators to add, subtract, multiply, divide, etc.

In this section we will see the Morfik C# operators and how they are used.

Assignment Operator

The assignment operator allows you to assign new values to variables or object properties. Morfik C# utilizes the ’=’ (equals) sign as the symbol for this operator. The following example means that the variable called Variable1 is assigned the value of 5.

 Variable1 = 5;

Equality Operator

Morfik C# uses as the equality operator the ’==’ (double equals) sign when comparing values as seen in the following example:

 If (X == Y) {}

Inequality Operator

Morfik C# uses the pair ’!=’ (exclamation and equals) of signs to represent the Inequality operator. You can see the usage of this operator in the following example:

If (A != B) {}

Logical Operators: AND, OR, NOT

These operators are used for logical combinations in comparisons. The following are examples of their usage:

If ((X = Y) && (A != B)) {}
If ((X = Y) | (A = B)) {}
If ((X != Y) && (!(A != B))) {}

The following is a list of the logical operators in Morfik C#.

Operator Definition
&& Logical AND.
|| Logical OR.
! Logical NOT.
^ Logical Exclusive OR.

Note: Through the usage of parenthesis you can combine several different logical operations in one more complex operation as seen in the last of the previous examples.

Other Comparison Operators

The following is a list of definitions of other comparison operators used in Morfik C#.

Operator Definition
< Less than. Yields a true value when the element to the left side holds a value which is "smaller" than the value held by the element to the right side.
> Greater than. Yields a true value when the element to the left side holds a value which is "larger" than the value held by the element to the right side.
<= Less than or equal. Yields a true value when the element to the left side holds a value which is "smaller" or equal to the value held by the element on the right side.
>= Greater than or equal. Yields a true value when the element to the left side holds a value which is "larger" or equal to the value held by the element to the right side.

Arithmetic Operators

Morfik C# has a full set of arithmetic operators which should be reasonably familiar to anyone with minimal programming experience. The following is a full list of the arithmetic operators.

Operator Definition
+ Addition operator, used on all numeric data types.
- Subtraction operator used on all numeric data types.
Multiplication operator used on all numeric data types.
/ Division operator used on floating point and Integer numeric data types.
% Remainder of division operator. This operator yields the value of the remainder of the integer division of the term to the left by the term on the right side.


String is a data type which is used to represent groups of characters. The following are examples of string variables and constants:

    Const Str = "Hello";
    string MyStr;
    string MyStr2 = "Hello!";

More details on strings can be found in the Data Types Chapter of this guide.

Application Flow Control

All programming languages share some constructs which are specific for application flow control. These structures are mainly divided into two groups: Branching and Loops.

All flow control structures (both for branching and loops) in Morfik C# can be used with a single statement or a statement block (code between braces {..}).


The Morfik C# language offers two constructs specific for branching control. These constructs have equivalents in most programming languages and are quite straightforward in usage.

If .. Else

The "IF" construct in most languages allows for choosing among two execution paths based on a Boolean expression. In Morfik C#, however, due to the possibility of nesting the "IF" statement you can actually branch into any number of different paths. The code snippet in Listing 8 shows an example of using the "IF" statement with several possible execution paths.

Listing 8 – Multiple paths with the If..Then..Else construct.

  If (((x + y) > 10) && (! ((x - y) > 0))) 
    z = x / y;
    If (X > Y) 
      Y = Z * X;
      If (Y == Z) 
        Z = 25;
        x = y / z;

Switch .. Case .. Default

The switch statement is used for conditional execution of code based on a list of values. The switch statement can have an optional default clause whose dependent code will be executed if the value of the condition did not match any of the Case values. The code snippet in listing 9 shows an example of the usage of this language construct.

Listing 9 – Code snippet showing the usage of Select Case.

  switch (z) 
   Case 1:
      z = z + 2;
   Case 2:
      y = x + 5;
      if (z > (x + y)) 
        x = y + 5;


Loop constructs handle repetitions in the execution of certain code blocks within an application.


The While statement in Morfik C# is very much straightforward in its utilization. While the condition specified is true, the contained code block will be executed. The following code snippet shows an example of its usage.

  While (Length(StringToConsume) > 0) {
    Delete(StringToConsume, 1, 1);

Note: In executing a While statement, the condition is always verified before the first execution of the code block contained by the statement. This means that it is possible that the contained code will never execute if the condition starts out as false.

Do .. While

The Do..While loop structure has its condition set at the bottom of the code section it contains. Having the condition at the bottom of the contained section means that the contained code will be executed at least once, since the condition will only be checked afterwards.

The main difference between using the While or Do While constructs is that when you use While, the contained code might not be executed if the condition is false and when you use Do..While the code will be executed at least once. The code snippet in Listing 10 shows an example of the possible usage of the Do..While construct.

Listing 10 – Do While usage Examples.

   i = XApp.OpenForms.Count1;
   Do {
     frm = XApp.OpenForms[i];
     i = i – 1;
   } While(i < 0);


When we have previous knowledge about the exact number of times a certain code block should be executed, we can use the For statement in one of its variations (ascending or descending). The execution of the contained code of a For statement is done in the following order:

1. The value of the control variable is checked against the end value.

2. The contained code is executed

3. The control variable is incremented or decremented (depending on whether it is an ascending or descending loop).

4. Return to step 1.

In order to have a descending For loop you should change the increment operator (++) for the decrement operator (--) in the construction of the loop construction, inverting the maximum and minimum value order. The code snippet in Listing 11 shows examples of both ascending and descending For loops.

Listing 11 – Examples of the usage of For loops.

  If (Descending) 
    For (i = Max; i >= Min; i--) //descending Loop
      { ShowMessage(IntToStr(i)) }
    For (i = Min; I <= Max; i++) //ascending Loop
      { ShowMessage(IntToStr(i)) }

Note: It is important to note that only the forms of the For loop shown in the example immediately above are accepted by Morfik C#.
For loops without all parameters specified are not supported in Morfik C# which differs in this respect from Microsoft’s C# specification.

Conditional Compilation

Morfik C# supports conditional compilation of code sections based on constants defined (through the #Define directive) at compile time. This is useful to include or exclude certain code sections for debug purposes, for example.

The following is a code snippet which exemplifies the usage of the #If..#Else #EndIf conditional compilation directives.

    #Define Aardvark
    #If Aardvark
       ShowMessage("you will see this!");
       ShowMessage("you won't see this!");

Wrapping it up

Morfik C# is a syntax mapping of the C# language over the Morfik semantic caracteristics. This language syntax, as all other Morfik syntaxes, is still in evolution. In the next chapters you will see more details on certain aspects of the Morfik C# language such as its data types, sub routine and class construction.

Related Topics

See Also

Back to top