Monday, November 1, 2010

Oxygene - Basics by Example



Continue with the Basics by Example; today's version of the post written in Oxygene Enjoy!

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 


Greetings Program - Verbose
// Delphi Prism Basics
namespace DPGreetProgram;  

interface
uses
    System;   
  
type 
    Greet = public class      
        // Fields of Attributes
        private var fMessage: String;            
        private var fName: String;
        private var fLoopMessage: Integer;
        // Methods Definition
        private method Capitalize(value: String): String;
        private method SetMessage(value: String);
        private method SetName(value: String);    
        // Properties
        public property Message: String read fMessage write SetMessage;
        public property Name: String read fName write SetName;
        public property LoopMessage: Integer read fLoopMessage write fLoopMessage;        
        // Constructors Definition
        public constructor();
        public constructor(message: String; name: String; loopMessage: Integer);  
        // Methods Definition
        public method Salute();
        public method Salute(message: String; name: String; loopMessage: Integer);  
        public method Salute(name: String);
    end;  
  
type  
    GreetProgram = public class      
    public class method Main(args: array of String);  
    end;

implementation
// Property Setters/Getters Methods
method Greet.SetMessage(value: String);
begin
    self.fMessage := self.Capitalize(value);
end;
method Greet.SetName(value: String);
begin
    self.fName := self.Capitalize(value);
end;
// Constructor 
constructor Greet();
begin
    self.fMessage := "";
    self.fName := "";
    self.loopMessage := 0;    
end;  
// Overloaded Constructor 
constructor Greet(message: String; name: String; loopMessage: Integer);
begin
    self.fMessage := message;
    self.fName := name;
    self.loopMessage := loopMessage;
end;
// Method 1
method Greet.Capitalize(value: String): String;
begin
    // "if-then-else" statement 
    if value.Length >= 1 then 
    begin
        result := value[0].ToString().ToUpper() + value.SubString(1, value.Length - 1);
    end
    else 
    begin
        result := "";
    end;
end;
// Method 2
method Greet.Salute();
begin  
    // "for" statement 
    for i: Integer := 1 to self.loopMessage step 1 do
    begin
        Console.WriteLine("{0} {1}!", self.fMessage, self.fName);
    end;
end;  
// Overloaded Method 2.1 
method Greet.Salute(message: String; name: String; loopMessage: Integer);
var
    i: Integer;
begin
    // "while" statement  
    i := 0;
    while i < loopMessage do 
    begin
        Console.WriteLine("{0} {1}!", self.Capitalize(message), self.Capitalize(name));
        i := i + 1;
    end;
end;
// Overloaded Method 2.2  
method Greet.Salute(name: String);
var    
    dtNow: DateTime;
begin
    // "switch/case" statement  
    dtNow := DateTime.Now;
    case dtNow.hour of
        6..11: self.fMessage := "good morning,";
        12..17: self.fMessage := "good afternoon,";
        18..22: self.fMessage := "good evening,";
        23,0..5: self.fMessage := "good night,";
        else self.fMessage := "huh?"; 
    end;
    Console.WriteLine("{0} {1}!", self.Capitalize(self.fMessage), self.Capitalize(name));
end;

// Console Program
class method GreetProgram.Main(args: array of String);  
var
    // Define object of type Greet    
    g: Greet;
begin  
    // Instantiate Greet. Call Constructor 
    g := new Greet();
    // Call Set Properties  
    g.Message := "hello";  
    g.Name := "world";  
    g.LoopMessage := 5;  
    // Call Method 2  
    g.Salute();  
    // Call Overloaded Method 2.1 and Get Properties  
    g.Salute(g.Message, "delphi Prism", g.LoopMessage);  
    // Call Overloaded Method 2.2  
    g.Salute("carlos");
    
    // Stop and exit  
    Console.WriteLine("Press any key to exit...");  
    Console.Read();  
end;  
end.


Greetings Program - Minimal
// Delphi Prism Basics
namespace;

interface
uses
    System;   
  
type
    Greet = class  
    private
        // Fields of Attributes
        fMessage: String;
        fName: String;
        fLoopMessage: Integer;
        // Methods Definition
        method Capitalize(value: String): String;
        method SetMessage(value: String);
        method SetName(value: String);
    public
        // Properties
        property Message: String read fMessage write SetMessage;
        property Name: String read fName write SetName;
        property LoopMessage: Integer read fLoopMessage write fLoopMessage;        
        // Constructors Definition
        constructor;
        constructor(message: String; name: String; loopMessage: Integer);  
        // Methods Definition
        method Salute;
        method Salute(message: String; name: String; loopMessage: Integer);  
        method Salute(name: String);
    end;  
  
type 
    GreetProgram = class
    public  
        class method Main(args: array of String);  
    end;

implementation
// Property Setters/Getters Methods
method Greet.SetMessage(value: String);
begin
    fMessage := Capitalize(value);
end;
method Greet.SetName(value: String);
begin
    fName := Capitalize(value);
end;
// Constructor 
constructor Greet;
begin
    fMessage := "";
    fName := "";
    loopMessage := 0;    
end;  
// Overloaded Constructor 
constructor Greet(message: String; name: String; loopMessage: Integer);
begin
    fMessage := message;
    fName := name;
    loopMessage := loopMessage;
end;
// Method 1
method Greet.Capitalize(value: String): String;
begin
    // "if-then-else" statement 
    if value.Length >= 1 then 
    begin
        result := value[0].ToString().ToUpper() + value.SubString(1, value.Length - 1);
    end
    else 
    begin
        result := "";
    end;
end;
// Method 2
method Greet.Salute;
begin  
    // "for" statement 
    for i: Integer := 1 to loopMessage step 1 do
    begin
        Console.WriteLine("{0} {1}!", fMessage, fName);
    end;
end;  
// Overloaded Method 2.1 
method Greet.Salute(message: String; name: String; loopMessage: Integer);
var
    i: Integer;
begin
    // "while" statement  
    i := 0;
    while i < loopMessage do 
    begin
        Console.WriteLine("{0} {1}!", Capitalize(message), Capitalize(name));
        i := i + 1;
    end;
end;
// Overloaded Method 2.2  
method Greet.Salute(name: String);
var    
    dtNow: DateTime;
begin
    // "switch/case" statement  
    dtNow := DateTime.Now;
    case dtNow.hour of
        6..11: fMessage := "good morning,";
        12..17: fMessage := "good afternoon,";
        18..22: fMessage := "good evening,";
        23,0..5: fMessage := "good night,";
        else fMessage := "huh?"; 
    end;
    Console.WriteLine("{0} {1}!", Capitalize(fMessage), Capitalize(name));
end;

// Console Program
class method GreetProgram.Main(args: array of String);  
var
    // Define object of type Greet    
    g: Greet;
begin  
    // Instantiate Greet. Call Constructor 
    g := new Greet();
    // Call Set Properties  
    g.Message := "hello";  
    g.Name := "world";  
    g.LoopMessage := 5;  
    // Call Method 2  
    g.Salute;
    // Call Overloaded Method 2.1 and Get Properties  
    g.Salute(g.Message, "delphi Prism", g.LoopMessage);  
    // Call Overloaded Method 2.2      
    g.Salute("carlos");    
    // Stop and exit  
    Console.WriteLine("Press any key to exit...");  
    Console.Read();  
end;  
end.

And the Output is:





















Auto-Implemented Properties in Delphi Prism
Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. The following code shows how to use them just like with VB.NET, C#, C++/CLI and so on.

namespace;
interface
uses System;   

type Greet = class  
    // No explicit private field required
    // private fMessage: String;
    // Auto Implemented Property
    property Message: String;
    constructor();        
    method Salute();        
    method Salute(value: String);
end;  

type GreetProgram = class
    class method Main(args: array of String);  
end;

implementation
// Constructor 
constructor Greet();
begin
    Message := "";
end;  
method Greet.Salute();
begin  
    Console.WriteLine(Message[0].ToString().ToUpper() + Message.SubString(1, Message.Length - 1));
end;  
method Greet.Salute(value: String);
begin  
    Console.WriteLine(value[0].ToString().ToUpper() + value.SubString(1, value.Length - 1));
end; 

class method GreetProgram.Main(args: array of String);  
var g: Greet;
begin  
    g := new Greet();
    // Call Set Auto Implemented Property
    g.Message := "hello";  
    g.Salute;
    g.Message := "bye";
    // Call Get Auto Implemented Property
    g.Salute(g.Message);
end;  
end.


And the Output is:

No comments:

Post a Comment