Delphi Projects News

The First Computer Program – In Delphi

Lady Ada Lovelace using a modern day laptop
Charles Babbage

Lady Ada Lovelace is generally considered the world’s first computer programmer. Born Augusta Ada Byron, in 1815, she was the daughter of Lord Byron, the English romantic poet. She took an interest in mathematics and her talents lead her to work with Charles Babbage, “the father of computers,” whom she met in 1833.

Babbage started work on his Difference Engine, as a means to automatically calculate and print the values of polynomial functions. While he never saw his Difference Engine completed, two were finished in modern times based on his original designs. One of which is in the Computer History Museum in Silicon Valley, where I saw it in operation.

In 1837 Babbage first described his Analytical Engine, as a general-purpose computer. In 1843, Lovelace translated a French paper that Italian mathematician Luigi Menabrea wrote about Charles Babbage’s Analytical Engine. While translating it she added thousands of words of her own notes to the paper. As an example she wrote of one such sequence—how to calculate Bernoulli numbers. This is regarded by computer historians as the first computer program.

Lovelace's diagram from "note G", the first published computer algorithm

Two Bit History has a great write up on Lovelace’s program: What Did Ada Lovelace’s Program Actually Do? which includes a C translation and a solution for the bug it included.

Naturally I wanted to see what her code would look like in Delphi’s Object Pascal.

program LovelaceBernoulli;



function B7: extended;
 * Calculates what Lady Ada Lovelace labeled "B7", 
 * which today we would call the 8th Bernoulli number.
 * Based on
 * More info
 * Bernoulli Numbers:
    // ------------------------------------------------------------------------
    // Data
    // ------------------------------------------------------------------------
    var v1: Single := 1; // 1
    var v2: Single := 2; // 2
    var v3: Single := 4; // n

    // ------------------------------------------------------------------------
    // Working Variables
    // ------------------------------------------------------------------------
    var v4: Single := 0;
    var v5: Single := 0;
    var v6: Single := 0;                        // Factors in the numerator
    var v7: Single := 0;                        // Factors in the denominator
    var v8: Single := 0;
    var v10: Single := 0;                       // Terms remaining count, basically
    var v11: Single := 0;                       // Accumulates v6 / v7
    var v12: Single := 0;                       // Stores most recent calculated term
    var v13: Single := 0;                       // Accumulates the whole result

    // ------------------------------------------------------------------------
    // Result Variables
    // ------------------------------------------------------------------------
    var v21: Single := 1.0 / 6.0;             // B1
    var v22: Single := -1.0 / 30.0;           // B3
    var v23: Single := 1.0 / 42.0;            // B5
    var v24: Single := 0;                     // B7, not yet calculated

    // ------------------------------------------------------------------------
    // Calculation
    // ------------------------------------------------------------------------
    // ------- A0 -------
    (* 01 *) v6 := v2 * v3;                // 2n
    (* 02 *) v4 := v6 - v1;                // 2n - 1
    (* 03 *) v5 := v6 + v1;                // 2n + 1

    // In Lovelace's diagram, the below appears as v5 / v4, which is incorrect.
    (* 04 *) v11 := v4 / v5;               // (2n - 1) / (2n + 1)

    (* 05 *) v11 := v11 / v2;              // (1 / 2) * ((2n - 1) / (2n + 1))
    (* 06 *) v13 := v13 - v11;             // -(1 / 2) * ((2n - 1) / (2n + 1))
    (* 07 *) v10 := v3 - v1;               // (n - 1), set counter?

    // A0 = -(1 / 2) * ((2n - 1) / (2n + 1))

    // ------- B1A1 -------
    (* 08 *) v7 := v2 + v7;                // 2 + 0, basically a MOV instruction
    (* 09 *) v11 := v6 / v7;               // 2n / 2
    (* 10 *) v12 := v21 * v11;             // B1 * (2n / 2)

    // A1 = (2n / 2)
    // B1A1 = B1 * (2n / 2)

    // ------- A0 + B1A1 -------
    (* 11 *) v13 := v12 + v13;            // A0 + B1A1
    (* 12 *) v10 := v10 - v1;             // (n - 2)

    // On the first loop this calculates B3A3 and adds it on to v13.
    // On the second loop this calculates B5A5 and adds it on.
    while (v10 > 0) do
        // ------- B3A3, B5A5 -------
        while (v6 > 2 * v3 - (2 * (v3 - v10) - 2)) do
        begin                                // First Loop:
            (* 13 *) v6 := v6 - v1;           // 2n - 1
            (* 14 *) v7 := v1 + v7;           // 2 + 1
            (* 15 *) v8 := v6 / v7;           // (2n - 1) / 3
            (* 16 *) v11 := v8 * v11;         // (2n / 2) * ((2n - 1) / 3)

                                             // Second Loop:
            // 17    v6 := v6 - v1;              2n - 2
            // 18    v7 := v1 + v7;              3 + 1
            // 19    v8 := v6 / v7;              (2n - 2) / 4
            // 20    v11 := v8 * v11;            (2n / 2) * ((2n - 1) / 3) * ((2n - 2) / 4)

        // A better way to do this might be to use an array for all of the
        // "Working Variables" and then index into it based on some calculated
        // index. Lovelace might have intended v14-v20 to be used on the
        // second iteration of this loop.
        // Lovelace's program only has the version of the below line using v22
        // in the multiplication.
        if (v10 = 2) then
        (* 21 *) v12 := v22 * v11             // B3 * A3
        (* 21 *) v12 := v23 * v11;            // B5 * A5

        // B3A3 = B3 * (2n / 2) * ((2n - 1) / 3) * ((2n - 2) / 4)

        // ------- A0 + B1A1 + B3A3, A0 + B1A1 + B3A3 + B5A5 -------
        (* 22 *) v13 := v12 + v13;            // A0 + B1A1 + B3A3 (+ B5A5)
        (* 23 *) v10 := v10 - v1;             // (n - 3), (n - 4)

    (* 24 *) v24 := v13 + v24; // Store the final result in v24
    (* 25 *) v3 := v1 + v3;    // Move on to the next Bernoulli number!

    // This outputs a positive number, but really the answer should be
    // negative. There is some hand-waving in Lovelace's notes about the
    // Analytical Engine sorting out the proper sign.
    Result := v24;

    Writeln('Calculates what Ada Lovelace labeled "B7", which today we would call the 8th Bernoulli number.');
    Writeln('This outputs a positive number, but really the answer should be negative.');
    Writeln('There is some hand-waving in Lovelace''s notes about the Analytical Engine sorting out the proper sign.');
    Writeln(Format('A0 + B1A1 + B3A3 + B5A5: %.4f',[B7]));
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  Writeln('Press [enter] to close');

While this is one way to calculate the 8th Bernoulli number, what would a modern implementation look like? Using Rudy’s Big Numbers library I created a sample application to calculate any Bernoulli number.

function Bernoulli(n: Uint64): BigRational;
  var a: TArray<BigRational>;
  SetLength(a, n + 1);
  for var m := 0 to High(a) do
    a[m] := BigRational.Create(1, m + 1);
    for var j := m downto 1 do
      a[j - 1] := (a[j - 1] - a[j]) * j;
  Result := a[0];

You can install the BigNumbers library via GetIt and this sample application is included.


13 Reasons NOT to use Delphi

Over the years I’ve heard a lot of reasons or excuses people don’t use Delphi. I’ve collected the thirteen best reasons here.

1. Want to Write More Code – Delphi requires less code to accomplish the same task. If you want to write more code, don’t use Delphi. Especially helpful if writing more code gives you more sense of accomplishment, or you are paid based on lines of code written.

Number, 2, Digit, Figure, Cipher, Count, Numerary, Red

2. Larger Developer Teams – Who doesn’t love having lots of co-workers? If you use other tools and frameworks then you will need more developers, more developer tools, more frameworks, and more teams to support all the platforms. Makes for better office parties. Unfortunately, when you use Delphi you only need to write the app once for all the platforms, so one team and one code base for all the platforms. With Delphi, you just can’t justify hiring all those extra developers!

3. Like Fixing Bugs – All that code you had to write to implement the features on each platform? More code means more bugs! And more bugs means more job security for you and that huge team of testers. Gotta love bugs! Heck, you can even name some and keep them as pets! You know they will be around for a while.

4. More Meetings – Since each platform has its own code and own team you need more meetings to coordinate. You don’t want features to get out of sync between platforms! And then another round of meetings to coordinate bug fixes for each platform. Everyone knows meetings have the best snacks! Since Delphi lets you support all the platforms from one codebase then you can’t have all those planning meetings!

5. More Documentation – Each platform has a completely different app (despite all the meetings to keep them in sync) so now you need completely different documentation for each platform. We know how much you love writing code, so clearly you love writing end-user documentation too!

6. Larger Support Department – Each platform has its own version so you need to talk to a support tech that knows that version of the app. Who wants an Android version that behaves similar to the iOS version? Not to mention the desktop versions! 

7. Longer Compile Times – If it isn’t weren’t for long compiles developers would never get a break from their desk. We all know Delphi compiles super fast, which means you have less time to slack off.

'Are you stealing those LCDs?' 'Yeah, but I'm doing it while my code compiles.'
‘Are you stealing those LCDs?’ ‘Yeah, but I’m doing it while my code compiles.’
If Delphi can compile 1 million lines that quick, when will you slack off?

8. Slower Execution – If your executable runs slower the user feels safer and pretends that a lot of stuff is going on in the background. With Delphi’s native execution speed your programs are fast, so your users won’t believe it is doing anything. 

9. Separated Runtime – If your program depends on an external runtime library instead of having one executable on Windows, you can blame the runtime for any bugs. All those support calls just result in telling them to update or rollback the runtime libraries. You’ll be able to convince them it is all their fault that the program doesn’t work!

10. Putting Memory to Use – Great apps should use at least a full GB of memory just like small Electron utilities do. The great thing about Electron is it includes all of the Chrome browser features like Xbox 360 controller support. Why only use a few megabytes of memory for the same simple app? The more memory the better app. Electron puts all those CPU cores to use too!

11. In Deep Love With “DLL Hell” – You love sending your customer a dozen DLLs along with your EXE and you are having so much fun to debug over the phone, which DLL is not up-to-date and makes your app fail. Closely related to #9, but worth mentioning twice!

12. Unexpected Garbage Collection Pauses – Deterministic execution is boring! What fun is it to have your program behave the same every time it runs. Delphi doesn’t have any of those unexpected garbage collection pauses to mix things up. It gives you deterministic memory management either through ref-counting, an ownership / auto-free model, or whatever level of control you want. Why control when the memory is free when you can just wait for the garbage collector?

13. Would Rather “Re-Invent the Wheel” – Delphi comes with so many useful components and libraries and has a rich 3rd party ecosystem. This means there is usually some reusable code for any task you need. That means less opportunity to create something new.

Just in case it wasn’t obvious: This is a sarcastic list of bad reasons not to use Delphi. The reality is all the excuses are just reasons to use Delphi.

Carlos translated my post to Portuguese.

News Source Code

Unexpected Benefit of Inline Variables: Conditional Blocks

Inline variables is one of the cool new feature coming in 10.3. The obvious huge use case is loop control variables, but I just discovered another great use case while reviewing some code. 

procedure DoesSomething;
  var1, var2: Integer;
  // use var1
  {$IFDEF Something}
  // use var1 &amp;amp;amp;amp;amp; var2
  {$ENDIF Something}

This is a pattern I see a lot, and it generates a hint on var2 being unused based on the current compiler directive status.

[dcc32 Hint] myUnit.pas(123): H2164 Variable 'var2' is declared but never used in 'DoesSomething'

Now there are a number of ways to deal with this with more compiler directives, which is what I’ve done in the past, but I never like adding more compiler directives. It makes the code way more complicated and harder to maintain. Now with Inline Variables I can simplify it, make it easier to maintain, and hande the hint! (all of which makes me so happy!)

procedure DoesSomething;
  var1: Integer;
  // use var1
  {$IFDEF Something}
  var var2: Integer;
  // use var1 and var2
  {$ENDIF Something}
Happy dance commencing in T-minus 10 seconds. 

What are some interesting ways you see inline variables benefiting you?


Arduino & Delphi Cosplay

Me with a Spartan Boba Fett, but I thought he was more of a Delphi Dude ?(FanX Salt Lake City - 2018)
Me with a Spartan Boba Fett. I thought he was more of a Delphi Dude (FanX Salt Lake City – 2018)

Cosplay (aka costume-play) takes the fun and imagination of childhood dress-up to extreme levels only justifiable by an adult! Not only is it expensive, and needs a large time commitment, but it is rarely comfortable.

Comic conventions are the usual place to find cosplay, but Halloween is also a good excuse for some cosplay fun. I’m a huge fan of creative cosplay and love getting my picture with cosplayers.

In writing this post I realized I am frequently attaching LEDs to my face.

The V character from Cyberpunk 2077. Cosplayer Maul. photo by eosAndy.
The V character from Cyberpunk 2077. Cosplayer Maul. photo by eosAndy. His cosplay is much better than mine.

This year I am putting together my Ultimate Cyberpunk cosplay. The first part is a jacket based on the main character from the game Cyberpunk 2077 by CD Projekt RED. Neuromancer, by William Gibson, inspired the Cyberpunk 2020 RPG by Mike Pondsmith. They both inspired this unreleased video game Cyberpunk 2077.

I’m a big fan of both the book and the original RPG and am looking forward to the video game. I combined elements from all three sources for my cosplay. The character V is more of a Street Samurai / Solo, while I wanted to go for more of a Decker / Netrunner (a little Shadowrun RPG influence in there too).

At this point the secret is out, I’m a huge nerd. I love Role Playing Games, and Cyberpunk & Shadowrun are my favorite genres.

I didn’t have my RGB Shades in time for Salt Lake FanX, so I justed glued some extra LEDs (NeoPixel 4×4 grids) to my face. They represent cybernetic enhancements, which is why I left the wires exposed. I used an Arduino MEGA 2560 by ELEGOO as the controller. It also controlled the string of NeoPixel lights in the collar. I attached a Bluetooth keyboard to one wrist and an Android Pixel phone to the other. The phone was decorative for now.

Later I got my RGB LED Shades which will become the key to my outfit for this Halloween. The RGB LED Shades use an Arduino Mini as the brains. I added an HC-06 Serial Bluetooth module to control the leds remotely.

After soldering the HC-06 onto my shades I can power them up and pair my Android phone with the HC-06 module. One note about my soldering is that if you look you will see that it is connected to two analog pins instead of digital pins. On most Arduinos the analog pins can double as digital pins (see the Pinout).

Android Bluetooth Pairing Dialog
Android Bluetooth Pairing Dialog

After that, a TBluetooth component is able to open a socket to the shades. Start the pairing with Bluetooth1.DiscoverDevices( 5000 ); In the DiscoveryEnd event handler the following code will open a socket to the HC-06 module (Thanks to Boian Mitov for the basis of this code):

  ADevice   : TBluetoothDevice;
  AService  : TBluetoothService;
  for ADevice in ADeviceList do
    // HC-06 is the name of the bluetooth device
    if ADevice.DeviceName = 'HC-06' then 
      Bluetooth1.Pair( ADevice );
      for AService in ADevice.LastServiceList do
        // FSocket is a TBluetoothSocket with larger scope
        FSocket := ADevice.CreateClientSocket( 
            AService.UUID, False );
        If Assigned( FSocket ) then

Once the socket is open you can send characters with this code.

FSocket.SendData( TEncoding.UTF8.GetBytes( 'D' ));

It is so easy to work with Classic Serial Bluetooth in Delphi (or C++Builder) and the TBluetooth component. You even could send a whole string or other binary data. For this project a single character was all I needed to change modes on the shades. A single character is also easier to process on the Arduino side. (Technically Bluetooth Classic works on all platforms, but on iOS you need special approval from Apple on a per app basis.)

RGB Rio Shades
I’m wearing the Rio RGB Shades with the Delphi powered controller on my Android phone

I took my RGB Shades with me to Sao Paulo Brazil for the 10.3 Rio preview. Even though the Bluetooth worked before the conference, my phone couldn’t make the connection on stage. When I opened my phone’s Bluetooth connection window it was obvious that with 750+ people in attendance there were a few hundred Bluetooth devices broadcasting on the same wavelength.

David Millington kicking off the Keynote
David Millington kicking off the 10.3 Keynote

My next project involves Bluetooth LE with these RGB LED Steampunk Goggles.

Jim wearing LED Steampunk Goggles
RGB LED Steampunk Goggles

I built them a year ago from an AdaFruit kit. Later I added a Bluetooth LE Module to make them controllable. Unfortunately, my soldering didn’t hold up and I need to rebuild them. This makes them both my previous and next cosplay project.

RGB LED Goggles, a horse mask, and a Los Angeles Kings hockey jersey
RGB LED Goggles, a horse mask, and a Los Angeles Kings hockey jersey (don’t ask)

I’m considering salvaging the NeoPixels and rebuilding them with an ESP32 microcontroller. The ESP32 is a little larger than the original Trinket microcontroller. This is because it has integrated Bluetooth LE, WiFi, and more pins. I’d also like to get some 50% mirrors to create an infinite LED tunnel effect (I’ll post pictures when I get them – it is really amazing).

I met Star Lord!
I met Star Lord!

I’ve worked with the TBluetoothLE component before and it is even easier to work with than TBluetooth. So I’m really looking forward to this project.

On the Arduino side, you can use the Arduino IDE and flex your C programming skills. Or you can do like I usually do and use Visuino by Boian Mitov of Mitov Software. It provides a visual drag and drop interface for programming Arduino devices. It won the Embarcadero Cool App contest in April 2017 as Boian used Delphi to develop Visuino. Boian also recently added RGB LED Shades support to Visuino (along with unboxing and assembly videos.)

Visuino makes Arduino and LED RGB Shade development easy. It is an Embarcadero Cool App developed with Delphi!
Visuino makes Arduino and LED RGB Shade development easy. It is an Embarcadero Cool App developed with Delphi!

I think Boian is a great guy who makes some great technology. Most of it has a free version and the commercial prices are very reasonable too. He is always very helpful as well.

Boian and Jim at the 2017 SoCal CodeCamp in Los Angeles
Boian and Jim at the 2017 SoCal CodeCamp in Los Angeles

So what’s next (after the Bluetooth LE RGB Steampunk Goggles obviously)? I’m working on a design for an electronic physical polyhedral die with Bluetooth LE. So you roll a physical die of a single size and it can become any number of sides you need, all controlled via your phone. Did I mention how much of a nerd I am?

I love polyhedral dice
I love polyhedral dice

Where is the ActiveX Project Type for Delphi 10.1 Berlin?

In 10.1 Berlin the ActiveX project types are missing from the New Items Window under Delphi. They are there for C++Builder, but not for Delphi.

Missing AcitveX

This only happens if you use the shiney new Feature Installer that was introduced with 10.1 Berlin. So if you use the old offline ISO installer then it shows up fine.

[Update: This was just fixed in the installer. So if you download and install after today everything will work great!]

Turns out though, it is only a simple registry issue. Instead of explaining the details behind why, here is the quick and easy work around.

  1. Close RAD Studio or Delphi 10.1 Berlin.
  2. Open RegEdit
  3. Browse to “HKEY_CURRENT_USER\SOFTWARE\Embarcadero\BDS\18.0\Known IDE Packages\Delphi”
  4. Find the entry named “$(BDS)\Bin\delphicompro240.bpl” and change the value from “__(Untitled)” to “(Untitled)”. Here is a before screenshot:
  5. And here is an after screenshot (notice the missing double underscore)
  6. Then restart your RAD Studio or Delphi 10.1 IDE and bask in all the ActiveX glory that you have restored!
    ActiveX Success
  7. Now you can create a new ActiveX Library or Type Library with your favorite programming language.

Interesting story, long ago a friend of mine worked for a software development company that used Visual Basic. He was in the “skunkworks” division that used Delphi to create all the ActiveX components that the Visual Basic developers used. There were 2 Delphi developers and about 50 VB developers. Whenever there was something that couldn’t be done in VB (which happened a lot) then they would make a new ActiveX in Delphi for them.

My friend showed him that they could actually out produce the VB developers with Delphi, but his boss was convinced that they were a VB development firm, and didn’t want to switch to the superior development tool. Unfortunately VB has died off, and all of those developers lost their jobs. My friend is glad he stuck with Delphi.


Hour of CodinGame with Object Pascal / Delphi

Hour of Code is upon us again. Take some time to learn about programming, or share what you know with others. In this blog post I show how the side CodinGame helps teach Delphi and also provide some resources for those wanting to learn programming.

A short overview to CodinGame for Hour of Code

CodinGame uses Free Pascal behind the scenes, which is based on Delphi. This means it is actually Object Pascal, which is an extension on Pascal. The code I’ve seen on CodinGame is mostly plain procedural Pascal, but since it is using an actual compiler, you should be able to use full Object Pascal. You will however be limited because Free Pascal doesn’t support some of the latest features Delphi does. Still a good learning resource and a lot of fun.

I’m planning to sit down with each of my kids, and probably my wife, on CodinGame this week! If you already know how to code then find someone you can share with too!

Resources for CodinGame and Hour of Code: 


CodeRage C++

Tomorrow is the start of CodeRage 8 C++, which is a lot like regular CodeRage, but with an emphasis on C++Builder. This time there is a lot of coverage on the new iOS support, as well as the new REST Components and FireDAC data access.

It all starts with JT’s opening keynote at 6:00 AM Pacific Time, which may also be of interest to Delphi developers who want to get some glimmers of future plans, roadmaps and announcements. Actually most of the sessions will have content of interest to Delphi developers, especially if you’ve missed some of the more recent CodeRage conferences. But if you are a C++ developers, then be sure to make time for all the sessions!

CodeRage C++ - February 25-26, 2014


The Delphi Object Pascal Language

What’s in a name? That which we call a rose
By any other name would smell as sweet;

The other day I noticed Github has a language choice for Pascal, but not Delphi. It turns out originally they had Delphi listed as a language, but some of the Delphi clones were getting marked as Delphi, so they renamed it to the more generic Pascal. Which immediately resulted in people requesting they rolled it back.

This brings up a good question though, “What is Delphi and what is Object Pascal?” Interestingly there were three implementations of Object Pascal that evolved from the original Pascal. The one we are all familiar with was designed at Borland as part of Turbo Pascal. Apple also designed one consulting with Nicholas Wirth. And there was the Think Pascal IDE with it’s own flavor.

The Borland flavor of Object Pascal evolved into the language we see in Delphi today, while the other two faded away. There actually exists a few other variations of Object Pascal, most all of which were inspired by the language that still lives in Delphi today.

Personally I think it is exciting to see so many other tools and languages in the Object Pascal and Pascal space. That is part of what made C & C++ so vibrant: All the other languages wanted to copy them (Java, JavaScript, C#, etc.)

So back to the question, “What is Delphi and what is Object Pascal.” Object Pascal is the language that powers Delphi. Object Pascal can exist without Delphi, but part of what defines Delphi is it’s Object Pascal language. Just in the same way C++Builder isn’t the only implementation of C++, but part of C++Builder is the C++ language. So Delphi and C++Builder are each the whole package: Language + IDE + Compiler + Debugger + Libraries + Tools. You could say they are the definitive implementation of those languages.

Could we see Delphi with a different language? That would be interesting. At one end of the spectrum there was Delphi for PHP (which evolved into HTML5 Builder.) It was Delphi’s Rapid Application Development concept combined with the PHP language. And then Delphi Prism which used the Oxygene language variant of Object Pascal combined with Visual Studio and .NET.

In my opinion, Delphi is a specific version of Object Pascal, if for no other reason than because it has a fabulous runtime library and framework. Using Object Pascal without TStringList and all the other useful types, function and libraries that Delphi comes with wouldn’t be much fun.


Hidden features in the Delphi Object Pascal language

A list of hidden features in Delphi Object Pascal that are great, obscure, best avoided or remarkable.

This was copied from Stack Overflow’s question of the same name which is closed and flagged for deletion. Licensed under cc by-sa 3.0 with attribution required. I’ve made a few changes, updates and some copy editing. Original question by Johan and others on May 19 2011 at 18:34. Post inspired by Jeroen W. Pluimers’ post.


Oh Yeah, the Ouya!

The Ouya is an Android powered game console / set-top box that you can pick up for $99. Not only is it a cheap game console, but it is also an affordable Android platform designed to drive big screen TVs. Easily turn a TV into a wall mounted dashboard or living picture frame.

Once the ADB driver is installed, You can develop for and deploy to it with Delphi XE5 just like any other Android device.

This video is from our Making the Connection: Programming Devices and Gadgets with RAD Studio webinar. Check out the on-demand replay and download the full source code too.

The Ouya has an SDK that goes beyond what I showed above. I’ll be revisiting it with more updates in the future.