Saturday, March 9, 2019

Delphi Tip of the Day - Forward Declarations. Solving the Chicken or the Egg Problem.

Today's Delphi Tip of the Day is about resolving "which came first, the chicken or the egg". Because Delphi is a strongly typed language you can't refer to something until it's been defined. However, there are situations where you want to refer to something NOW, but unfortunately it hasn't been defined yet.

In other words "I want the egg of that chicken. I know that chicken hasn't laid that egg yet. However, since I know that chicken is going to lay that egg (trust me I know), let me have that egg now."

This is where forward declarations come into play.

I was brought here by something that came up while reading Pawel Glowacki's book Expert Delphi. I'm on page 233 from Chapter 7 - Sensing the world. Pawel discusses the TSensorManager class in the System.Sensors unit.

There is main TSensorManager class that acts as a gateway to all sensor information. It has a Current: TSensorManager class property that is used to reference all sensor information. At the top of the System.Sensors unit, you can find a TSensoryCategory enumerated type that provides the top level categorization of all possible sensors: 

At this point my head was about to explode so I decided to open the System.Sensors unit and have a look at the actual code Pawel was referring to. Then it hit me.

What the hell is this empty class doing here? How can you have an empty class?
TSensorManager = class;
I remember going down this empty class learning path a while ago. But do you think I can remember what it was, or means. No. Obviously, I had not associated a nice, simple "word picture" to the meaning of this empty class. Because if I had, I would have remembered.

So now I have the nice, easy to remember, "Oh that's Delphi's way of resolving the chicken or the egg thing" word picture.

Have a look at this type definition snippet from the System.Sensors unit:"
type

  //
  //other type declarations
  //
  
  TSensorManager = class; //←-- egg reference
  
  TCustomSensor = class abstract
    public type
      TProperty = (UniqueID, Manufacturer, Model, SerialNo, Name, Description);
    private
      FOnDataChanged: TNotifyEvent;
      FOnSensorRemoved: TNotifyEvent;
      FOnStateChanged: TNotifyEvent;
      FManager: TSensorManager; //←-- egg reference
      FStarted: Boolean;
    //
    // other class definitions
    //
  end;
  
  //
  //other type declarations
  //

  TSensorManager = class abstract  //←--- chicken reference
  public type
    TFilters = TDictionary‹string tsensorfilter=""›;
  private class var
    FCurrentManager: TSensorManager;
    class function InternalGetSensorManager: TSensorManager; static;
    class constructor Create;
    class destructor Destroy;
  protected type
    TSensorManagerType = class of TSensorManager;
    //
    // other class definitions
    //
  end;

    
  //
  //other type declarations
  //

 implementation
The problem arises because there's a private field declaration in the TCustomerSensor class that references the TSensorManager class that doesn't exist yet. It's the damn "chicken or the egg" thing.

To get around this Delphi allows what is called a forward declaration.

This is not by any means the complete class definition. The complete class definition must be declared somewhere inside the same type declarations where the forward declaration resides.

Here are a couple useful links:

http://docwiki.embarcadero.com/RADStudio/Rio/en/Classes_and_Objects_(Delphi)#Forward_Declarations_and_Mutually_Dependent_Classes

http://www.delphibasics.co.uk/RTL.asp?Name=Class

Special Characters:
The  "‹" and "›" in the source code sections were created using the Alt + Keypad method.
https://www.keynotesupport.com/websites/special-characters-symbols.shtml


Enjoy!
Semper Fi
Gunny Mike
https://zilchworks.com

Monday, March 4, 2019

Delphi Tip of the Day - What is the "A" prefix I see used on parameters?

Today's Delphi Tip of the Day is all about consistent naming conventions. Consistency in the Delphi and Object Pascal language makes it easier to read and comprehend the code.

I have often wondered why the "A" prefix is used on Delphi parameters. Instead of just accepting it as some esoteric thing as I have done for the past twenty years, I googled around and found an answer. Have a look at the following code snippet:
constructor TPerson.Create(AFirstName, ALastName: string);
begin
  FirstName := AFirstName;
  LastName  := ALastName;
end;
The "A" in "AFirstName" and "ALastName" denotes an Argument as in the above constructor code example.

Typical naming conventions that are used are:
A := Argument
F := Field
T := Type
E := Exception
I := Interface
L := Local Variable
G := Global Variable
The key is to be consistent in all your code. If we as a Delphi community are consistent then it makes it much easier to communicate with each other.

#delphi #tipoftheday #capecodgunny

Enjoy,
Gunny Mike
https://zilchworks.com

Sunday, February 24, 2019

Expert Delphi - Code Errata

For the past month I have been making my way through Pawel Gloawcki's book Expert Delphi. I love this book. I am having fun and learning so much. I completed the "Game of Memory" application from Chapter 4, and it works pretty good.


I initially ran into an issue running The Game of Memory on my Galaxy S9+. In portrait mode, if I selected "6 Pairs" it rendered two columns of six. However, only the top portion of the bottom two tiles displayed. I changed the TILE_MARGIN constant from 2 to 4 and it fixed the render problem.

I moved onto Chapter 5 - Firemonkey in 3D. I discovered an error from page 162 that took me over an hour to track down. I finally figured it out. The program is supposed to display a red 3D Pyramid. However, I was only getting a blank screen. I discoverd one of the private properties was not getting set.

  TWireframe = class(TControl3d)
  private
    FDrawColor: TAlphaColor;
    FEdges: TEdges;
    FPoints3D: TPoints3D;
    FDisplayed: Boolean;
  public
    constructor Create(AOwner: TComponent) ; override;
    destructor Destroy; override;
    procedure Render; override;
    property DrawColor: TAlphaColor read FDrawColor write FDrawColor;
    property Points3D: TPoints3D read FPoints3D;
    property Edges: TEdges read FEdges;
    property Displayed: Boolean read FDisplayed write FDisplayed;
  end;

It is the code for the constructor where the error is:

The private boolean field FDisplayed was not getting set to true.

constructor TWireframe.Create(AOwner: TComponent);
begin
  inherited;
  FPoints3D := TPoints3D.Create;
  FEdges := TEdges.Create;
  FDrawColor := TAlphaColorRec.Red;
end;



As soon as I added this code to the constructor it worked like it was supposed to.

constructor TWireframe.Create(AOwner: TComponent);
begin
  inherited;
  FPoints3D := TPoints3D.Create;
  FEdges := TEdges.Create;
  FDrawColor := TAlphaColorRec.Red;
  FDisplayed := True; // This is missing on page 162 of the book
end;


It probably wouldn't have taken me so long to figure this out if I was a more seasoned Delphi programmer. Oh well, it is what it is. So, for any of you who are reading or plan to read "Expert Delphi" keep this little tidbit in mind when you get to Chapter 5.

I have submitted an errata to the folks at https://packtpub.com

Enjoy,
Gunny Mike
https://zilchworks.com


Saturday, December 8, 2018

Code Rage 2018 Replay: What's New With RAD Studio 10.3 Rio

I was unable to attend Code Rage 2018 this year. I'd like to thank Embarcadero for making the replays available. In this video, Sarina Dupont, David Millington, and Marco Cantu discuss what's new with RAD Studio 10.3 Rio. I like the new changes to the IDE.

Code Rage 2018: RAD Studio 10.3 Rio Product Address
https://www.embarcaderoacademy.com/courses/441209/lectures/7010714


Q&A Slide 

10.3 Rio is now available!
https://community.idera.com/developer-tools/b/blog/posts/delphi-c-builder-and-rad-studio-10-3-rio-are-now-available

IDE Main Window:
https://community.idera.com/developer-tools/b/blog/posts/new-in-10-3-ide-ui-improvements-in-the-main-window

IDE and Project Options:
https://community.idera.com/developer-tools/b/blog/posts/new-in-rad-studio-10-3-options-dialog-improvements

GetIt, Compile and other dialogs:
https://community.idera.com/developer-tools/b/blog/posts/new-in-rad-studio-10-3-improvements-to-the-getit-new-items-and-compile-dialogs

C++17:
https://community.idera.com/developer-tools/b/blog/posts/new-in-rad-studio-10-3-c-17-asynchronous-c-code-completion-and-more

Delphi inline variables & type inference:
https://community.idera.com/developer-tools/b/blog/posts/introducing-inline-variables-in-the-delphi-language

Delphi RTL Improvements:
https://community.idera.com/developer-tools/b/blog/posts/delphi-rtl-improvements-in-10-3-rio

High DPI perMonitorV2 & GetSystemMetrics:
https://community.idera.com/developer-tools/b/blog/posts/vcl-support-for-per-monitor-v2-and-getsystemmetrics-coming-in-10-3

High DPI Image List:
https://community.idera.com/developer-tools/b/blog/posts/new-in-rad-studio-10-3-high-dpi-image-list-for-windows

Enjoy,
Gunny Mike
http://www.zilchworks.com

Wednesday, November 28, 2018

The 20-Second Hug

program TwentySecondHug;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

begin
  try
    Writeln('The 20-Second Hug');
    Writeln('Copyright (C) 2018 by Michael J. Riley');
    Writeln('(May be freely distributed worldwide)');
    Writeln('#20SecondHug #20SecondHugs #PilotLight ');
    Writeln(#13#10);
    Writeln('+--------------+');
    Writeln(' Instructions ');
    Writeln('+--------------+');
    Writeln(' 1. Squeeze recipient.');
    Writeln(' 2. Don''t let go until this window closes.');
    Sleep (20000); //Stay awake don't miss this part;
      { TODO 1 :
      Translate instructions into other languages.
      Ask Delphi programmers for help by putting
      translations in blog comments. }
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Enjoy,
Gunny Mike
zilchworks.com

Sunday, August 12, 2018

Delphi Book Collection

One week after I wrote about Warren Buffet's 25-5 rule I created my own "Top 5" list. However, this time I put a little twist on it. Instead of creating a to-do list of things I wanted to accomplish in the next six months, I created a list of the top 5 things I have accomplished within my lifetime. It took me about five or six days but I finally came up with my list.

#4 on my list: "Creating a software product back in 1991 that still generates sales today."

If you check the list of Delphi releases you'll notice that Delphi didn't exist in 1991. Turbo Pascal was the tool I used to create that first version of my software. Here is a picture of Zilch v1.11. Unfortunately, I do not have a copy of my very first version 1.0.

Zilch Version 1.11 (Version 1.0 not available)

Learning Turbo Pascal was fun. I owe my passion and desire for computer programming to Jeff Duntemann. He wrote a book called "Complete Turbo Pascal". It was that book that taught me everything I needed to know about computer programming, and the Pascal language. In fact, the reports within my software are pulled using the double-linked list example in his book. Duntemann does a brilliant job of explaining this concept.

That book is by far my absolute favorite programming book. I used several snippets of code, and in some cases entire routines from the examples in his book. Like the linked list routines. I remember getting close to being done. My software was about a month away from being released as shareware. And it dawned on me, "Holy crap. I've used a bunch of code from this book and it's copyrighted. Can I do this?" So I called Jeff and the conversation went something similar to this...

Me: Mr. Duntemann.

Jeff: Yes.

Me: You don't know me. My name is Michael Riley. I'm a US Marine developing software in my spare time.

Jeff: That's good. I like Marines.

Me: Mr. Duntemann, I've...

Jeff: Please call me Jeff not Mr. Duntemann.

Me: Okay. I've been using your book "Complete Turbo Pascal" to learn how to write my software. And I've embedded several of the code snippets into my software. The're all over the place. I've even used the complete linked-list code routines. Am I allowed to do this? Can I use your code in my own software?

Jeff: Of course. That's why I wrote the damn book.

Me: Whew, that's a relief. You have no idea how worried I was. I really like how you explained double linked lists.

Jeff: I get quite a few calls from people telling me they have my book and asking to explain something. I remember the phone ringing at 1 in the morning one time and I was explaining linked lists to this guy. I remember using a kite metaphor. You see it's like a kite with a tail, and attached to that tail is another kite with a tail. And attached to that tail is another kite. Kites and tails with attached kites keep going on and on and on.

I love this guy. That was 1991 and Jeff and I have been friends ever since. One of these days I'm going to have to get Jeff to sign this book. I've completely destroyed the spine. I even had to use clear shipping tape to hold the spine together.

Complete Turbo Pascal Third Edition - Jeff Duntemann

My books have been scattered all over my basement office. When I want one I go searching from pile to pile. Moving stuff, looking around. It's frustrating and not very productive. Arrrgh!

It's been like this for a long time. It's not just books that are unorganized. It's me. It's everything. Besides, the older I get the harder it is to keep track of stuff. Stuff I need to do. When it needs to get done. Who I owe it to. Who owes me stuff. All that stuff.

So, I dusted off my old Franklin Planner that I haven't used since 2010. Inserted the new planner pages on Wednesday night and started using it the next day. Life is so much better now. I set a task for organizing my basement home office on Saturday. Then I decided to gather up all my Delphi books and put them on one single bookshelf. Wow, I have quite the collection.

Click to enlarge image

And guess what? All those books are tied to my #4 lifetime accomplishment: "Creating a software product back in 1991 that still generates sales today".

However, it not about what I did. It's about people. The people who helped me while I was in the midst of doing the thing I did. Like I said, I owe my #4 to Jeff Duntemann. Without him this never would have happened. Look what it's lead to. It's because of Jeff that I stayed enthused, and stayed passionate. Passionate enough to buy 32 Delphi books over the past 28 years. Not to mention the eBooks sitting on my computer. Thank you Jeff.

Don't worry I've told this to Jeff already. He's not hearing this for the first time reading it here. What kind of guy do you think I am.

I have a challenge for you! I want you to create your own "Top 5" list of lifetime achievements.

It doesn't matter if it takes you a week, or two weeks, or longer. Just do it. Then sit and think about your accomplishments. Think long enough about them until you discover who helped you get there. And then, reach out to them and let them know.

Enjoy,
Gunny Mike
zilchworks.com



Sunday, May 20, 2018

Don't Just Code in Delphi, Think in Delphi

I have always struggled with fully understanding the object oriented nature of Delphi. I still struggle with it today. For example, I have tried to read Nick Hodge's book "Coding in Delphi" three times and can't get past page 23. I'm currently on page 96 of Pawel Glowaki's book "Expert Delphi" and had to stop because he talks about using the TTextWriter class which is a class with virtual abstract methods. Virtual Abstract Methods, are you kidding me. What the hell are Virtual Abstract Methods.

(Expert Delphi page 96) "Notice that the TTextWriter class is a class with virtual abstract methods that just define the interface to the text writing functionality so we need to use one of the text writer descendants such as TStringWriter."

Okay, so I get a small glimpse into what Pawel's talking about. These VAM's are just interface definitions. They don't really exist. The real functions exist somewhere else. In this case the TStringWriter class. I'm just barely hanging on here, but I'm hanging on.

I blame my Delphi ignorance on my lack of going back to square one and learning Delphi's OOP think from the beginning. At the time I started using Delphi I just plowed ahead and made the code work. I wanted a Windows program and that is all I cared about. Looking back at it, I'd say I forced Delphi to work like my top-down procedural thinking. I never fully embraced the Delphi OOP think.

Hodges, Glowaki, Cantu, and all the other Delphi authors out there think in Delphi. And if I want to understand them and be proficient in Delphi I need to think in Delphi too.

So, how do you think in Delphi?

You find a resource that explains Delphi in a simple straight-forward manner. Perhaps a resource that teaches you how Delphi came into being. Does such a resource exist? Yes. And I just so happened to have a copy on my bookshelf.


Here are three paragraphs from page 7 and 8. If these paragraphs speak to you like they did me, then I highly recommend you read the entire Turbo Pascal 5.5 Object Oriented Programming Guide.

The challenge of object-oriented programming (OOP) is that it sometimes requires you to set aside habits and ways of thinking about programming that have been considered standard for many years. Once that is done, however, OOP is simple, straight- forward, and superior for solving many of the problems that plague traditional software programs. 

A note to you who have done object-oriented programming in other languages: Put aside your previous impressions of OOP and learn Turbo Pascal 5.5's object-oriented features on their own terms. OOP is not one single way; it is a continuum of ideas. In its object philosophy, Turbo Pascal 5.5 is more like C++ than Smalltalk. Smalltalk is an interpreter, while from the beginning, Turbo Pascal has been a pure native code compiler. Native code compilers do things differently (and far more quickly) than interpreters. Turbo Pascal was designed to be a production development tool, not a research tool. 

And a note to you who haven't any notion at all what OOP is about: That's just as well. Too much hype, too much confusion, and too many people talking about something they don't understand have greatly muddied the waters in the last year or so. Strive to forget what people have told you about OOP. The best way (in fact, the only way) to learn anything useful about OOP is to do what you're about to do: Sit down and try it yourself.

I'm convinced this little 124 page resource will give me the solid foundation of Delphi think that I've  been missing. I was so excited to find this little gem and what it offers, I had to stop reading and tell you guys about it.

A copy is available on archive.org

Enjoy!

Semper Fi
Gunny Mike
zilchworks.com