Changing type in objects 
Author Message
 Changing type in objects

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?

Thanks in advance,

Jan Sipke van der Veen




Wed, 18 Jun 1902 08:00:00 GMT  
 Changing type in objects

Quote:
>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).

From my experience with OOP in TP/BP, methods are dynamic, fields are
static.  There doesn't seem to be any way to explicitly override a field's
type.

Quote:
>Can anyone help me?

You'd be better off using pointers instead.  Take a look at the TCollection
object, and create a descendant of it to suit your needs.  It's polymorphic,
and you can store any type of data in it.

Quote:
>Thanks in advance,
>Jan Sipke van der Veen



--
Scott F. Earnest           | We now return you to our regularly scheduled



Wed, 18 Jun 1902 08:00:00 GMT  
 Changing type in objects

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 |
 ---------------------------------------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. OOP problem with object types, inheritance and association between objects

2. changing real type data to word type

3. type casting (objects)

4. Which DBMS can store object type from delphi?

5. IDEAS For Object Type Database

6. Type of object pointed at

7. VMT and DMT format for Object Types in 32Bit Delphi

8. Displaying Access DB( OLE Object Type) in 16bit

9. date type object using turbo vision

10. Passing variable object types to procedures/functions

11. Save and work with the Delphi 2 Object Pascal Variant type in Paradox tables

12. How to change the type of a database field

 

 
Powered by phpBB® Forum Software