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 ApplicationStarting; override; procedure PopulateConsole; override; 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.
When you press the Execute button, a EditBox is then displayed to handle input strings.
Pretty nice, isn't it?
If Code obfuscation (Project > Project options > Compiler) is enabled, SmartMS will generate minified-unclear
|