Getting Started with Sugar in Oxygene for .Net

See a clear introduction to Sugar on the RemObjects website. In short, the Sugar library enables you to share some of your code between the different platforms that you are targeting. You can see below code for creating and using a dictionary in the console application OxygeneDotNetSugarDemo. ( Add a reference to Sugar before compiling it). Compare this with the very similar code in OxygeneJavaSugarDemo in the Oxygene for Java tutorial. The code is based our demonstrations of using Sugar in RemObjects C# for .Net and for Java.

namespace OxygeneDotNetSugarDemo;

interface

uses System.Linq, Sugar.Collections;

type
  ConsoleApp = class
  private
    class var capitals : Sugar.Collections.Dictionary<String, String>;
    class var country: String;
  public
    class method Main(args: array of String);
    class method MakeDictionary;
  end;

implementation

class method ConsoleApp.MakeDictionary;
begin
  capitals := new Sugar.Collections.Dictionary<String, String>;
  capitals.Add("England", "London");
  capitals.Add("Scotland", "Edinburgh");
  capitals.Add("Northern Ireland", "Belfast");
  capitals.Add("Wales", "Cardiff");
  capitals.Add("Ireland", "Dublin");
  capitals.Add("Germany", "Berlin");
  capitals.Add("France", "Paris");
  capitals.Add("Greece", "Athens");
  capitals.Add("Finland", "Helsinki");
  capitals.Add("Hungary", "Budapest");
  capitals.Add("Italy", "Rome");
  capitals.Add("Liechtenstein", "Vaduz");
  capitals.Add("Lithuania", "Vilnius");
  capitals.Add("Luxembourg", "Luxembourg");
  capitals.Add("Poland", "Warsaw");
  capitals.Add("Portugal", "Lisbon");
  capitals.Add("Romania", "Bucharest");
  capitals.Add("Russia", "Moscow");
  capitals.Add("Serbia", "Belgrade");
  capitals.Add("Slovakia", "Bratislava");
  capitals.Add("Slovenia", "Ljubljana");
  capitals.Add("Spain", "Madrid");
  capitals.Add("Sweden", "Stockholm");
  capitals.Add("Switzerland", "Bern");
  capitals.Add("Norway", "Oslo");
  capitals.Add("Austria", "Vienna");
  capitals.Add("Belgium", "Brussels");
  capitals.Add("Bosnia and Herzegovina", "Sarajevo");
  capitals.Add("Bulgaria", "Sofia");
  capitals.Add("Netherlands", "Amsterdam");
  capitals.Add("Czech Republic", "Prague");
  capitals.Add("Moldova", "Chisinau");
  capitals.Add("Latvia", "Riga");
  capitals.Add("Iceland", "Reykjavik");
  capitals.Add("Malta", "Valletta");
  capitals.Add("Macedonia", "Skopje");
  capitals.Add("Montenegro", "Podgorica");
  capitals.Add("Turkey", "Ankara");
  capitals.Add("Estonia", "Tallinn");
end;

class method ConsoleApp.Main(args: array of String);
begin
  MakeDictionary;
  Console.WriteLine('Type end to finish.');
  repeat
    Console.Write('Please enter a European country to find its capital: ');
    country := Console.ReadLine;   
    if country = 'end' then
      exit;
    try
      Console.WriteLine('Capital of ' + country + ': ' + capitals[country]);
      Console.WriteLine();
    except
      Console.WriteLine(country + " not found");
    end;
  until country = 'end';  
end;

end.


Programming - a skill for life!

How to write programs in the Oxygene for .Net dialect of Pascal