Delphi Constant
In Object Pascal or Delphi mode, Free Pascal supports the Array of Const construction to pass parameters to a subroutine. This is a special case of the Open. Delphi source code A global UNIT with functions and procedures For complex projects, I advise you to put your 'general' (globally used) variables, constants, functions and procedures in a separate unit.
In Delphi, I want to be able to create an private object that's associated with a class, and access it from all instances of that class. In Java, I'd use:
Or, if MySharedObject needed more complicated initialization, in Java I could instantiate and initialize it in a static initializer block.
(You might have guessed.. I know my Java but I'm rather new to Delphi..)
Anyway, I don't want to instantiate a new MySharedObject each time I create an instance of MyObject, but I do want a MySharedObject to be accessible from each instance of MyObject. (It's actually logging that has spurred me to try to figure this out - I'm using Log4D and I want to store a TLogLogger as a class variable for each class that has logging functionality.)
What's the neatest way to do something like this in Delphi?
9 Answers
Here is how I'll do that using a class variable, a class procedure and an initialization block:
Note that this class variable will be writable from any class instance, hence you can set it up somewhere else in the code, usually based on some condition (type of logger etc.).
Edit: It will also be the same in all descendants of the class. Change it in one of the children, and it changes for all descendant instances.You could also set up default instance handling.
Delphi Constant String Array
Last year, Hallvard Vassbotn blogged about a Delphi-hack I had made for this, it became a two-part article:
Yeah, it's a long read, but very rewarding.
In summary, I've reused the (deprecated) VMT entry called vmtAutoTable as a variable.This slot in the VMT can be used to store any 4-byte value, but if you want to store, you could always allocate a record with all the fields you could wish for.
The keywords you are looking for are 'class var' - this starts a block of class variables in your class declaration. You need to end the block with 'var' if you wish to include other fields after it (otherwise the block may be ended by a 'private', 'public', 'procedure' etc specifier). Eg
(Edit: I re-read the question and moved reference count into TMyClass - as you may not be able to edit the TMySharedObjectClass class you want to share, if it comes from someone else's library)
Please note the above is not thread-safe, and there may be better ways of reference-counting (such as using Interfaces), but this is a simple example which should get you started. Note the TMySharedObjectClass can be replaced by TLogLogger or whatever you like.
Well, it's not beauty, but works fine in Delphi 7:
..
I'm curently using it to build singletons objects.
For what I want to do (a private class constant), the neatest solution that I can come up with (based on responses so far) is:
Perhaps a little more object oriented would be something like:
That might make more sense if there were multiple such class constants.
Two questions I think that need to be answered before you come up with a 'perfect' solution. Security monitor pro 5.41 torrent.
- The first, is whether TLogLogger is thread-safe. Can the same TLogLogger be called from multiple threads without calls to 'syncronize'? Even if so, the following may still apply
- Are class variables thread-in-scope or truly global?
- If class variables are truly global, and TLogLogger is not thread safe, you might be best to use a unit-global threadvar to store the TLogLogger (as much as I don't like using 'global' vars in any form), eg
Code:
Edit: It seems that class variables are globally stored, rather than an instance per thread. See this question for details.

In Delphi static variables are implemented as variable types constants :)
Vmware vsphere 7 release. We still wanted to go through a few of the ones we see as highlights: • VMware vCenter Server Appliance (vCSA) has seen new APIs added and increased performance with this generation. • VMware vSphere 6.7 in many instances does away with the double reboot required for major ESXi enhancements.
This could be somewhat misleading.
And yes, another possibility is using global variable in implementation
part of your module.
This only works if the compiler switch 'Assignable Consts' is turned on, globally or with {$J+}
syntax (tnx Lars).
Before version 7, Delphi didn't have static variables, you'd have to use a global variable.
To make it as private as possible, put it in the implementation
section of your unit.