Board index » delphi » Internal lists vs. control lists

Internal lists vs. control lists

-*-------
In many applications, the picklists shown in various places
all map to a few lists maintained by the internal model part
of the application.  To use a hoary example, a baseball
application might maintain the lists TEAMS, PLAYERS, and
COACHES, which are then displayed in a variety of control
boxes, etc.  Ideally, one could just point the control box
to the appropriate internal list:

    xControl.Items := TEAMS;

where both sides are of type TStrings, and the assignment
merely sets a pointer, so that updating TEAMS automatically
changes all control boxes that use it.  

But I get the impression that (a) Items is not assignable
in this fashion, and (b) the documentation exhorts one not
to use TStrings for this purpose.  

A BAD alternative is to copy the internal list into the
control whenever it is displayed or when TEAMS is updated.  
This is both lousy programming, since it distributes what
ought to be done once, and slow, since bringing up a form
may cause several long lists to be copied.

So ... how do people generally handle the mapping between
internal lists and control lists?

Russell

--
We unmasked the doctrines of objectivity because they threatened our
budding sense of collective historical subjectivity and agency, and we
ended up with one more excuse for not learning any post-Newtonian physics.
                          -- Donna Haraway  ("Situated Knowledges")

 

Re:Internal lists vs. control lists


On 11 Apr 1997 16:30:36 -0500, tur...@cs.utexas.edu (Russell Turpin)
wrote:

Quote
>In many applications, the picklists shown in various places
>all map to a few lists maintained by the internal model part
>of the application.  To use a hoary example, a baseball
>application might maintain the lists TEAMS, PLAYERS, and
>COACHES, which are then displayed in a variety of control
>boxes, etc.  Ideally, one could just point the control box
>to the appropriate internal list:

>    xControl.Items := TEAMS;

>where both sides are of type TStrings, and the assignment
>merely sets a pointer, so that updating TEAMS automatically
>changes all control boxes that use it.  

> But I get the impression that (a) Items is not assignable
>in this fashion, and (b) the documentation exhorts one not
>to use TStrings for this purpose.  

You can assign to Items in this fashion, and it is exactly what
TStrings is meant to do.

This does not, however, do a pointer assignment from xControl to
Teams. Instead, it copies all the strings from Teams to xControl. If
you later change Teams, you will need to reassign Teams to xControl to
update the control. You can arrange for Delphi to do this
automatically by defining an OnChange event handler for Teams (see
below).

XControl is probably a Windows control, such as a list box or combo
box. Windows stores the text for these controls internally to the
control. Thus, you have no way to assign a storage mechanism to a
Windows control. (Unless you want to invent your own controls that
share data.)

procedure TForm1.OnTeamChange(Sender: TObject);
begin
  xControl.Items := Sender as TStrings;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Teams.OnChange ;= OnTeamChange;
  { From now on, any time you change the strings in Teams, it
automatically updates xControl. }
end;

--
Ray Lischner             (send email to "lisch" at tempest-sw.com)
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)

Other Threads