- 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.
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
------
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.
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: