The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Archive for the ‘Delphi 6’ Category

Delphi: the Factory Pattern with virtual Create Constructors (via: What Design Patterns do you implement in common Delphi programming? – Stack Overflow)

Posted by jpluimers on 2014/02/20

Delphi Component Design

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:

  1. finds about the components instance name,
  2. uses that name to find the underlying class reference,
  3. 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: , | 4 Comments »

Delphi: `with` dos and dont’s (more of the latter though).

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 »

Do not share DCU files between projects (via: delphi – Compiler Directive IF and IFEND – Stack Overflow)

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 »

XOR swap/exchange: nowadays an almost extinct means to exchange two distinct variables of the same size

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 »

Delphi – Direct3D and the wrong FPU state: Now() function returns a wrong value (via: StackOverflow)

Posted by jpluimers on 2013/10/31

The question datetime – Delphi Now() function returns a wrong value – Stack Overflow is similar to my article Delphi – Michael Justin had strange floating point results when his 8087 FPU Control Word got hosed.

Good that stackoverflow user Anton Zhuchkov found out the cause himself: his answer indicates the Precision Control (and rounding) part of the FPU state got hosed by wrongly initializing the Direct3D device.

I edited his answer with some extra links to documentation.

Finally I’ve found the solution. I needed to specify the D3DCREATE_FPU_PRESERVE flag when creating a D3D device by D3D.CreateDevice.

Otherwise, without that flag, all floating point operations are performed with single precision. As the TDateTime is a simple Double, and Now() functions is consist of simple addition of date value to time value, it all get messed up by DirectX “smart” override.

Problem solved. It was a tricky one indeed. :)

–jeroen

via: datetime – Delphi Now() function returns a wrong value – Stack Overflow.

Posted in 8087, Algorithms, Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 5, Delphi 6, Delphi 7, Delphi XE, Delphi XE2, Delphi XE3, Development, Floating point handling, Software Development | 2 Comments »

.NET/C#/VB.net: some links on ExpandoObject

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 »

Delphi virtual constructors: example of the “Factory” design pattern (via: Stack Overflow)

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 »

2D transformations as answer to “delphi – Change orientation of a shape” – Stack Overflow

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 »

Don’t pass interfaces between application architectures over a DLL boundary

Posted by jpluimers on 2013/05/15

It is unwise to pass objects allocated in one framework over a DLL boundary to a different framework.

In the case of Using C dll in delphi return nothing, someone tries to pass an Interface to some memory in the C side over to Delphi.

Unless that interface is COM based, don’t do that!

In a more general way: don’t pass memory allocated on the DLL side over to the client side, no matter what kind of client you have.

From the DLL, either pass simple types, or fill buffers allocated at the client side.

Edit:

There was a nice Delphi DLL return string from C# … .NET 4.5 Heap Corruption but .NET 4.0 works? Explain please? – Stack Overflow question explaining in detail what to do for strings in a specific case: use the COM heap on the Delphi side using CoTaskMemAlloc (actually it uses the OLE task memory allocator as the Old New Thing explains).

–jeroen

via: Using C dll in delphi return nothing – Stack Overflow.

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, 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, Software Development | 5 Comments »

Delphi + ADO + dBase – Stack Overflow

Posted by jpluimers on 2013/04/10

For my research queue:

I should look at the below ConnectionStrings to access dBase with ADO from Delphi, If I ever need to do that.

Thanks Cromulent for asking, Nelson for editing and Pieter for answering:

Microsoft dBase ODBC driver

Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;OLE DB Services = -1;Extended Properties=dBase IV;Dbq=c:\mypath

doing operations like ADOTable1.Open are very fast (good) but GetIndexNames returns nothing (bad).

Microsoft Jet OLEDB 4.0 driver

Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=dBASE IV;OLE DB Services=-1;Data Source=c:\mypath

doing operations like ADOTable1.Open are exceedingly slow (bad) while GetIndexNames does return index names the way it should (good).

How do I get both speed and the index info via ADO for the dBase tables?

Microsoft Paradox Driver 7.x

“We use the following connection string which works really well.”

Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="Driver={Microsoft Visual FoxPro Driver};UID=;SourceDB=c:\mypath;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;"

–jeroen

via Delphi + ADO + dBase – Stack Overflow.

Posted in Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 5, Delphi 6, Delphi 7, Delphi XE, Delphi XE2, Delphi XE3, Development, Software Development | 8 Comments »