views:

124

answers:

2

I am developing a Delphi frame and need to load a list of business objects into a grid control at the beginning. Delphi frames have no OnCreate event, so what is the best way to replicate this? Should I override the constructor like is suggested on About? Should I just make a public loadData() procedure and have the parent container call it when its ready to create?

I want to make sure all the child controls are loaded and ready to go before filling them with data and I am not familiar with the Delphi component creation hierarchy.

+2  A: 

I would not tie populating of data to creation. You could reasonably create a frame and keep it but change the content it displays later. if you populate in onCreate then you have to free and recreate in order to get fresh data. So having a LoadData() is a better approach.

Depending on the complexity of the app and the number of forms and frames that you have, it may be handy to have all your Frames inherit from a common base frame. You could then introduce a virtual LoadData method in the base and override in frame subclasses.

Alternatively, you could also design an interface and have frames implement it. If done properly that could allow you to treat forms, frames or even panels, etc, uniformly.

It is difficult to give you a more specific advice, as it depends on the complexity of the gui and the app. In general, it is always good to have as little logic/code in forms/frames as possible. So introducing some kind of FormManager class that handles registration and display of forms/frames can help isolate this behavior in a single location. But if it's a small, simple app you can get away with just doing it in forms.

Daniel Maurić
A: 

I would usually choose one of two different approaches:

  1. An overridden constructor taking some kind of object which either contains all items, or know how to get them (e.g. an enumerator)
  2. A property with a setter for the same kind of object

This way, the logic of how to get the objects can be tucked away some where else.

(Now, it may be that this is what you already do, but the name 'LoadData' makes me believe that your fame is actually going to load data from some kind of storage repository instead of just displaying the data some one else has previously fetched...)

Vegar