Class Scope

A

Albert D. Kallal

I cannot see how you can have a variable local to your form module
with the same name as a public variable and then have the local
variable refer to the public instance, unless you set it
accordingly. I don't see that you did that anywhere

I do use a "set" command in the forms on-load event.

So, this is a public variable in the FORMS code module. So, it is public,
but NOT global.

The concept here is that I want to pass along a class object from one form
to the next form opened

So, to copy from my above post I have in the forms module defs:

Option Compare Database
Option Explicit

Public clsBookInfo As clsBooking
Dim frmPrevious As Form

Note how there is no new keyword used. I not going to instantiate a new
copy,but I want to pick up the class object from the previous CALLING form.

So, my on-load code for this form I go:


Set frmPrevious = Screen.ActiveForm
Set clsBookInfo = frmPrevious.clsBookInfo

Note the set command in the above on-load event. (that's what you likely
missed here).

I simply don't want a global var here. I want the calling form to "pass" the
class object to this form.

As I mentioned, if there is a serous of 4 forms (called from each other),
ONLY the 1st form has to create an instance of the class object. All other
forms opened AFTER that 1st form simply use the set command in the load
event to pick up that class object.

So, I was just pointing out that using set in this fashion creates a pointer
to the object, not a new instance.

for example:

dim c1 as control
dim c2 as contorl
dim c3 as contorl

If I go
set c1 = me.TextBox1
set c2 = c1
set c3 = c2

c3.Value = "hello"
debug.print me.TextBox1 -->hello

In the above, I don't have 3 copies of the contorl. I have 3 pointers to the
same object. Modifying anyone of them will cause all others and including
the me.Textbox1 value to change. I am doing exactly the same thing by
passing the object from form to form. I am passing the object along so I
don't have to use a bunch of forms references to get my hands on the class
object that started out 3 form deep ago.

So as each new form is launched it picks up the Previous class object that
declared local to the form (but since I'm using the set command, it's not a
new instance, but simply a pointer to the previous forms class object).

I am only doing this so I don't have to hard code reference to the previous
form name. I also don't want to use a global var either. I also do not have
to worry if one form is called from two DIFFERENT forms in the application
that created their own instance of that class object (and in fact I don't
care if it was the previous form that created the instance of the class
object, or five forms down earlier). The simple issue here is that I passing
the object along for the ride...
any reason to have the form-level variable in the first place *if*
you want to use the instance that is being referred to with the
public variable.

Well, the variable is pubic, but not global. It is only declared as public
in the form's module to because I want the next calling form to be able to
pick it up with ease. If I use set without the new keyword..then it just a
pointer to the object that was already created. And of course as these forms
close, the class object values set come back down along for the ride (just
like setting anyone of those controls in the above code example does).
 
D

David W. Fenton

I do use a "set" command in the forms on-load event.
So, this is a public variable in the FORMS code module. So, it is
public, but NOT global.

OK, what I misinterepreted was the beginning of your post, in that
you didn't specify that the first instance of clsBooking was in a
form, though when you did the SET statement later, you did refer to
it as a member of the form.

I get the concept, and don't need an explanation. I just didn't
understand your original post's explanation of it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top