D^4 := Dirty Delphi Dips'n'Dricks

System
'special folder' locations
Misc
TList of TObject

 

'special folder' locations
var
   p : pitemidlist;
   pc : pchar;

begin
   SHGetSpecialFolderLocation (Form1.handle, CSIDL_PROGRAMS, p);
   new (pc);
   getmem (pc, 255);
   SHGetPathFromIDList (p, pc);
//   pc now contains path to C:\Program Files\
end;

Posssible arguments for SHGetSpecialFolderLocation are:

CSIDL_BITBUCKET Recycle bin - file system directory containing file objects in the user's recycle bin. The location of this directory is not in the registry; it is marked with the hidden and system attributes to prevent the user from moving or deleting it
CSIDL_CONTROLS    Control Panel - virtual folder containing icons for the control panel applications.
CSIDL_DESKTOP    Windows desktop - virtual folder at the root of the name space.
CSIDL_DESKTOPDIRECTORY     File system directory used to physically store file objects on the desktop (not to be confused with the desktop folder itself).
CSIDL_DRIVES    My Computer - virtual folder containing everything on the local computer: storage devices, printers, and Control Panel. The folder may also contain mapped network drives.
CSIDL_FONTS    Virtual folder containing fonts.
CSIDL_NETHOOD    File system directory containing objects that appear in the network neighborhood.
CSIDL_NETWORK    Network Neighborhood - virtual folder representing the top level of the network hierarchy.
CSIDL_PERSONAL    File system directory that serves as a common respository for documents.
CSIDL_PRINTERS    Printers folder - virtual folder containing installed printers.
CSIDL_PROGRAMS    File system directory that contains the user's program groups (which are also file system directories).
CSIDL_RECENT    File system directory that contains the user's most recently used documents.
CSIDL_SENDTO    File system directory that contains Send To menu items.
CSIDL_STARTMENU    File system directory containing Start menu items.
CSIDL_STARTUP    File system directory that corresponds to the user's Startup program group.
CSIDL_TEMPLATES    File system directory that serves as a common repository for document templates.

back to top

 

TList of TObject
If you often use the TList object, you allways are typecasting like hell. what you get is something like
If TThisIsMyObject(MyObjectList[ItemCoumt]).SomeThingLikeAnInteger = 0 then ... .

One problem is, that you can't read your code anymore. The other problem is, that Borland Delphi can't check, if you are using the right object right now, because you arte typecasting. Better would be a TList of TObject because you can't put items in the list, that doesn't fit in and you can't get items out of it, that aren't stored in it.

To create a list, that only accepts specific objects like (in this example) real values, you have to define a new TList. Create a new unit and type something like this:

unit UnitTRealList;

uses classes;

type TRealList = class(TList)
   private
      function Get (Index: Integer): Real;
      procedure Put (Index: Integer; Item: Real);
   public
      function Add (Item: Real): Integer;
      property Items[Index: Integer]: Real read Get write Put; default;
end;

implementation

function TRealList.Add (Item: Real): Integer;
begin
   result := inherited Add (Item);
end;

function TRealList.Get(Index: Integer): Real;
begin
   result := Real (inherited get (index));
end;

procedure TRealList.Put(Index: Integer; Item: Real);
begin
   inherited put (index, Item);
end;

end.

Use this class and create a new TRealList. Just write

Var
   RL   : TRealList;

begin
   RL := TRealList.Create;
   RL.Add (3.1415);
   Label1.Caption := FloatToStr (RL[0]);
   RL.Free;
end;

The TRealList now does not accept any members but only real values (except you are typecasting), and you now can read your code.

back to top