Ten Tips For Migrating From C# And .NET To Multi Platform Object Pascal And Delphi Firemonkey

Delphi XE8 Firemonkey Migrate From C# .NET To Object Pascal#1 Guide For Migrating From C# Syntax To The Object Pascal Syntax Used In Delphi XE8 Firemonkey

Marco Cantu has a guide called the Object Pascal Handbook which is an in depth guide to the state of the Object Pascal language as it applies to cross platform development on Android, IOS, Windows, and OSX. Both C# and Object Pascal share a language designer so there are also a lot of things that are similar. There are some syntax changes and gotchas to be aware of when migrating from C# to Object Pascal like using begin and end instead of { and } (as the most basic example). The guide can be really helpful in making you aware of the differences so you can hit the ground running. It covers things like language keywords, variables and data types, procedures and functions, strings, objects, inheritance, exceptions, properties, events, interfaces, classes, generics, anonymous methods, reflection, and RTTI. Object Pascal also has Thread Barriers, tuples, duck typing, IfExp ternary operator, and Task continuation implementations.

#2 Declare Your Variables Inline And The Delphi XE8 IDE Will Take Care Of The Rest

In C# and some other languages you can declare your local variables as you go along mid function. However, in Object Pascal you must declare your local variables at the top of the current function. The Delphi XE8 IDE has functionality where you can just write your variable inline like you would in C# and it will automatically move the variable code up into the correct location. Someone asked this question on StackOverflow and the answer was here.

#3 Mix .NET WinForms And Firemonkey Forms Within The Same Application

If you are switching to Firemonkey you may not want to leave all of your C# and .NET code behind. There is a commercial tool called Hydra that allows you to include Firemonkey forms within your WinForms applications. Additionally, you can include WinForms within your Delphi XE8 Firemonkey applications. Basically this means you can create a hybrid app that contains code from both. You may have an existing .NET application and you want to include some Firemonkey functionality into it. Hydra allows you to do this. On the flip side you could start a new Firemonkey application and include some of your existing .NET forms within it. This option is really great for moving to Delphi XE8 Firemonkey and retaining your existing codebase. As you start moving to multi platforms (Android, IOS, OSX, Windows) you can replace that C# functionality with cross platform functionality. You can also call a Delphi function in a DLL with C# as seen in this StackOverflow answer. JclDotNet is a free solution for communicating with the .NET framework. You can also checkout CrossTalk which lets you use .NET libraries in Delphi.

#4 Convert JSON To A Delphi Class Just Like You Would Convert JSON To A Class In C# And .NET

C# developers love writing classes and one really helpful tool that is used allows C# developers to convert JSON to a C# class. This reduces having to manually create all of the class data from a huge (or even small) JSON file. There is a similar utility for Delphi XE8 Firemonkey which will take a JSON class and convert it to an Object Pascal class. You can even configure some of the class options (like naming) using the utility prior to generating the class file. A great thing about this is that you could output your C# generated JSON from a Windows ASP.NET server to a Firemonkey app that has your Object Pascal generated JSON based class running on Windows, IOS, Android, and OSX.

#5 Leverage Your Existing C# And .NET Server REST Infrastructure Into A Multi Platform App

C# and ASP.NET are great tools for creating service side infrastructure and REST endpoints. However, if you want to leverage your existing REST endpoints from within cross platform apps on Android, IOS, OSX, and Windows then Delphi XE8 Firemonkey makes that easy. There is a TRESTClient component in Delphi XE8 which uses the native HTTP client on each platform for maximum TLS support. You have your IIS web server with your ASP.NET code delivering JSON via a REST endpoint. Next you have your Firemonkey app connecting to that end point with TRESTClient and delivering your API into Android, IOS, OSX, and Windows clients with one codebase. Delphi can also generate an ISAPI which you can install and use from within IIS as well if needed. This allows you to leverage new code built in Object Pascal from your existing ASP.NET infrastructure.

#6 Access Strings With TStringBuilder In Object Pascal Like You Would From C# And .NET Using System.Text.StringBuilder

If you are use to using the StringBuilder class in C# you will feel right at home in Delphi with the TStringBuilder class. The Delphi TStringBuilder class gives you access to all of the string operations you are use to using in C# from within Object Pascal. You have access to Append, AppendFormat, AppendLine, CopyTo, EnsureCapacity, Equals, Insert, Remove, Replace, ToString, Capacity, Chars, Length, and MaxCapacity. Another great object that I use for strings in Delphi Firemonkey is TStringList. TStringList makes it so easy to load a text file, manipulate it, save it back out, sort it, parse it, and lots more. I use TStringList in practically every Object Pascal app I write. TStringList can even have objects attached to each line in the TStringList which allows it to double up as a searchable TObjectList as well.

#7 Object Pascal Does Not Support Circular Referencing Classes Like Java, C#, And .NET

If you are migrating C# and .NET code to Object Pascal so that it can run on Android, IOS, OSX, and Windows you may have run into this issue. As an aside Java also supports circular references so this applies to migrating Java code to Object Pascal as well. The are a lot of different solutions on how you can architect your code so that circular referencing of classes is not needed.  Here are three different answers on StackOverflow on how to do this. I translated some C# code to Object Pascal recently and the easiest solution was to just combine the circular referencing classes into a single unit. So instead of two or three class files I ended up with one unit file that contained all three classes.

#8 In .NET The Runtime Library Is External To Your Application And In Firemonkey The Runtime Library Is Within Your App

Managing all of the different .NET framework versions can be a pain when deploying your C# and .NET applications to customer machines. You will not have this problem with Delphi XE8 Firemonkey apps on Android, IOS, OSX, and Windows because the run time library and Firemonkey framework are compiled within your app itself. A C# app may also appear small in size because the runtime is external to that application. In Firemonkey there is only one file to deploy and that is your app. Everything the app needs to run is contained within the app. Your app will make calls out to the API of each platform and will mainly use OpenGL to render the interface of your application (unless you prefer to use native controls). What this means is that a Firemonkey Hello World type app may appear large (multiple megabytes) but you can pack an entire enterprise application and tens of thousands of lines of code into a Firemonkey app and it with not increase that much in size.

#9 You Can Access .NET Run Time Libraries From Within Delphi Using A Third Party CrystalNet Runtime Environment

The CrystalNet Runtime Environment is a third party library available through SourceForge which provides you access to .NET from Delphi using COM. Obviously this solution is only for Windows but it allows you to access your legacy code with Object Pascal. Some of the features of the library allow you to: “Access .Net Libraries without Registration of Type Libraries in Delphi”, “Load .Net Assemblies in Delphi”, “Create instance of .Net Types in Delphi”, “Access base class libraries of .Net framework in Delphi”, “Accessing external .Net Library Type Objects(Fields, Properties, Methods, Events etc.)”, and “.Net Event Handling and Callbacks in Delphi”.

#10 Manage Your Mobile And Cloud Databases With Navicat As A Replacement For Microsoft SQL Server Management Studio

As a C#, .NET, and ASP.NET developer you have probably worked with Microsoft SQL Server and it’s fantastic SQL Server Management Studio. I love SQL Server Management Studio and I always miss it when working with other databases like MySQL. However, the solution to this is to utilize Navicat which is a third party database management studio. It supports MySQL, MariaDB, SQL Server, Oracle, PostgreSQL, and SQLite databases. One of the local database formats you can use in Delphi Firemonkey is SQLite and Navicat is the perfect database management studio for building your local databases for deployment to Android, IOS, OSX, and Windows. Navicat allows you to do things like copy and paste tables between databases which can really speed up your development process.

BONUS: Universal Data Access Components And LINQ Like Functionality

Delphi’s strength is working with databases and an amazing cross platform third party component set for directly connecting to databases is UniDAC from Devart. UniDAC runs on Android, IOS, OSX, and Windows. It allows you to directly connect to Oracle, Microsoft SQL Server, MySQL Server, Interbase, Firebird, PostgreSQL, and SQLite. UniDAC also supports server independent SQL which makes it even easier to use the same codebase to connect to any of the mentioned databases without changing your code. A second third party component set from Devart is called EntityDAC and it includes LINQ like support. C# and .NET developers are very keen on using LINQ functionality and EntityDAC allows you to use this same kind of functionality from within Delphi. EntityDAC is targeted for use in your middle tier. For more LINQ like functionality in Delphi check out TMS Aurelius as well. For a free option try this SQL Query Builder.


Find out more about the Firemonkey Framework on Android, IOS, OSX, and Windows over at the FiremonkeyX website.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Ten Tips For Migrating From C# And .NET To Multi Platform Object Pascal And Delphi Firemonkey by Delphi XE5 XE6 XE7 XE8 10 Seattle Berlin Firemonkey, Delphi Android, Delphi IOS is licensed under a Creative Commons Attribution 4.0 International License.

4 Responses to Ten Tips For Migrating From C# And .NET To Multi Platform Object Pascal And Delphi Firemonkey

  1. Pingback: Flotsam and Jetsam #103 | Delphi-losophy

  2. Pingback: Ten Tips For Building Professional Multi Platform Enterprise Apps With Delphi XE8 Firemonkey | Delphi XE5 XE6 XE7 XE8 Firemonkey, Delphi Android, Delphi IOS

  3. Joseph G. Mitzen says:

    Where in the real world is there *any* migration from C# to Delphi?!? I’d love to see a job ad for that path instead of the opposite.

    Also, as has been noted elsewhere, the “tuples” code doesn’t resemble tuples as understood in other languages; it is a nice shortcut record maker though. Similarly, the duck typing doesn’t implement true duck typing either. And while I’m at it 🙂 at the last Code Rage Mr. Mitov gave a talk explaining how to do “functional programming” in Delphi when it was really just about the “fluent style” (aka method chaining). In the Delphi world we tend to use a lot of terms differently than the development world at large uses them.

    Oh, just noticed another one. 🙂 EntityDAC and Aurelius are ORM (object-relational mappers) for databases. LINQ allows many data sources (e.g. lists) to be filtered, mapped and reduced in an SQL-like syntax. Neither are true LINQ replacements.

    For instance, if one had a C# list like this:

    var fruit = new List() { “apple”, “orange”,
    “banana”, “pear”, “raspberry”, “peach”, “plum” };

    One could write this to check if any fruit contained the letter “e’:

    fruit.Any(f => f.Contains(“e”));

    Or find the length of the largest fruit name:

    fruit.Max(f => f.Length);

    This would return a list of the squares of the values 0..9:

    Enumerable.Range(0, 10).Select(x => x*x).ToList();

    The real benefit of LINQ is being able to treat all data sources that support it in the same manner of databases. That’s not present in Delphi (nor is true duck typing, functional programming, tuples, etc.).

Leave a Reply

Show Buttons
Hide Buttons
Read previous post:
Delphi XE8 Firemonkey Google Analytics And App Ratings Android IOS
Google Analytics And App Ratings For Delphi XE8 Firemonkey On Android And IOS

Developer Yegor K. from JVESoft has released an updated version of his JVESoft Components Suite for Delphi XE8 Firemonkey. This...