[AccessD] Can a function return an object?

John Colby jwcolby at gmail.com
Fri Mar 17 22:08:14 CDT 2023


Well Stuart, I will stop using the term pointer and take up the term
reference.  As I said previously, I always used pointer and reference
interchangeably.

I believe perhaps part of Arthur's issue is that he is trying to make VBA
into C.  It is a completely different animal.

I have worked in various implementations of C back before I got really
proficient with VBA.  I worked with it mostly to drive controllers.

It was 15 years later that I worked in C# for several years, almost
exclusively during that time. Back when I learned it, MS was touting each
of the languages on top of .net core as a thin layer that pretty much just
called down into the .net framework, mapping each language (C#, F# and
Visual Basic) down into the .Net core.

I have worked in VBA for decades, not exclusively during that time.  Using
the two languages and their environments I make no effort to understand one
in terms of the other.  C# is an OO language built on the enormously
powerful .net framework.  VBA is a self contained interpreter with
extensions for the various Office apps.  .Net is multi-tasking / threading,
VBA is single threaded.  Other than the fact that they both map english
keywords into machine code, it feels like they have little in common below
the covers.

Back to Arthurs' original point however, can a vba function return an
object, I contend that technically it cannot, since in my head that would
be returning a value not a reference and you don't pass an object by
value.  Which is the point (reference?) I was originally trying to make.

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference

It can return a reference to an object.  But in the end that is just
semantics, since the reference allows us to manipulate the object which is
really what Arthur appears to want to do.

So yes, I used (and thought of) the terms pointers and references
interchangeably in VBA.  I am still not really sure why a pass by reference
is not returning a pointer.  It is at the end of the day, an integer or
long integer which 'points' to something.  Apparently it 'points' to a
reference which 'points' to the actual object?  I'm good with that since it
makes no difference to me.  How it works under the covers isn't
particularly useful to me in this case.  I always just assumed that VBA was
doing a whole ton of magic that I am not aware of, and have no need to
understand.  Memory management for example.

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
>


-- 
John W. Colby
Colby Consulting


More information about the AccessD mailing list