Board index » delphi » Access violations in class member functions???

Access violations in class member functions???

I was creating a unit with Delphi 1.0 the other day, and I ran into an
Access Violation exception when a member function of my class tried to
modify a private variable of my class.  example:

-----------------------------------------------------------
unit testing;

interface

type
   myclass = class
   private
      anumber : integer;
   public
      function GetNumber : integer;
      function SetNumber(num : integer) : integer;
   end;

var
   testobject : myclass;

implementation

function myclass.GetNumber : integer;
begin
   GetNumber := anumber;
end;

function myclass.SetNumber(num : integer) : integer;
begin
   anumber := num;
end;
-----------------------------------------------------------

now lets say in another unit (like another form that uses a unit that
contains this definition) I call testobject.SetNumber.  Delphi returns with
an Access Violation.  (NT says the "location blahblahblah coud not be
read")

When I change the word "class" to object, everything works as expected.

what am I doing wrong?

--
Adrian Kwong

 

Re:Access violations in class member functions???


Quote
Adrian Kwong wrote:

> I was creating a unit with Delphi 1.0 the other day, and I ran into an
> Access Violation exception when a member function of my class tried to
> modify a private variable of my class.  example:

> -----------------------------------------------------------
> unit testing;

> interface

> type
>    myclass = class
>    private
>       anumber : integer;
>    public
>       function GetNumber : integer;
>       function SetNumber(num : integer) : integer;
>    end;

> var
>    testobject : myclass;

> implementation

> function myclass.GetNumber : integer;
> begin
>    GetNumber := anumber;
> end;

> function myclass.SetNumber(num : integer) : integer;
> begin
>    anumber := num;
> end;
> -----------------------------------------------------------

> now lets say in another unit (like another form that uses a unit that
> contains this definition) I call testobject.SetNumber.  Delphi returns with
> an Access Violation.  (NT says the "location blahblahblah coud not be
> read")

> When I change the word "class" to object, everything works as expected.

> what am I doing wrong?

        Probably you have not created the object before trying to use it.
You need something like

var Hmm: myclass;
begin
Hmm:= myclass.Create;
Hmm.SetNumber(42);
Hmm.Free;
end;

When you say "object" instead of "class" you get one of the "old
objects" - I wasn't around in the old days but I gather the way
they get insatantiated is different...

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:Access violations in class member functions???


In article <331311F4.4...@math.okstate.edu>,
   David Ullrich <ullr...@math.okstate.edu> wrote:

Quote
>When you say "object" instead of "class" you get one of the "old
>objects" - I wasn't around in the old days but I gather the way
>they get insatantiated is different...

I don't think so. I don't think there is any difference. 'class' is just a
synonym for 'class(TObject)'

--
Brad Aisa <ba...@tor.hookup.net>  http://www.hookup.net/~baisa/

"The highest responsibility of philosophers is to serve as the
guardians and integrators of human knowledge."   -- Ayn Rand

Re:Access violations in class member functions???


In article <01bc22db$7b2b2aa0$5d5ab5cf@lioness>,
   "Adrian Kwong" <a.kw...@ieee.ca> wrote:

Quote
>I was creating a unit with Delphi 1.0 the other day, and I ran into an
>Access Violation exception when a member function of my class tried to
>modify a private variable of my class.  example:
>var
>   testobject : myclass;

 I call testobject.SetNumber.  Delphi returns with

Quote
>an Access Violation.
>what am I doing wrong?

Um, are you creating an instance of TestObject via:

TestObject := Myclass.Create

before accessing it?

--
Brad Aisa <ba...@tor.hookup.net>  http://www.hookup.net/~baisa/

"The highest responsibility of philosophers is to serve as the
guardians and integrators of human knowledge."   -- Ayn Rand

Re:Access violations in class member functions???


Quote
Brad Aisa wrote:

> In article <331311F4.4...@math.okstate.edu>,
>    David Ullrich <ullr...@math.okstate.edu> wrote:

> >When you say "object" instead of "class" you get one of the "old
> >objects" - I wasn't around in the old days but I gather the way
> >they get insatantiated is different...

> I don't think so. I don't think there is any difference. 'class' is just a
> synonym for 'class(TObject)'Brad Aisa wrote:

> In article <331311F4.4...@math.okstate.edu>,
>    David Ullrich <ullr...@math.okstate.edu> wrote:

> >When you say "object" instead of "class" you get one of the "old
> >objects" - I wasn't around in the old days but I gather the way
> >they get insatantiated is different...

> I don't think so. I don't think there is any difference. 'class' is just a
> synonym for 'class(TObject)'

        Yes, "class" and "class(TObject)" mean the same thing. But I
didn't say anything about "class(TObject)", did I?

        If you say

type TAClass = class;

you get a new-style class. If you say

type TAClass = class(TObject);

that does exactly the same thing. OTOH if you say

type TAClass = object;

that gives you an old-style object. Look it up.

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Other Threads