Getting Started with Sugar

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 portions of identical code for creating and using a dictionary in JavaSugarDemo and DotNetSugarDemo. In each case you need to add a reference to Sugar.

You can compare these examples with the Oxygene versions (for which initialisation of the dictionary is not as neat) in our PPS pages Getting Started with Sugar in .Net and Getting Started with Sugar in Oxygene for Java.

JavaSugarDemo

using java.util; using sugar; namespace JavaSugarDemo { static class Program { private SByte[] inputBytes = new SByte[30]; string country; Integer inputLength; sugar.collections.Dictionary<String, String> capitals; public static void Main(string[] args) { capitals = new sugar.collections.Dictionary<String, String>() { {"England", "London"}, {"Scotland", "Edinburgh"}, {"Northern Ireland", "Belfast"}, {"Wales", "Cardiff"}, {"Ireland", "Dublin"}, {"Germany", "Berlin"}, {"France", "Paris"}, {"Greece", "Athens"}, {"Finland", "Helsinki"}, {"Hungary", "Budapest"}, {"Italy", "Rome"}, {"Liechtenstein", "Vaduz"}, {"Lithuania", "Vilnius"}, {"Luxembourg", "Luxembourg"}, {"Poland", "Warsaw"}, {"Portugal", "Lisbon"}, {"Romania", "Bucharest"}, {"Russia", "Moscow"}, {"Serbia", "Belgrade"}, {"Slovakia", "Bratislava"}, {"Slovenia", "Ljubljana"}, {"Spain", "Madrid"}, {"Sweden", "Stockholm"}, {"Switzerland", "Bern"}, {"Norway", "Oslo"}, {"Austria", "Vienna"}, {"Belgium", "Brussels"}, {"Bosnia and Herzegovina", "Sarajevo"}, {"Bulgaria", "Sofia"}, {"Netherlands", "Amsterdam"}, {"Czech Republic", "Prague"}, {"Moldova", "Chisinau"}, {"Latvia", "Riga"}, {"Iceland", "Reykjavik"}, {"Malta", "Valletta"}, {"Macedonia", "Skopje"}, {"Montenegro", "Podgorica"}, {"Turkey", "Ankara"} }; capitals.Add("Estonia", "Tallinn"); System.@out.println("Please enter a European country to find its capital: "); inputLength = System.@in.read(inputBytes); country = new string(inputBytes, 0, inputLength - System.lineSeparator().length()); try { System.@out.println("Capital of " + country + ": " + capitals[country]); } catch(Exception E) { System.@out.println(country + " not found"); }; System.@in.read(); } } }
DotNetSugarDemo
using System; using System.Collections.Generic; using Sugar; namespace DotNetSugarDemo { static class Program { private Sugar.Collections.Dictionary<String, String> capitals; public static void Main(string[] args) { capitals = new Sugar.Collections.Dictionary<String, String>() { {"England", "London"}, {"Scotland", "Edinburgh"}, {"Northern Ireland", "Belfast"}, {"Wales", "Cardiff"}, {"Ireland", "Dublin"}, {"Germany", "Berlin"}, {"France", "Paris"}, {"Greece", "Athens"}, {"Finland", "Helsinki"}, {"Hungary", "Budapest"}, {"Italy", "Rome"}, {"Liechtenstein", "Vaduz"}, {"Lithuania", "Vilnius"}, {"Luxembourg", "Luxembourg"}, {"Poland", "Warsaw"}, {"Portugal", "Lisbon"}, {"Romania", "Bucharest"}, {"Russia", "Moscow"}, {"Serbia", "Belgrade"}, {"Slovakia", "Bratislava"}, {"Slovenia", "Ljubljana"}, {"Spain", "Madrid"}, {"Sweden", "Stockholm"}, {"Switzerland", "Bern"}, {"Norway", "Oslo"}, {"Austria", "Vienna"}, {"Belgium", "Brussels"}, {"Bosnia and Herzegovina", "Sarajevo"}, {"Bulgaria", "Sofia"}, {"Netherlands", "Amsterdam"}, {"Czech Republic", "Prague"}, {"Moldova", "Chisinau"}, {"Latvia", "Riga"}, {"Iceland", "Reykjavik"}, {"Malta", "Valletta"}, {"Macedonia", "Skopje"}, {"Montenegro", "Podgorica"}, {"Turkey", "Ankara"} }; capitals.Add("Estonia", "Tallinn"); Console.Write("Please enter a European country to find its capital: "); string country = Console.ReadLine(); try { Console.WriteLine("Capital of " + country + ": " + capitals[country]); } catch(Exception E) { Console.WriteLine(country + " not found"); } Console.Read(); } } }