Board index » delphi » How to instantiate components at run-time?

How to instantiate components at run-time?

I'm trying to give users of a database app an option to use the bde or
direct access tools like DAO and IBX.
For this you need different datamodules.
Earlier suggestions I got were to instantiate the right datamodule at
run time like:

type
  DataAccessToolType=(datBDE,datADO,datIBX);

  case DataAccessTool of
   datBDE: dmData:=TdmEdDataBDE.create(self);
   datIBX:   dmData:=TdmEdDataIBX.create(self);
   datADO: dmData:=TdmEdDataIBX.create(self);
  end;

I can make all of them "look" the same in giving their components the
same name and so, but that is not enough.

I have to declare the var dmData somewhere, but as what? I can't use a
generic tdatamodule and descend all from there because the components
internally have different properties which I need to address at run-time
like the database name and connectivity settings.

Setting dmData in each of the datamodules does not work either, because
the compiler only "remembers" the dmData declared in the last datamodule
in the uses clause of the main app.

How on earth do you do this?

Lewie

 

Re:How to instantiate components at run-time?


Quote
"Lewie Dekker" <lewi...@putra.net.my> wrote in message

news:3BD7F3F6.302CB977@putra.net.my...

Quote
> I have to declare the var dmData somewhere, but as what? I can't use a
> generic tdatamodule and descend all from there because the components
> internally have different properties which I need to address at run-time
> like the database name and connectivity settings.

> Setting dmData in each of the datamodules does not work either, because
> the compiler only "remembers" the dmData declared in the last datamodule
> in the uses clause of the main app.

> How on earth do you do this?

There are several ways, however, the obvious one would be to design a class
of the datamodule with command functions/procedures to perform tasks for
you. Each descendent can override these and do their own thing. In this
case, you would probably be taking all control of data from the forms and
using structures or objects to pass information back and forth. This is a
more difficult approach but would allow you to add more data access methods
in the future for any type of database you need.

Woody

Re:How to instantiate components at run-time?


I'll have to think this one through a bit more. Perhaps it could do the trick.
Thanks, Woody.

Lewie

Quote
Woody wrote:
> There are several ways, however, the obvious one would be to design a class
> of the datamodule with command functions/procedures to perform tasks for
> you. Each descendent can override these and do their own thing. In this
> case, you would probably be taking all control of data from the forms and
> using structures or objects to pass information back and forth. This is a
> more difficult approach but would allow you to add more data access methods
> in the future for any type of database you need.

> Woody

Other Threads