Board index » delphi » Difference between object and class keywords

Difference between object and class keywords

Hi, I'm new to delphi and have a problem:
If I have two units and in unit2 i put the following declaration under the
type section:

Person = class
private
         sName : string;
         sJob : string;
public
         property Name : string read sName write sName;
         property Job : string read sJob write sJob;
end;

and then in unit1 I create a variable of type Person in the var section, the
program starts with an error message:

    Runtime error 216 at 00002AD0

which is an EAccessViolation error.

However, if I change the keyword class to object, the program runs fine.
what is the difference? Why can i not use the class keyword?

Any help much appreciated

 

Re:Difference between object and class keywords


Quote
Steve wrote in message <70uuis$d1...@eros.clara.net>...
>Hi, I'm new to delphi and have a problem:
>If I have two units and in unit2 i put the following declaration under the
>type section:

>Person = class
>private
>         sName : string;
>         sJob : string;
>public
>         property Name : string read sName write sName;
>         property Job : string read sJob write sJob;
>end;

>and then in unit1 I create a variable of type Person in the var section,
the
>program starts with an error message:

>    Runtime error 216 at 00002AD0

>which is an EAccessViolation error.

>However, if I change the keyword class to object, the program runs fine.
>what is the difference? Why can i not use the class keyword?

>Any help much appreciated

Object keyword is used  to create old-style objects (in pascal versions
prior to Delphi1), and they are not dynamic per se like new-style objects,
so when you declare it, it's allready created. New-style objects are allways
dynamic, so you have to create them before use and destroy after:
Old:
PAnObject = ^TAnObject;
TAnObject = object
  private
         sName : string;
         sJob : string;
  constructor Init;
  destructor Done;
end;

TAclass = class
  private
         sName : string;
         sJob : string;
end;

var AnObject:TAnObject;
    AnObjectPtr:PAnObject;
  AClass : TAClass
begin
{0}
  AnObject.Name:='blabla';
{1}
  AnObjectPtr:=new(PAnObject,Init); {this create a dynamic instance}
  AnObjectPtr^.Name := 'blabla'
  Dispose(AnObjectPtr,Done)
{2}
  AClass := TAClass.Create;
  AClass.Name := 'BlaBla';
  AClass.Free;
end;

Dynamic objects must ALLWAYS be created. The {1} is equivalent to {2},
preferred is {2}, but {1} has no overhead, while {2} has more and more
overhead in every newer Delphi version.
--
-------------------------
Regards,
Robert

Remove leading aa when replying.
MailTo: aarobert.ce...@eunet.si

Re:Difference between object and class keywords


Quote
>Hi, I'm new to delphi and have a problem:
>If I have two units and in unit2 i put the following declaration under the
>type section:

>Person = class
>private
>         sName : string;
>         sJob : string;
>public
>         property Name : string read sName write sName;
>         property Job : string read sJob write sJob;
>end;

>and then in unit1 I create a variable of type Person in the var section,
the
>program starts with an error message:

>    Runtime error 216 at 00002AD0

>which is an EAccessViolation error.

>However, if I change the keyword class to object, the program runs fine.
>what is the difference? Why can i not use the class keyword?

>Any help much appreciated

You have to create an instance of Person (better use TPerson for type
definition).

procedure DoIt;
var
  P: Person;
begin
  P:= Person.Create;
end;

It could be the error, but how can we find it, if you don't show us the
content of unit1.

Heiko

Other Threads