Writing your 1st Smart program

Top  Previous  Next

Different types of application

 

Smart Mobile Studio allows you to create GUI (Graphical User Interface) or Console (text-only) applications (programs) along with many other types. See project types. We will concern ourselves here with the common, basic CONSOLE application.

       

Smart Mobile Studio does a lot of work for us - a code called Unit, one chunk of code. If you save this code, it will save in a file called [project_name].sproj - a Smart Mobile project. This is best illustrated with a very simple console program.

 

Looking at the code that Smart generated

 

Whilst we haven't typed one line of code, SmartMS has typed many for us. Let us first look at the main program code. Notice that we have added comments (in blue, starting with the // comment identifier). These are ignored by the  SmartMS compiler, but help the coder understand the code.

       

       

unit Unit1;

 

interface

 

uses 

  System.Types, System.Lists, SmartCL.System, SmartCL.Scroll, SmartCL.Console,

  SmartCL.Components, SmartCL.Application, SmartCL.ConsoleApp;

 

type

  TApplication = class(TW3CustomConsoleApplication)

  private

    { Private fields and methods }

  protected

    procedure ApplicationStartingoverride;

    procedure PopulateConsoleoverride;

    procedure ProcessCommand(aCommand: string); override;

  end;

 

implementation

 

{ TApplication}

 

procedure TApplication.ApplicationStarting;

begin

  // Initialize objects above inherited;

  inherited;

  // UI elements below inherited

end;

 

procedure TApplication.PopulateConsole;

begin

  Console.WriteLn('Hello world');

end;

 

procedure TApplication.ProcessCommand(aCommand: string);

begin

  // Handle input string here

  inherited ProcessCommand(aCommand);

end;

 

end.

 

Creating a simple 'Hello World' program

 

When you first create a SmartMS console app, Smart will generate 3 special methods:

 

ApplicationStarting: basically, SmartMS setup the application header to the console screen. Actual console object is added directly to the screen.

 

PopulateConsole: Display strings when console screen is started.

 

  Console.WriteLn('Press the Execute button (or type Tab then Enter) to answer questions.');

  Console.WriteLn('');

  Console.WriteLn('What is your name?');

 

ProcessCommand(aCommand: string): Smart console applications are a little bit different from the conventional GUI. In order to input data, the user first presses the Execute button.  SmartMS will use WriteLn method.

 

Our first example shows how you can use an incrementing variable to match each input with the appropriate code to process it. The other examples show how you can nest routines inside the ProcessCommand and/or PopulateConsole procedures, which is useful when adapting code from existing console programs. Please, declare and define a variable i : integer := 0; in Private session and type this code:

 

 

procedure TApplication.ProcessCommand(aCommand: string);

var

  age : integer;

begin

  inc(i);

  case i of

  1 : begin

        Console.writeln('Hello, ' + aCommand + '!');

        Console.writeln('How old are you?');

      end;

  2 : begin

        age := StrToInt(aCommand);

        Console.writeln ('Next year you will be ' + IntToStr(age + 1) + '.');

      end;

  end;

  // Handle input string here

  inherited ProcessCommand(aCommand);

end;          

       

 

Running our first program

 

To run the program, press F9, the program runs it look like this.

 

img0

When you press the Execute button, a EditBox is then displayed to handle input strings.

 

img2

Pretty nice, isn't it?

 

 

mytoggle_plus1SmartMS generated this javascript code:

var TApplication = {

   $ClassName : "TApplication",

   $Parent : TW3CustomConsoleApplication,

   $Init : Function ($) {

      TW3CustomConsoleApplication.$Init($);

      $.i = 0

   },

   ApplicationStarting : Function (S) {

      TW3CustomConsoleApplication.ApplicationStarting(S)

   },

   PopulateConsole : Function (S) {

      TW3Console.WriteLn$1($Check(S, "").FConsole, "Press the Execute button (or type Tab then Enter) to answer questions.");

      TW3Console.WriteLn$1($Check(S, "").FConsole, "");

      TW3Console.WriteLn$1($Check(S, "").FConsole, "What is your name?")

   },

   ProcessCommand : Function (S, aCommand$1) {

      var age = 0;

      ++$Check(S, "").i;

      switch ($Check(S, "").i) {

      Case 1:

         TW3Console.WriteLn$1($Check(S, "").FConsole, "Hello, " + aCommand$1 + "!");

         TW3Console.WriteLn$1($Check(S, "").FConsole, "How old are you?");

         break;

      Case 2:

         age = parseInt(aCommand$1, 10);

         TW3Console.WriteLn$1($Check(S, "").FConsole, "Next year you will be " + (age + 1).toString() + ".");

         break

      }

      TW3CustomConsoleApplication.ProcessCommand(S, aCommand$1)

   },

   Destroy : TW3CustomApplication.Destroy,

   ApplicationClosing : TW3CustomConsoleApplication.ApplicationClosing,

   ApplicationStarted : TW3CustomConsoleApplication.ApplicationStarted,

   ApplicationStarting$ : Function ($) {

      Return $.ClassType.ApplicationStarting($)

   },

   PopulateConsole$ : Function ($) {

      Return $.ClassType.PopulateConsole($)

   },

   ProcessCommand$ : Function ($) {

      Return $.ClassType.ProcessCommand.apply($.ClassType, arguments)

   }

};

 

 

If Code obfuscation (Project > Project options > Compiler) is enabled, SmartMS will generate minified-unclear

mytoggle_plus1JS code:

var A3 = {

   $ClassName : "TApplication",

   $Parent : o,

   $Init : function ($) {

      o.$Init($);

      $.ZC = 0

   },

   Rn : function (S) {

      o.Rn(S)

   },

   VG : function (S) {

      bG.SE($Check(S, "").Es, "Press the Execute button (or type Tab then Enter) to answer questions.");

      bG.SE($Check(S, "").Es, "");

      bG.SE($Check(S, "").Es, "What is your name?")

   },

   ky : function (S, uW) {

      var b7 = 0;

      ++$Check(S, "").ZC;

      switch ($Check(S, "").ZC) {

      case 1:

         bG.SE($Check(S, "").Es, "Hello, " + uW + "!");

         bG.SE($Check(S, "").Es, "How old are you?");

         break;

      case 2:

         b7 = parseInt(uW, 10);

         bG.SE($Check(S, "").Es, "Next year you will be " + (b7 + 1).toString() + ".");

         break

      }

      o.ky(S, uW)

   },

   Destroy : H.Destroy,

   Om : o.Om,

   Vb : o.Vb,

   Rn$ : function ($) {

      return $.ClassType.Rn($)

   },

   VG$ : function ($) {

      return $.ClassType.VG($)

   },

   ky$ : function ($) {

      return $.ClassType.ky.apply($.ClassType, arguments)

   }

};