[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Example of Tpersitant Class

Posted on 2006-11-24
Medium Priority
Last Modified: 2012-06-27
       Iam Abu.I am using Delphi-5.I just want to learn about Tpersistant and how to use Tperstiant with small example.Everyone says it is used for streaming of data.But i dont know what is streaming in delphi with easy example
Question by:abunectar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
LVL 28

Expert Comment

ID: 18007131
that means that it can be save to a stream. a stream can be anything from a memory stream to a filestream or some custom stream. (you should look up stream in you language dictionary if you do not know the meaning of the word)

TPersistent offers the means (functions) to persist a class easily (save and load to and from a stream). so any class that extends TPersistent will have it's published properties save to a stream.

quote from delphi help:

TPersistent is the ancestor for all objects that have assignment and streaming capabilities.




TPersistent encapsulates the behavior common to all objects that can be assigned to other objects, and that can read and write their properties to and from a form file (.xfm or .dfm file). For this purpose TPersistent introduces methods that can be overridden to:

Define the procedure for loading and storing unpublished data to a stream.
    Provide the means to assign values to properties.
    Provide the means to assign the contents of one object to another.

Do not create instances of TPersistent. Use TPersistent as a base class when declaring objects that are not components, but that need to be saved to a stream or have their properties assigned to other objects.

all components are derived from TPersistent which allows them to be save and loaded from a stream. specifically, if you look in a dfm file, you will notice that it is a text file with the components from a form saved in there.

some examples of use:


Author Comment

ID: 18083194
In DFM when we right click and view as text,we see stream.Is that created bt Tpersistant class
LVL 28

Expert Comment

ID: 18083360
well, you could say that it's a stream though I never heard anyone reffering to the contents of a dfm as a stream :)
when you look in a dfm you will see something like:

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 224
    Top = 200
    Width = 75
    Height = 25
    Caption = 'save'
    TabOrder = 0
    OnClick = Button1Click
  object Button2: TButton
    Left = 312
    Top = 200
    Width = 75
    Height = 25
    Caption = 'load'
    TabOrder = 1
    OnClick = Button2Click
and continues with rest of the components from the form. But only components will be saved there. tpersistent only gives the idea how to do it. the base support. it's tcomponent that does the hard work in the case of dfm saving/loading.

the above dfm contains an object Form1 of type TForm1 which also contains a few objects, button1 and button2. all objects have their published properties saved (in the form: property name = property value).

if you look in tstream class you will notice 2 functions: ReadComponent and WriteComponent which are called when you want to save a component to a stream. those functions will iterate through the published properties of teh comonent and save them to the stream. this is basically how that dfm is betting to be a text file.

now, what TPersistent actually does in this case is just make the inherited classes to also inherit it's compilation settings, which if you look in the delphi sources (if you have the right delphi version, not personal or whatever) you will see that tpersistent is defined as:


  TPersistent = class(TObject)
    procedure AssignError(Source: TPersistent);
    procedure AssignTo(Dest: TPersistent); virtual;
    procedure DefineProperties(Filer: TFiler); virtual;
    function  GetOwner: TPersistent; dynamic;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); virtual;
    function  GetNamePath: string; dynamic;


the key thing here is the {$M+} about which delphi help says:
Type      Switch
Syntax      {$M+} or {$M-}
Default      {$M-}
Scope      Local
The $M switch directive controls generation of runtime type information (RTTI). When a class is declared in the {$M+} state, or is derived from a class that was declared in the {$M+} state, the compiler generates runtime type information for fields, methods, and properties that are declared in a published section. If a class is declared in the {$M-} state, and is not derived from a class that was declared in the {$M+} state, published sections are not allowed in the class. Note that if a class is forward declared, the first declaration of the class must be declared with the $M switch.

When the $M switch is used to declare an interface, the compiler generates runtime type information for all properties and methods. That is, for interfaces, all members are treated as if they were published.

Note:      The TPersistent class defined in the Classes unit of the component library is declared in the {$M+} state, so any class derived from TPersistent will have RTTI generated for its published sections. The component library uses the runtime type information generated for published sections to access the values of a component's properties when saving or loading form files. Furthermore, the IDE uses a component's runtime type information to determine the list of properties to show in the Object Inspector.

Note:      The IInvokable interface defined in the System unit is declared in the {$M+} state, so any interface derived from IInvokable will have RTTI generated. The routines in the IntfInfo unit can be used to retrieved the RTTI.

There is seldom, if ever, any need for an application to directly use the $M compiler switch.


so, basically, the tstream class will use RTTI to save the component to the stream. and TComponent inherits from TPersistent so it also inherits the {$M+}

that is why TPersistent is the base class for any class that needs persistance: so you don't have to remember to add the {$M+}/{$M-} switches and to have a base class that defines the base functionality (see tpersistant definition above).

this allows you to write for example:



procedure whatever;
var a:ta; b:tb;
  // initialize a,b

You should not get into the deep dark ways of *how it's done* right now. try concentrating on "how to use" instead. the 3 examples I've given you a few days back shold be a starting point. you will not interface directly with tpersistant, but through the classes that extend it, so it's best to get familiar with thos for starters and tcomponent is maybe the easiest (or at least the most used :) )

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 18083482
Thanks cuily for ur quick response.Sorry for days i delayed.Iam really struggling hard to understand the example u have given.Thats why i came into assumption that Text file in dfm is due to tPeristent class.But iam wondering about the o/p. Since there is no output iam struggling hard to find the workings of Tpersistant.And more over iam jus a beginner in delphi-5
LVL 28

Expert Comment

ID: 18083500
let's look at the problem from another angle: why are fixated on tpersistent? you don't work with it directly anyway so I don't see why spend time understanding it. all you have to know is that it provides a few methods for copying object properties from one to another and that it enables RTTI (Run Time Type Information) for all classes that inherit from it.
so again: why do you want to understand how TPersistent work? it's not tpersistent itself that implements the persitency: as I said, it only enables it.

Author Comment

ID: 18083558
At interview point they are asking what is tpersistant and how it is useful.Thats why iam asking this question to u
LVL 28

Accepted Solution

2266180 earned 400 total points
ID: 18083593
well ... at its simplest description: tpersistent is a class that enables RTTI for all classes that extend it and provides means of copying object properties among eachother. it's usefull for cloning objects, saving them to streams or any other operation that involves knowing the object properties and their values.
you can mix the above with a few info from the help quotes I already posted.

and if they want detailed tehnical information, then the job is probably not for you since you'll have to start learning about RTTI for starters, which itself is quite a big chapter (and very tehnical).

Author Comment

ID: 18083673
Thanks Ciuly for the excellent defnition

Author Comment

ID: 18083712
Ciuly if u dont mind kindly tell me the usage of rtti
LVL 28

Expert Comment

ID: 18083734
you can start from here: http://www.blong.com/Conferences/BorConUK98/DelphiRTTI/CB140.htm
it's a pretty good and easy article in my opinion. then you can search for some more, on specific things if you will want to know more specific things. The above article however should give you a picture of what you can do with rtti

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question