[AccessD] Can a function return an object?

John Colby jwcolby at gmail.com
Sun Mar 26 06:25:33 CDT 2023


Bill,

I am not a 'compiler writer' obviously, however as I understand it... (and
in VBA for the example)

The interpreter keeps track of how many objects are referencing a given
object.  It does this for garbage collection purposes at the very least.
Let's assume that you have a form (which is an object).  As soon as it
opens VBA increments a counter to one, meaning one variable points to that
form.  If you pass a pointer (or reference, as some folks insist on calling
it) to a function which is going to do something with that form, now two
different pointers or references to that form exist.  The form cannot be
closed and the garbage collector cannot free the memory for the form until
both users of the form tell the system that they are done with the form.

So it truly isn't as simple as 'here is a pointer to a memory location',
have fun with it.  the system has to handle this reference count thingie.

Similarly a pointer or reference points to an object.  The object is
different depending on what is being referenced.  The footprint in memory
is going to be different for a string as opposed to a form as opposed to a
recordset.  How the object is used, what you can do with it, An object such
as a class can have entire sections of code as well as data.  In some
languages an object like a string also has code which can do things like
upper or lower or proper case it etc.

So just handing around pointers to objects is problematic.  I assume (out
of extreme ignorance of the subject) that every object has a descriptor
which tells the caller what it is and how to use said object.  So the
process of 'getting back' or using any object is going to be an entire
process.  I simply assumed this process is what 'dereferencing' means.

I have never found it necessary to understand the details of
dereferencing.  I just have a high level concept of 'it must do this kind
of thing' to use the object referenced.





On Sat, Mar 25, 2023 at 9:52 PM Bill Benson <bensonforums at gmail.com> wrote:

> I followed your discussion until the last sentence. What is de-referenced?
>
> On Fri, Mar 17, 2023 at 5:50 PM Stuart McLachlan <stuart at lexacorp.com.pg>
> wrote:
>
> > Be careful with the word "pointer".
> > A pointer "points"  to a memory location.
> > There is a differentce between a pointer and a reference.
> > You pass a reference to an object instance , not a pointer.
> >
> > An object reference (reference to an object instance) stays  the same for
> > the life of the
> > instance, but theinstance's location in memory  may change.   VBA doesn't
> > expose the
> > pointer to it. It's managed behind the scenes
> >
> > It's a bit like the difference between VARPTR and STRPTR for dynamic
> > strings.The STRPTR
> > will change every time the string is modified so that it moves in memory,
> > the VARPTR
> > remains the same since it is a pointer to a pointer to the string.
> >
> > In general, all return values are pointers returned in registers which
> are
> > de-referenced by the
> > calling statement.
> >
> >
> > On 17 Mar 2023 at 16:13, John Colby wrote:
> >
> > > Technically speaking a function returns a *pointer* to an object.  It
> > > is a 32 bit or 64 bit number which points to an address in memory
> > > where the actual instance of the object resides.  This is an important
> > > distinction because functions can accept and return actual instances
> > > of *some* things such as an integer variable.  But AFAIK objects are
> > > never passed, only pointers to objects are passed.
> > >
> > > IIRC the function is defined with the 'as' keywords after it, followed
> > > by the type of thing being returned, in this case an object of type
> > > form.
> > >
> > > Function fFrm() As Form
> > >
> > >     Set fFrm = Forms("frmLocation")
> > >
> > > End Function
> > >
> > > Now in the debug window:
> > >
> > > ?ffrm.Caption
> > > frmLocation
> > >
> > > On Fri, Mar 17, 2023 at 5:56AM Arthur Fuller
> > > <fuller.artful at gmail.com> wrote:
> > >
> > > > Can a VBA function return an object, such as a form or DAO.database?
> > > >
> > > > Can a function return a collection?
> > > >
> > > > --
> > > > Arthur
> > > > --
> > > > AccessD mailing list
> > > > AccessD at databaseadvisors.com
> > > > https://databaseadvisors.com/mailman/listinfo/accessd
> > > > Website: http://www.databaseadvisors.com
> > > >
> > >
> > >
> > > --
> > > John W. Colby
> > > Colby Consulting
> > > --
> > > AccessD mailing list
> > > AccessD at databaseadvisors.com
> > > https://databaseadvisors.com/mailman/listinfo/accessd
> > > Website: http://www.databaseadvisors.com
> >
> >
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > https://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> >
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


-- 
John W. Colby
Colby Consulting


More information about the AccessD mailing list