Variant fields in object declaration in TP7 or DELPHI 
Author Message
 Variant fields in object declaration in TP7 or DELPHI

Does somebody know the reason why you can not declare
variant (case) fields in object declaration in TP7 or Delphi?

It is possible to do it in Record declaration like

TestRecord = Record
               Case Byte
                 0: (W: Word);
                 1: (LowByte, HighByte: Byte);
                 2: (B: Boolean);
             end;

The size of the record is size of longest case.

I see no reason why this is not allowed for objects (Classes) like

TestObject = Object
               Case Byte
                 0: (W: Word);
                 1: (LowByte, HighByte: Byte);
                 2: (B: Boolean);
             end;

Aren't objects just records with methods?

Igor



Wed, 18 Jun 1902 08:00:00 GMT  
 Variant fields in object declaration in TP7 or DELPHI

Quote:

> Does somebody know the reason why you can not declare
> variant (case) fields in object declaration in TP7 or Delphi?

> It is possible to do it in Record declaration like

> TestRecord = Record
>                Case Byte
>                  0: (W: Word);
>                  1: (LowByte, HighByte: Byte);
>                  2: (B: Boolean);
>              end;

> The size of the record is size of longest case.

> I see no reason why this is not allowed for objects (Classes) like

> TestObject = Object
>                Case Byte
>                  0: (W: Word);
>                  1: (LowByte, HighByte: Byte);
>                  2: (B: Boolean);
>              end;

> Aren't objects just records with methods?

> Igor

From BP7 help files:

"Object types     (object: reserved word)

An object type is a data structure that contains a fixed number of
components.

 Syntax:
 object
   Field;
   Field;
   ...
   Method;
   Method;
 end;

 Remarks:
Objects consist of an identifier, a colon and a data type. In addition,
object contain method headers.

Each component is either a field (which contains data of a particular
type)
or a method, which performs an operation on the object.

The declaration of a field specifies an identifier that names the field,
and
its data type. The declaration of a method specifies a procedure,
function,
constructor, or destructor heading.

   Field  = FieldName(s): type;
   Method = procedure MethodName(<parameter(s)>:type);
     or   = function MethodName(<parameter(s)>:type):type;
     or   = constructor MethodName(<parameter(s)>: type
[;<parameter(s)>:
            type]); [virtual];
     or   = destructor MethodName[(<parameters>: type)];[virtual];

An object type can inherit components from another object type. The
inheriting object is a descendant and the object inherited from is an
ancestor.

The domain of an object type consists of itself and all its descendants.

 See Also:
 object-type constants"

Looks like it's not a record with methods.

Regards Hanford



Wed, 18 Jun 1902 08:00:00 GMT  
 Variant fields in object declaration in TP7 or DELPHI

Quote:

> Does somebody know the reason why you can not declare
> variant (case) fields in object declaration in TP7 or Delphi?

The "reason" you should ask Borland team (I do not know
if it has to do with the parser or the organisation of
the type during semantic analysis; certainly this is
possible to add with the current data structures).

However, it is pretty easy to turn the limitation.
Just declare your record with variants in the usual way,
and then declare a object type which single member
is of the record type you just declared.
As a result, you have to write a "r." in addition,
but that is not that heavy, I think.

Antoine



Wed, 18 Jun 1902 08:00:00 GMT  
 Variant fields in object declaration in TP7 or DELPHI


Quote:

>> Does somebody know the reason why you can not declare
>> variant (case) fields in object declaration in TP7 or Delphi?

>> It is possible to do it in Record declaration like
...
>> I see no reason why this is not allowed for objects (Classes) like
...
>> Aren't objects just records with methods?

>> Igor
>From BP7 help files:

>"Object types     (object: reserved word)

>An object type is a data structure that contains a fixed number of
>components.
...

>Looks like it's not a record with methods.

- not "just" that, anyhow.

But there's no reason why a data member of an object couldn't be a
variant record, if you really want to do that sort of thing.

type
  t1 = Record
    case Kind: integer of
      1: (N: real);
      3: (S: string);
  End;
  t2 = object
    x : t1;
    procedure pn;
    procedure ps;
  End;
procedure t2.pn;
Begin
  Writeln(x.n:5:2);
End;

procedure t2.ps;
Begin
  Writeln(x.s);
End;

Var
  ob : t2;
Begin
  ob.x.n := 1.234;
  ob.pn;
  ob.x.s := 'blah';
  ob.ps;
End.

However, don't assume that the fields you put into an object account
for all the space taken by that object: if there is a virtual method,
there is also a VMT link.
FP



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

 Relevant Pages 

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

2. DElphi 3 paradox table empty fields Variant Conversion error

3. Variant does not reference an automation object

4. variant record to object?

5. Variant type and TDateTime field incompatibility

6. Delphi and MSAccess OLE Object fields

7. accessing ole object fields in MSAccess from Delphi 2.0

8. ODBC32 API Declaration Unit for Delphi 2.0

9. Encrytion Components(Delphi), Object Pascal Objects(other) Available for DL

10. TP7 object question

11. Oriented Object Tp7

12. Objects.pas for TP7

 

 
Powered by phpBB® Forum Software