Archive for the ‘Delphi 3’ Category
Posted by jpluimers on 2014/03/13
Yet another example of somehow who got bitten hard by using the with statement (I decided to give with its own category on my blog).
This time it got shared by Paul Foster on G+ and comes down to this:
Even in unsuspiciously looking code, the wit statement can bite you, especially if you need to do refactoring and (because of that) introduce two names in the same scope.
Or in Paul‘s words:
Whilst upgrading the code to remove the Containers unit (its not supported on NextGen platforms, so I have to make things work with Generics.Collections instead, (bye bye D7 support for this code) and refactor a couple stupidities in my original design (they always creep in, don’t they) I ended up with two class members of the same name. The with block then looked OK but I was in fact not access the member I thought I was.
–jeroen
via: Paul Foster – Google+ – WITH IS EVIL! God damn it, I know it makes code easier to….
Posted in Borland Pascal, Delphi, Delphi 1, Delphi 2, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Development, Pascal, Software Development, Turbo Pascal, With statement | 18 Comments »
Posted by jpluimers on 2014/03/10
Every once in a while you have multiple threads or processes wanting to write a short message to the same log file. Append then will give you an I/O error 32 (ERROR_SHARING_VIOLATION), but the below small routine will sleep a bit while retrying a couple of times.
It uses these Delphi aspects around the $I or $IOCHECKS compiler directive:
- in $I+ mode, you get exceptions when certain “classic” Pascal style I/O operations fail.
- in $I- mode, you access the IOResult to obtain the results of those I/O operations
- IOResult gets the result of the last failed operation (if any) or zero if none failed
- IOResult clears the underlying storage to zero
- $IFOPT checks for a certain state of a compiler flag
- You can store the state of $OPT in a temporary conditional define
Note there are a few tables of codes you can get back through IOResult as basically you can get many GetLastError results in IOResult as well: Read the rest of this entry »
Posted in Delphi, Delphi 1, Delphi 2, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Development, Software Development | 2 Comments »
Posted by jpluimers on 2014/02/20

Delphi Component Design
From long ago, but still very valid, as I recently had another question like “what design patterns does Delphi use?”.
The Delphi usage of patterns to make the VCL and your applications work is one of the reasons I like the Delphi Component Design: Danny Thorpe so much.
Do not let you scare by the book title: a lot of information in this book is much broader than designing components.
It is about why and how things are done in the RTL and VCL, and which patterns you can use yourself.
Try and git it while you can still get it. It is excellent, but rare to get as it has been out of print for a while.
Only a minority of the Delphi developers knows that every Delphi developer uses a Factory pattern (delphi.about.com has an example in “regular” Delphi), but then implemented using virtual Create constructors.
So: time to shed some light on that :-)
Virtual constructors are to classes like virtual methods are like object instances.
The whole idea of the factory pattern is that you decouple the logic that determines what kind (in this case “class”) of thing (in this case “object instance”) to create from the actual creation.
It works like this using virtual Create constructors:
TComponent has a virtual Create constructor so, which can be overridden by any descending class:
type
TComponent = class(TPersistent, ...)
constructor Create(AOwner: TComponent); virtual;
...
end;
For instance the TDirectoryListBox.Create constructor overrides it:
type
TDirectoryListBox = class(...)
constructor Create(AOwner: TComponent); override;
...
end;
You can store a class reference (the class analogy to an object instance reference) in a variable of type ‘class type’. For component classes, there is a predefined type TComponentClass in the Classes unit:
type
TComponentClass = class of TComponent;
When you have a variable (or parameter) of type TComponentClass, you can do polymorphic construction, which is very very similar to the factory pattern:
var
ClassToCreate: TComponentClass;
...
procedure SomeMethodInSomeUnit;
begin
ClassToCreate := TButton;
end;
...
procedure AnotherMethodInAnotherUnit;
var
CreatedComponent: TComponent;
begin
CreatedComponent := ClassToCreate.Create(Application);
...
end;
The Delphi RTL uses this for instance here:
Result := TComponentClass(FindClass(ReadStr)).Create(nil);
and here:
// create another instance of this kind of grid
SubGrid := TCustomDBGrid(TComponentClass(Self.ClassType).Create(Self));
The first use in the Delphi RTL is how the whole creation process works of forms, datamodules, frames and components that are being read from a DFM file.
The form (datamodule/frame/…) classes actually have a (published) list of components that are on the form (datamodule/frame/…). That list includes for each component the instance name and the class reference.
When reading the DFM files, the Delphi RTL then:
- finds about the components instance name,
- uses that name to find the underlying class reference,
- then uses the class reference to dynamically create the correct object
A regular Delphi developer usually never sees that happen, but without it, the whole Delphi RAD experience would not exist.
Allen Bauer (the Chief Scientist at Embarcadero), wrote a short blog article about this topic as well.
There is also a SO question about where virtual constructors are being used.
Let me know if that was enough light on the virtual Create constructor topic :-)
–jeroen via: What Design Patterns do you implement in common Delphi programming? – Stack Overflow.
Posted in Delphi, Delphi 1, Delphi 2, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Development, Software Development | Tagged: Delphi, Delphi Component Design | 4 Comments »
Posted by jpluimers on 2014/02/18
About a year ago, I wrote about Delphi: you should avoid the with statement as it makes your code less future proof. Then I already tweeted I would follow up. Time to do it now (:
Besides my first post, these links inspired me most:
Posts about the with statement usually cause a stir: people either like or dislike it with passion.
Starting with some history and examples, this posts lists a few DOs and DON’Ts when using the with statement, shows advantages and drawbacks, and shows you tools to eliminate with statements. Read the rest of this entry »
Posted in Borland Pascal, Delphi, Delphi 1, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Development, Pascal, Software Development, Turbo Pascal, With statement | 9 Comments »
Posted by jpluimers on 2014/02/13
Summary: Always try to avoid sharing .DCU files between projects.
I see a lot of projects at clients that do not have their individual DCU directories set (therefore having the DCU files in the same directory as the PAS files causing shared units to share the DCU files), or share DCU files among different projects.
Both are a very bad idea, as the compiler does not always understand when the DCU file does not match the combination of PAS file and compiler options.
The result is the occasional use of the DCU file in stead of the PAS file causing wrong code to be used, or wrong debugger information to be included.
Danny Thorpe phrased it on Stack Overflow: Read the rest of this entry »
Posted in Borland Pascal, Delphi, Delphi 1, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Development, FreePascal, Lazarus, Pascal, Turbo Pascal | 9 Comments »
Posted by jpluimers on 2013/12/19
Almost a year ago, a thread on “premature Delphi optimization” came by on G+ about this code:
procedure ExchangeInteger(var AValue1, AValue2: Integer);
begin
AValue1 := AValue1 xor AValue2;
AValue2 := AValue1 xor AValue2;
AValue1 := AValue1 xor AValue2;
end;
I don’t think that was premature optimization, just some code from an old fart that had already been programming in the era where processors had reasons to use it:
Back then, the only efficient way to exchange two variables of the same data type was using the XOR swap algorithm.
Nowadays you have more options, and this is where the fun in that thread began, which I will show in a minute.
First a bit of history
The XOR swap algorithm was widely known in the 80s of last century and before, especially because the 6502 processor (oh the days of LISA Assembler) was vastly popular, as was the Z80. Together, they powered the majority of the home computers in the 70s and 80s.
Read the rest of this entry »
Posted in Borland Pascal, Delphi, Delphi 1, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Development, History, Pascal, Software Development, Turbo Pascal, UCSD Pascal | 7 Comments »
Posted by jpluimers on 2013/10/10
Still a C# 4.0 / .NET 4 feature that I need to investigate more deeply: ExpandoObject, partially because I had very bad memories of Variant support in Delphi.
So here are a few links.
First of all: since VB.NET already does late binding with the Object keyword, you cannot use ExpandoObject with Strict On in VB.NET 10.0 and up:
Now the C# links:
–jeroen
Posted in .NET, .NET 4.0, .NET 4.5, C#, C# 4.0, C# 5.0, Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi XE, Delphi XE2, Delphi XE3, Development, Software Development | 5 Comments »
Posted by jpluimers on 2013/07/18
I bumped into the below answer that I gave a while (what is 4 years in a developer’s life ) on StackOverflow.
It is about Delphi Design Patterns. Sepcifically the Factory Pattern, and explains how virtual constructors implement it.
They are one of the 3 corner stones on which the component based Delphi form designer and object inspector are built:
- Virtual constructors
- Properties (events are just a special form of property)
- Run-Time Type Information.
So here it goes: Read the rest of this entry »
Posted in Delphi, Delphi 1, Delphi 2, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Development, Software Development | 6 Comments »
Posted by jpluimers on 2013/07/09
Sometimes a generic answer to a specific answer gives people a lot more insight into what they actually want to accomplish than a specific answer.
Plus that the knowledge does not only apply to VCL in any Delphi version: it works in any development environment where you can draw.
That’s why I like this 2D transformation answer so much: Read the rest of this entry »
Posted in Delphi, Delphi 1, Delphi 2, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/06/12
I was involved in a big project migrating a complex stack from Windows XP to Windows 7 x86, and got scared this !@#$ out of me.
The stack communicated to WebSphere MQ on AS/400 from Windows, and on the Windows side of things consisted of a UI developed in in Cool:Gen, a C interface to a Delphi DLL, which takes care of the communication to WebSphere MQ.
Digression:
This all was histerically grown. In the beginning (early to mid 90s last century) it was a big Borland Pascal/Turbo Pascal application – running on DOS, Windows 3.1x, Windows 95 and OS/2 2.x/3.0 – that talked over a propriatary layer over SNA to AS/400.
The vendor of that layer didn’t respond to a request for Windows NT 4.x compatibility, and meanwhile some client applications were about to be developed in Delphi.
So in 1997/1998 – together with a great AS/400 software developer – I wrote a SNA based APPC/CPI-C communication layer in Delphi 3 that could be accessed from both Turbo Pascal (using a file based interface) and Delphi (using an object interface).
The DOS interface was an executable around the Delphi interface, which was a set of classes.
Delphi part
The Delphi part of the DOS interface was centered around FindFirstChangeNotification/CreateProcess combined with MsgWaitForMultipleObjects/WaitForSingleObject to make the waiting as efficient as possible.
DOS part
DOS part
The DOS part of the Delphi interface was centered around this piece to make waiting efficient:
asm
int $28
mov ax, $1000
int $15 { DESQview/TopView give up time slice }
mov ax, $1680
int $2F
end;
Thanks to the RBIL: Ralf Brown’s Interrupt List (there are now multiple HTML versions of it), it makes use of these tricks so DOS applications can efficiently wait : Read the rest of this entry »
Posted in C, CVS, Delphi, Delphi 2006, Delphi 3, Delphi 5, Delphi XE2, Development, Dimensions CM by Serena, MKS Integrity, Software Development, Source Code Management | 3 Comments »