Passing Numeric Value One Form to Another (1 Viewer)

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:02
Joined
Sep 12, 2006
Messages
15,660
well if you have a class which contains a value you want to track, you can read and write your value with the class.


eg
create a class module called clsLngVar. It references a singular long value.
The value is set by calling the property let, and read by calling the property get. This works like a "black box", and all that the user sees is the myvalue property to read/write the value, which are exposed with intellisense.


Code:
'class module clsLngVar
Option Compare Database
Option Explicit

Private lngvar As Long

Public Property Get myvalue() As Long
    myvalue = lngvar
End Property

Public Property Let myvalue(x As Long)
    lngvar = x
End Property

So to use this, you declare a variable (object) of type clsLngVar. The class module is an object type. If you declare 2 such objects, you get 2 completely separate instances of the object So in this examble, a and b are independent instances of the class, - and won't interfere with one another. You can pass either a or b to another procedure.

So, now a normal module, using the class module

Code:
Option Compare Database
Option Explicit

Sub demonstrateclass()
Dim a As clsLngVar
Dim b As clsLngVar

Set a = New clsLngVar
a.myvalue = 6

Set b = New clsLngVar
b.myvalue = 12

MsgBox "Instance a: " & a.myvalue & vbCrLf & _
    "Instance b: " & b.myvalue

'example of passing the class instance to another function/sub
showclass b

Set a = Nothing   'dispose of the classes
Set b = Nothing

End Sub

Sub showclass(x As clsLngVar)
    MsgBox x.myvalue
End Sub


------
The class can be as complicated as you want it to be. You can make the internal values hidden/readonly/writeonly. You can have processes which manipulate the variables within the class, and expose whichever bits you want.

for example, I have a standard security class, which contains my licensing subroutines. All I need to do to see if a licence is valid is declare an instance of my security class, set the values that the class uses, and call whatever methods I want.

I just need to copy my class from application to application, and it works automatically.


Code:
dim licence as classSecurity  'my ClassModule

set licence = new classSecurity

licence.productkey = whatever
licence.productname= whatever
licence.username = whatever

'now see if the values "work"
if not licence.verified then 
  'give a fail message here (but normally within the class)
  'quit app
end if
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:02
Joined
Jul 9, 2003
Messages
16,285
you can read and write your value with the class

Thanks Dave, I will have a look at that and most probably try it out in the near future. I'm interested in these different ways of handling global variables because I've always been told never to use global variables. I suspect it's not a question of there being something wrong with using a global variable, but there's usually a better way of doing it.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:02
Joined
Sep 21, 2011
Messages
14,350
PMFJI.

Would there be any issue if you used

Code:
Sub showclass(x As Long)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:02
Joined
Jul 9, 2003
Messages
16,285
I've always been told never to use global variables. I suspect it's not a question of there being something wrong with using a global variable, but there's usually a better way of doing it.

Further to the Above:-
By the way I found the following on the Microsofts website in 2014, however I had been trying to resolve the problem with the confusion over whether you should use global variables or not long before that!

On this Page:-

Writing a property procedure

This Line:- "Property procedures should be used instead of Public variables in code that must be executed when the property value is set."

I find it difficult to grasp exactly what it means, in one sense you could understand it to mean don't use global variables, in another sense you could understand it to mean don't use global variables when you're passing values through property statements. Either way it's not clear.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 20:02
Joined
Feb 28, 2001
Messages
27,218
Uncle G. What it means is that if your public variable is plain vanilla, nobody cares what you do with it. Just set it and forget it. HOWEVER, when you have a complex object, it will need what is called an "initializer" that does more than just set a variable. For example, you don't just say "OpenRecordset" to open a recordset. You give it a query and maybe some mode flags. Then, behind the scenes that code has to store the query in a protected area and establish contact with the DB Engine servicing that recordset. Then it needs to set flags for EOF, BOF, and anything else.

Well, for objects with property procedures, similar events might be needed. In the simplest case, you just set the named property and you are done. But when you are setting a value that requires consistency with other content, the property SET code must be used not only to set the "obvious" variable, but also it might have to set other variables too.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 02:02
Joined
Sep 12, 2006
Messages
15,660
PMFJI.

Would there be any issue if you used

Code:
Sub showclass(x As Long)


I was just demonstrating that you can pass the instance of the entire class to a different sub. It depends how complicated the class is - obviously a real class is likely to include some internal computation, so passing the class (ie the pointer to the class), with all it's computational internals, is different from passing a simple variable.

It's somewhat similar to passing a form (or a control) to a sub. The sub header includes frm as Form , as a parameter, and we just pass an instance of a form - often just me, Within the sub, there's never any question about the object in use. It's just very handy to have the option in our toolbox, as it gives us a common process for any form without having to copy the code into every form.

It's longer lived than a sub though, because the class object is active until you decide you want to dispose of it.
 

Users who are viewing this thread

Top Bottom