Morfik Basic Data Types and Variables

In this chapter we will take a closer look at the available data types in Morfik Basic, how to create new ones and how to use them in declaring variables and constants.


Data Types

In Morfik Basic, every time you declare a variable you must tell the compiler what is its type. The type of a variable defines what values it can assume and what operations can be performed on it, during program execution.

A type declaration specifies an identifier for the type. It is through this identifier that the type will be referenced throughout the your application.

There are several different major kinds of data types. In this chapter we will review each of these kinds in detail.

Simple Types

Simple types define ordered sets of values. Let us go through the simple types that are available to Morfik Basic and see how these can be an important factor in making your code clearer and more elegant.

Note: It is important to note that some of the functions designed to work with ordinal data types are not available to browser side code of your application.

Ordinal Types

Ordinal types represent a sub set of the Simple types. All simple types which are not floating point numeric types are ordinal types which have a one to one mapping to an element of a sub set of the set of Integer values.


Integer Types

Morfik Basic offers a wide choice of Integer types. You will probably find one of them suitable for each special need. The following table shows each of the integer types which are available on the server side of your application with the range of values it can store and the format in which the value is stored.


Type Value Range Format
Shortint -128 .. 127 Signed 8 bits
Smallint -32768 .. 32767 Signed 16 bits
Longint -2147483648 .. 2147483647 Signed 32 bits
Byte 0 .. 255 Unsigned 8 bits
Word 0 .. 65535 Unsigned 16 bits
Integer -2147483648 .. 2147483647 Signed 32 bits
Cardinal 0 .. 4294967295 Unsigned 32 bits
Int64 -9.2*1018 .. 9.2*1018 Signed 64 bits
LongWord 0 .. 4294967295 Unsigned 32 bits


Due to the limitations of the data types which are supported by the JavaScript engine inside the browsers a few of these types are not available in the browser side of your application. The following table lists the Integer types available to the browser side of your application.


Type Value Range Format
Smallint -32768 .. 32767 Signed 16 bits
Longint -2147483648 .. 2147483647 Signed 32 bits
Byte 0 .. 255 Unsigned 8 bits
Word 0 .. 65535 Unsigned 16 bits
Integer -2147483648 .. 2147483647 Signed 32 bits


Note that if you declare a variable of one of the unsupported types on the browser side of your application, Morfik will generate a compiler error for it.


Note: It is important to realize that the browser side of your application, written with Morfik Basic, will be compiled into JavaScript for runtime execution. Because of this, JavaScript supported types define what can or cannot be supported on the browser side.
For numeric data, JavaScript has only one data type which is the equivalent of a floating point Double. All the supported data types on the browser side must be contained within the range of values covered by a Double.

Character Type

The Char data type is used to store a single character. The character encoding which will be used is selected when you create your project.

The Char type is an ordinal type since there is a numeric value associated with each character that is represented in the type. You can obtain this value and the set of characters is ordered by it.

Enumerated Types

Enumerated types define ordered sets by enumerating the identifiers that represent its elements. The order of the elements in the set is the same in which they were declared. The presence of an identifier in the list of elements of an enumerated type is, basically, equivalent to the declaration of a constant of that type. In Listing 1 there is an example of an enumerated type which declares an element for day of week and a test sub routine showing how this type can be used.


Listing 1 – The '''DaysOfTheWeek 'enumerated type.

    Public Enum DaysOfTheWeek
        Sunday
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
    End Enum
 
    Public Sub test
      Dim ADay As DaysOfTheWeek
      ' ...
      ADay = Monday
      ' ...
      Select Case ADay
        Case Monday
          ShowMessage("Monday")
        Case Tuesday
          ShowMessage("Tuesday")
        Case Wednesday
          ShowMessage("Wednesday")
        Case Thursday
          ShowMessage("Thursday")
        Case Friday
          ShowMessage("Friday")
      End Select
      ' ...
    End Sub


All enumerated type elements have a corresponding numeric value, with the first element in the enumerated type having the value of 0 and all remaining elements having a value equals to its predecessor plus one.

Note: On the server side of your application you can use the Pred and Succ functions to obtain the previous and next values in an enumerated type. You can also apply the Ord function to obtain the numeric value of an enumerated type variable.
These functions are not available on the browser side of the application. Similar behavior to that of these functions can be obtained however. For example, in order to obtain the next day in the DaysOfTheWeek set, instead of the Succ function you could use code such as this:
day = CType(CType(day, Integer) + 1, DaysOfTheWeek)
More about the CType function can be found under the topic of Typecasting in this chapter.

Boolean Type

The Boolean type is a special, pre-defined, enumerated type which is defined in the System module of the Mofik Framework. The declaration is the equivalent of the following Morfik Basic enumerated type declaration:

Public Enum Boolean
  False
  True
End enum


Note: Since Boolean is an enumerated value it is also an ordinal type with False having the value of zero and True the value of one. On the server side of your application you can apply the Ord function to obtain the numeric value of a Boolean variable.

Subrange Types

Subrange types define ranges of values within other ordinal types. We can, for example, define a subrange type called WeekDays as being a range from the DaysOfTheWeek enumerated type that is shown in Listing 1, as an example of enumerated types. The following is an example of what such a declaration would look like:

public type WeekDays Monday to Friday

A variable whose type has been declared as being a Subrange type will have all the properties and characteristics of the base type of the Subrange, but will only accept values which fall within the specified range.

Floating Point Types

Floating point data types are sub ranges of the set of Real Numbers . Morfik Basic supports several different floating point types, each of them stored in a different manner/format. The following is a table of the supported data types and their capacities.


Type Range Significant Digits Size in Bytes
Single 1.5*10-45 .. 3.4*1038 7-8 4
Double 5.0*10-324 .. 1.7*10308 15-16 8
Extended 1.9*10-4951 .. 1.1*104932 19-20 10
Currency -922337203685477.5808 .. 922337203685477.5807 19-20 8


Please note that Morfik Basic uses different division operators, as mentioned in chapter 2, for integer ("\") and floating point types ("/").

Note: It is important to realize that the browser side of your application, written with Morfik Basic, will be compiled into JavaScript for runtime execution. Because of this, JavaScript supported types define what can or cannot be supported on the browser side.
For numeric data, JavaScript has only one data type which is the equivalent of a floating point Double. All the supported data types on the browser side must be contained within the range of values covered by a Double.

String Type

The string type is used to store a list of characters as if to store a word or a sentence. We can think of this type as an ordered list of characters since the composing characters are stored in the precise order in which they were stored. The following are examples of declaring variables of the string type.

Public Str As String
 
Public Str2 As String = "Hello!  This is a string!"

You can assign values to a string variable by assigning it a list of characters surrounded by a pair of the double quotes character. Since double quotes are used to delimit the characters which are being assigned to the string variable, having a double quote character within the character list becomes a problem since the compiler would interpret it as being the end of the character list. To work around this small problem, Morfik Basic uses the same method as most other Basic language compilers, to repeat the double quote char indicating that it is to be interpreted literally and not as a delimiter. The following is an example of how to embed a double quote in the value assigned to a string variable:

Public s1 As String = "this is a "" double quote"

String Constants

You can declare simple string constants or typed string constants. Typed Constants are functionally equivalent to initialized variables. The following are some samples of string constant declarations.

  Const Str1 = "Hello"
 
  Const Str2 As string = "Hello"


To declare a string constant containing non-printable characters you can use #X syntax where X is an ASCII code of the symbol. For example, here is how you can declare a multiline message:

  Const Msg = "Warning"#13#10"Something is wrong"

Structured Types

Structured types are types which store more than one atomic value. This essentially means that we are storing more than one value within a single variable. Morfik Basic allows you to create structured types which have other structured types within their definition.

Arrays

Arrays are a structured data type which holds a fixed number of elements of a same type, called the base type. Arrays are ordered lists and can be accessed by a numeric index which will refer to one specific entry in the array. Most implementations of the Basic language only allow Arrays to be indexed by a range of numbers that starts with Zero. Morfik Basic, however, allows any range of numbers to be specified as the indexer of the array. The following are some sample declarations for arrays types in Morfik Basic:

    Public type TZeroBasedIntegerArray Integer(10)
 
    Public type TOneBasedIntegerArray  Integer(1 to 10)

Notice that the first declaration only specifies one value, in this case this value is assumed to be the index of the last element in the array and the first one is assumed to be indexed as zero. So in this first declaration we are actually specifying an array with eleven elements numbered from zero to ten.

The following are some sample declarations of Array variables in Morfik Basic:

    Dim A1 As TZeroBasedIntegerArray
 
    Dim A2 As TOneBasedIntegerArray
 
    Dim A4 As Integer(2 To 6)

Notice that you can directly define a variable as being an array or define it as being of a pre-defined array type such as the ones presented in this topic.

Once the array variables have been declared you can access the individual array elements by indexing the variable with the appropriate value such as shown in the following example:

    A1(0) = 1
 
    A2(1) = 10

Structures

Structure is the name we give to structured types which allow us to store multiple elements of information, with different data types, within a single variable. The declaration of a structure includes an identifier for the type and identifiers and types for each of the elements that compose the structure. Frequently the component elements of a structure are referred to as fields, much as instance variables of a class are. The following is an example of a structure declaration:

  Public structure TPersonalData
    Name As String
    Phone As String
  End structure

Structures can also be declared with an added "packed" modifier which instructs the compiler to optimize memory usage, instead of prioritizing performance. The previous declaration would then be as follows, if we added the packed modifier.

  Public packed structure TPersonalData
    Name As String
    Phone As String
  End structure

You can access a field of a structure by using the same "." (dot) notation which is used for objects as show in the following example:

  Public Sub test
    Dim Client1 As TPersonalData
    '... 
    Client1.Name = "John"
    '...
  End Sub

Classes

Classes are structured types very similar to structures, in many ways. Classes however can "inherit" fields from other classes and can have sub routines and functions as elements.

  Public Class TPersonalData
    Name As String
    Phone As String
 
    Public overridable Sub ChangeName(ANewName As String)
      Name = ANewName
    End Sub
 
    Public overridable sub ChangePhone(ANewPhone As String)
      Phone = ANewPhone
    End Sub
  End Class

We will see classes in much greater detail in Chapter 5 – Object Oriented Programming with Morfik Basic.

Sets

This type, defined by the Set keyword is exactly what its name implies a set of values which consists of a sub set of the values that can be assumed by the base type.

Morfik Basic not only implements this type but a full range of operations which can be handled on this data type.

The following is a declaration of a Set data type which is based on integer type:

  Public Type TIntegerSet Set Of Integer"

Literal values can be assigned to Set variables in a very simple and straightforward manner, as shown in the following example:

Listing 2 – Set declarations and operations

Imports SystemUtilities
 
Namespace Sets
 
public type TByteSet Set Of Byte
 
Dim S1 As TByteSet
 
  Public Sub Go
    Dim S2 As Set Of Byte
    '...
    S1 = {1,2}
    S2 = S1 + {1,3}
    S2 = S1 - {1,3}
    S2 = S1 * {1,3}
    '...
    If S1 >= S2 then ShowMessage("superset")
    If S1 =  S2 then ShowMessage("equal"   )
    If S1 <> S2 then ShowMessage("unequal" )
    If 3  In S2 then ShowMessage("member" )
End Sub
End Namespace
Note: The Set type allows the usage of operators which treat Sets in a special way. In this example you can see Sets being added together or subtracted from one another.
These set operations will result in new sets which will hold all the elements of the two sets, in case of addition or a set with the elements of S1 which are not in S2, in case of subtraction, for example. This, plus the comparisons done in the same routine in should tell you that Morfik Basic really takes Sets seriously.

File Types

File types represents a linear sequence of values saved into a file. File types are available only on the server side of a Morfik application.

Typed Files

File types are declared with a base type which can be just about any fixed size type that can be declared in Morfik, leaving out other File types and dynamically allocated types such as classes and pointer types. This data type is frequently referred to as a "typed file" since it references a particular data type (the base type).

Listing 3 – Usage of the a typed file data type.

namespace FileTypes
 
Public type TIntegerFile File Of integer
 
Dim F1 As TIntegerFile
 
Public Sub Go
    Dim F2 As File Of Integer
    AssignFile(F1, "c:\test.int")
    Rewrite(F1)
    Write(F1, 9)
    CloseFile(F1)
End Sub
 
End Namespace

Binary Files

You can also work with binary files, which could be considered a special case of the Type Files where the base type is Byte. These files are normally used for lower level I/O operations and everything that goes in and out of them is seen as nothing more than a stream of bytes. The following would be a declaration of such a binary file.

  Dim MyFile As File of byte

Pointers

Pointers store the memory address of other variables. Most of the time pointer variables will hold the address of a dynamically allocated variable.

The following are examples of pointer type declarations, which reference different data types, including a structure.

  Public Structure TPersonalInfo
    name As String
    phone As String
  End Structure
 
  Public Type TPersonalInfoList TPersonalInfo(1 to 10)
 
  Public Type PInteger Ref Integer 
  Public Type PCliente Ref TPersonalInfo
  Public Type PClientes Ref TPersonalInfoList

Notice that pointer data types use the keyword "ref" to indicate that they are actually a reference to a variable which actually holds a value of the base type used in declaring the pointer.

Listing 4 – Example of pointer usage.

Namespace Pointers
'The next line declares a pointer to an Integer variable
Public Type PInteger Ref Integer
 
Dim Ptr1 As PInteger 
 
Public Sub Test
  Dim I1, I2 As Integer
  Dim Ptr2 As Ref Integer
 
  I1 = 1
  I2 = 2
  Ptr1 = Ref I1 'Assign to variable Ptr1 the address of I1
  Ptr2 = Ref I2 'Assign to variable Ptr2 the address of I2
  'The next line Assigns to I1 the sum of I1 and I2
  I1 = Deref Ptr1 + Deref Ptr2
End Sub
 
End Namespace
Note: Note that in the example in listing 4 variables Ptr1 is declared as being of type PInteger and Ptr2 is defined as Ref Integer. These types are essentially the same since PInteger is declared as being Ref Integer.

The Pointer Type

In Morfik Basic you can declare a variable as being of a type called Pointer. The pointer type is a generic type, not bound to any base type. It holds a memory address and makes no assumptions about what is stored in that memory position. The following is an example of a Pointer type variable.

Dim Ptr As Pointer

Nothing

When a Pointer type variable needs to be "empty" or null, Morfik Basic provides a predefined value called Nothing. Nothing is essentially a Null pointer, a pointer that does not point to any memory location.

Delegates

The Delegate type is essentially the same thing as pointer to a function or to a method. Delegates are very useful; in fact it is through delegates that the Morfik Framework handles events generated by the controls in Forms.

The way in which you declare a delegate is very similar to writing the header of a function or sub routine, as you can see in the following example:

  Public delegate Sub TNotifyEvent1 (Sender As TObject) Of Object
 
  Public delegate Sub FuncType (Str As String)

In Listing 5 you can see an example of how to declare delegates, how to assign values to them and how to use them in calling a function.

Listing 5 – Example of delegate usage.

Imports SystemUtilities
 
Namespace FunctionTypes
 
Public Class TestClass
    Public Sub TestSub(Sender As TObject)
    End Sub
End Class
 
Public delegate Sub TNotifyEvent1 (Sender As TObject) of object
Public delegate Sub FuncType (Str As String)
 
Public Sub MsgFunc(Str As String)
   ShowMessage(Str)
End Sub
 
Dim N1 As TNotifyEvent1
 
Public Sub Test
    Dim T As TestClass
    Dim N2 As Delegate Sub (Sender As TObject) of object
    Dim MsgProc As FuncType
 
    N1 = T.TestSub
    N2 = T.TestSub
    MsgProc = MsgFunc
    MsgProc("Hello")
End Sub
 
End Namespace

In two variables declared in the example in Listing 5, you can see that the first one as the keywords "of object" added at the end of the line. The same is true of the delegate local variable declared inside the Test function. Delegates declared with "of object" are references to methods while delegates declared without it are references to common sub routines or functions.

Variables

A variable is an identifier (a name, if you will) that we assign to a specific memory location whose content can be altered. Variables can be defined as being of a standard or user-defined data type.

Variable Declarations

In Morfik Basic variable declarations must come at the start of a sub routine, function or method. All variables must be declared before the first line of code which is part of the body of the function of method. In this respect, Morfik Basic deviates from most implementations of Basic, but it should present no problem for developers to adapt their code and coding style

Variables can be declared one at a time or several at a time. The following sample shows you both styles of declarations.

    Dim  Int1 As Integer
 
    Dim  Int2 As Integer 
 
    Dim Str1, Str2, Str3 As String

Variables can also be declared with their initial values as shown in the following example.

    Dim Int3 As Integer = 10
 
    Dim Str4 As String = "Hello"
Note: Initialized variable declarations will not work for local variables if you have selected to use Delphi as your backend platform compiler. In this case you can only have global initialized variables.

Variable declarations can have inline structured type declarations in them as can be seen in the following example.

  Dim APerson As Structure 
           Name As String
           Phone As String
      End Structure

Qualifiers

A reference to a variable may contain one ore more qualifiers which alter the meaning of the identifier. Qualifiers may assume several different forms and in your contact with Morfik Basic through out this guide you have already had contact with them. Some examples of using qualifiers are: prefixing a structure or class name to a member name, using the "." (dot) notation, indexing arrays and dereferencing pointer variables. You can see some usages of qualifiers in the following sample:

  Dim TestObj As TestClass
 
  N1 = TestObj.Name
  A2(1) = 10
  I1 = Deref Ptr1 + Deref Ptr2

In all the cases shown in the sample, the meaning of an identifier is changed by a prefix, and index or an operator (deref – dereferencing operator).

Typecasts

Typecasts are a way to tell the compiler that you want it to temporarily treat a variable as if it were of a different type. Typecasts can be accomplished using the CType function which takes as its two parameters the variable and type you want the compiler to consider as its type. The following is a small sample of using CType to convert an integer to a pointer and a pointer to an integer.

  Private Sub test
    Dim P As Pointer
    Dim I As Integer
    I = Ctype(P, Integer)
    P = Ctype(I, Pointer)
  End Sub
Note: No verification is done by the compiler regarding the properness of a typecast. It is presumed that the developer knows what he is doing when a typecast is requested.

Helper Methods

Morfik Basic allows all language types to receive the addition of Helper Methods. Helper Methods are, essentially, functions which are associated with the type and are available for calling through a variable or a reference to a type.

Creating a Helper Method

Adding a Helper Method to a type is actually quite simple and can be done from anywhere in the code where the type is within scope. The following is an example of adding a Method called ToXString to the Integer data type. This method will return the string representation of the integer value, with an upper case "X" in front of it.

Private Function Integer.ToXString As String
Begin
  Return "X" & Me.ToString
End function

Notice that this Method makes use of another Helper Method of the Integer data type, the ToString method.

An extensive list of Helper Methods has been introduced into the Morfik Framework in Morfik 2.

Wrapping it up

Morfik Basic has a complete and sophisticated system for handling data types. In most respects, programming while programming in Morfik Basic and experienced Basic Developer, should feel right at home in working with Morfik Basic.


Related topics

See Also

Back to top