Quote:
van der Veen) writes:
>Hello everyone,
>Perhaps someone could help me with the following.
>I have written some sort of database. An object holds the information
>in the database. My problem is that I have to specify what the type is
>of the information I want to store in the database. My thought was
>this: make an object which can do the storage stuff and make objects
>which inherite from this object and fill in the type here. But BP
>won't allow me to do that. Maybe an example helps:
>TYPE GeneralDB = OBJECT
> TYPE Item = INTEGER;
> PROCEDURE Add (n : Item);
> END;
> SpecialDB = OBJECT (GenericDB)
> TYPE Item = REAL;
> END;
>Types integer and real are of course simple examples: I want to store
>complex records in the database. What you can see is that I don't want
>to rewrite the code for Add just because the type Item has changed.
>But BP won't allow this (no type definitions in an object, just
>variables and procedures/functions).
>Can anyone help me?
You can't override the field types of an object in TP, but you can use variant
types, though I suggest you also have a look at TCollection's (in the Turbo
Vision help). If you want to use variant fields you can do something like:
uses Objects;
type
TOrdinalRec = record
case integer of
0: (ValByte: Byte);
1: (ValInt : Integer);
2: (ValLong: Longint)
end;
TOrdinal = object(TObject)
Value: TOrdinalRec;
procedure Add(val: TOrdinalRec); virtual;
procedure Sub(val: TOrdinalRec); virtual;
end;
TOrdinalByte = object(TOrdinal)
procedure Add(Val: TOrdinalRec); virtual;
procedure Sub(Val: TOrdinalRec); virtual;
end;
TOrdinalInt = object(TOrdinal)
procedure Add(Val: TOrdinalRec); virtual;
procedure Sub(Val: TOrdinalRec); virtual;
end;
TOrdinalLong = object(TOrdinal)
procedure Add(Val: TOrdinalRec); virtual;
procedure Sub(Val: TOrdinalRec); virtual;
end;
procedure TOrdinal.Add(Val: TOrdinalRec);
begin
Abstract;
end;
procedure TOrdinalByte.Add(Val: TOrdinalRec);
begin
Inc(Value.ValByte, Val.ValByte);
end;
procedure TOrdinalInt.Add(Val: TOrdinalRec);
begin
Inc(Value.ValInt, Val.ValInt);
end;
procedure TOrdinalLong.Add(Val: TOrdinalRec);
begin
Inc(Value.ValLong, Val.ValLong);
end;
etc, etc...
This is off the top of my head and untested, so the syntax might not be quite
right, but hopefully you've got the idea.
-- Jay
---------------------------------------------------------------------------
| Jason Burgon - author of Graphic Vision, TV-Like GUI for 256 Colour SVGA |
| http://www.en.com/users/grendel/prog/tv_prog.html |
---------------------------------------------------------------------------