Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating the right instance at run-time

2 views
Skip to first unread message

Geert-Pieter Hof

unread,
Feb 12, 2003, 12:14:36 PM2/12/03
to
Hello,

I'm having a problem with reading object properties from disk.

I have three classes:
1. IMachine.cls (is an interface)
2. CMotor.cls (implements IMachine)
3. CPump.cls (implements IMachine)

General declaration:
Public Machine As IMachine

At run-time, the user can select a certain machine, resulting in

Set Machine = New CMotor

or

Set Machine = New CPump

The user can write and read the object properties to and from disk. The
properties of Machine are written to disk by using a propertybag.

But now....

When I'm reading the object properties from disk, how do I know if the
propertybag belongs to an instance of CMotor or CPump?

One way of doing this is by writing the TypeName of the instance to disk.
When reading the data from disk, you could read the TypeName first and with
an IF-THEN-construction you could create the right machine.

Is there a more refined way of doing this, especially when you have a lot
more classes than just the motor and the pump.

Geert-Pieter


George Copeland

unread,
Feb 12, 2003, 5:26:59 PM2/12/03
to
"Geert-Pieter Hof" <g.m...@wbmt.tudelft.nl> wrote in message
news:b2dvdu$l4d$1...@news.tudelft.nl...

> I have three classes:
> 1. IMachine.cls (is an interface)
> 2. CMotor.cls (implements IMachine)
> 3. CPump.cls (implements IMachine)

> When I'm reading the object properties from disk, how do I know if the


> propertybag belongs to an instance of CMotor or CPump?
>
> One way of doing this is by writing the TypeName of the instance to disk.
> When reading the data from disk, you could read the TypeName first and
with
> an IF-THEN-construction you could create the right machine.
>
> Is there a more refined way of doing this, especially when you have a lot
> more classes than just the motor and the pump.

Other than using a case statement for the MachineType when you create the
object, what you are doing is exactly how I would do it.


the other guy

unread,
Feb 12, 2003, 8:48:25 PM2/12/03
to
I would first force a unique interface on both object by making an
interface class with empty mehtods only and then for each class that
use that interface, use
Implement (inserClassNameHere)

I would put a public function WhatIsThis that return a string
descriptor of the object.

**********************************************************************
Richard Jalbert Programmer-Analyst Rich...@sympatico.ca

Dog thinks: they feed me, they take care of me: they are gods.
Cat thinks: they feed me, they take care of me: I am god.
http://www3.sympatico.ca/richmann/
http://www.geocities.com/richmannsoft/
**********************************************************************

0 new messages