|
-
Sep 11th, 2006, 10:19 AM
#1
Thread Starter
Addicted Member
*RESOLVED* Property Get and Let in a class module
Hi,
Apologies up front if my use of terminology is incorrect but ...
It is my understanding that when you declare properties in a class module you use Property Get (to read values) and Property Let (to write values) if the property is a standard variable type (boolean, integer etc).
However, if the class member is an object (e.g. another class) you just use Get to both read and write.
My question is how do you know whether the object class member is being accessed for read or write from within the Property Get code?
Any help, as always, greatly appreciated.
Last edited by A.P.Gumby; Sep 14th, 2006 at 05:54 AM.
Reason: Resolved
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 11th, 2006, 10:28 AM
#2
Re: Property Get and Let in a class module
when working with objects you use Set:
VB Code:
' In Class
Private oFrm As Form
Public Property Get PassedForm() As Form
Set PassedForm = oFrm
End Property
Public Property Set PassedForm(frm As Form)
Set oFrm = frm
Debug.Print oFrm.Name
End Property
' In Form1
Private Sub Form_Load()
Dim cCls As New Class1
Set cCls.PassedForm = Me
Debug.Print cCls.PassedForm.Name
End Sub
is that what you were getting at?
-
Sep 11th, 2006, 10:30 AM
#3
Re: Property Get and Let in a class module
The Get propperty is for reading data from the class to the calling code
the let/set to write someting into a class variable.
Doing a read an write at once can happen because the argument is passed byreference.
If you want to write classes in VB6.0 I seriously recommend MZ-tools
it will save you a lot of time, it's free an no I do not get anything out of reccomending this add-inn.
Last edited by Dnereb; Sep 11th, 2006 at 10:33 AM.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Sep 11th, 2006, 10:38 AM
#4
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks for the reply,
That is exactly what I am getting at.
In my case the main class is a list of machine parameters which are of the type of a sub class.
So for example my main class looks like :
VB Code:
Private mParameter1 As New MachineParameter
Private mParameter2 As New MachineParameter
'Parameter 1
Public Property Get Parameter1() As MachineParameter
Set Parameter1 = mParameter1
End Property
'Parameter 2
Public Property Get Parameter2() As MachineParameter
Set Parameter2 = mParameter2
End Property
And the MachineParameter class looks like :
VB Code:
Private mvDemand As Variant
Private mvActual As Variant
'Demand value.
Public Property Get vDemand() As Variant
vDemand = mvDemand
End Property
Public Property Let vDemand(vNewValue As Variant)
mvDemand = vNewValue
End Property
'Actual value.
Public Property Get vActual() As Variant
vActual = mvActual
End Property
Public Property Let vActual(vNewValue As Variant)
mvActual = vNewValue
End Property
What I need to know is how the Property Get in the main class is being acessed (i.e. is it going to access the property Get or Let in the sub class).
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 11th, 2006, 10:44 AM
#5
Re: Property Get and Let in a class module
 Originally Posted by A.P.Gumby
What I need to know is how the Property Get in the main class is being acessed (i.e. is it going to access the property Get or Let in the sub class).
i don't really understand what you mean when you use Set whatever = whatever it's just passing the object reference - the object you're passing won't be accessed.
-
Sep 11th, 2006, 10:55 AM
#6
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks for sticking with me on this one.
This is an ActiveX server project.
When the parameters in the main class are being accessed by clients, the clients read the parameters using.
MyLocalRecordOfParameter1 = Parameter1.vActual
After executing any code within Public Property Get Parameter1, this will eventually cause any code within Public Property Get vActual to be executed.
The clients can write to parameters by using
Parameter1.vDemand = TheValueIWantItToBe
After executing any code within Public Property Get Parameter1, this will eventually cause any code within Public Property Let vActual to be executed.
In both cases the Public Property Get Parameter1 code is executed but depending on how the parameter is accessed, the Get or Let in the sub class ill be executed. I need to know how the Public Property Get Parameter1 is being accessed before I get to the sub class.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 11th, 2006, 11:16 AM
#7
Re: Property Get and Let in a class module
I don't think what you want to do is possible with your current structure. As it is now everything is Public, so there is no way to intercept the calls. In fact, it is possible to completely bypass the Property Get in the Main class.
VB Code:
'in calling application
Dim x as MachineParameter
Set x = MainClass.Parameter1
'the following line will not even use the main class
x.vDemand = 12345
The Parameter objects would need to be private in the Main Class. Create methods to get/let Parameter values, you would then know exactly which one properties are being referenced.
VB Code:
Private mParameter1 As New MachineParameter
Private mParameter2 As New MachineParameter
'Parameter 1
'Public Property Get Parameter1() As MachineParameter
' Set Parameter1 = mParameter1
'End Property
Public Property Get Parameter1Value() as Variant
Parameter1Value = mParameter1.Value
End Property
Public Property Let Parameter1Value(Byval NewValue as Variant)
mParameter1.Value = NewValue
End Property
-
Sep 11th, 2006, 11:27 AM
#8
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks for the reply,
I will give that a try.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 12th, 2006, 03:44 AM
#9
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Still struggling with this.
Each member of the main class has two properties .vDemand and .vActual There may be a need to add others in the future hence the use of the MachineParameter class module.
What I need to do is intercept when a parameter is being written to.
I was holding off adding this additional layer of complexity but ...
I need to do this so that I can raise an event to the connected machine to tell it that a connected client has changed one of its parameters.
Just putting the event raising code in the Property Let of the MachineParameter class does not always work properly since this code is only executed after the End Property of the Property Get in the main class and due to other events coming into the server, I cannot guarrantee that this is always the next piece of code to be executed.
I appreciate your help brucevde ... You say that you don't think that what I am trying to do is possible with my current structure. Is that a definite?
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 12th, 2006, 03:57 AM
#10
PowerPoster
Re: Property Get and Let in a class module
ill take a go at this ..
VB Code:
Private mParameter As New MachineParameter
'Parameter 1
Public Property Get Parameter1() 'As MachineParameter
Set Parameter1 = mParameter.vDemand
End Property
'Parameter 2
Public Property Get Parameter2() 'As MachineParameter
Set Parameter2 = mParameter.vActual
End Property
VB Code:
Private mvDemand As Variant
Private mvActual As Variant
'Demand value.
Public Property Get vDemand() 'As Variant
vDemand = mvDemand
End Property
Public Property Let vDemand(vNewValue As Variant)
mvDemand = vNewValue
End Property
'Actual value.
Public Property Get vActual() 'As Variant
vActual = mvActual
End Property
Public Property Let vActual(vNewValue As Variant)
mvActual = vNewValue
End Property
-
Sep 12th, 2006, 04:10 AM
#11
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks for the reply rory.
Each Parameter (Parameter1, Parameter2 etc) has the .vDemand and .vActual properties. which can be read or written to.
I need to detect a write at the main class level.
i.e.
VB Code:
Private mParameter1 As New MachineParameter
Private mParameter2 As New MachineParameter
'Parameter 1
Public Property Get Parameter1() As MachineParameter
Set Parameter1 = mParameter1
'Was the parameter written to or read from?
End Property
'Parameter 2
Public Property Get Parameter2() As MachineParameter
Set Parameter2 = mParameter2
'Was the parameter written to or read from?
End Property
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 12th, 2006, 05:05 AM
#12
Re: Property Get and Let in a class module
how about raising an event in the sub-class then?
Main Class:
VB Code:
Private WithEvents mParameter1 As MachineParameter
Private WithEvents mParameter2 As MachineParameter
'Parameter 1
Public Property Get Parameter1() As MachineParameter
Set Parameter1 = mParameter1
End Property
'Parameter 2
Public Property Get Parameter2() As MachineParameter
Set Parameter2 = mParameter2
End Property
Private Sub mParameter1_Accessed(eCall As vbCallType)
Debug.Print eCall
End Sub
Private Sub mParameter2_Accessed(ByVal eCall As vbCallType)
Debug.Print eCall
End Sub
MachineParameter Class:
VB Code:
Public Event Accessed(eCall As vbCallType)
Private mvDemand As Variant
Private mvActual As Variant
' Raise the Event before:
Public Property Get vDemand() As Variant
RaiseEvent Accessed(vbGet)
vDemand = mvDemand
End Property
Public Property Let vDemand(vNewValue As Variant)
RaiseEvent Accessed(vbLet)
mvDemand = vNewValue
End Property
' Raise the Event After:
Public Property Get vActual() As Variant
vActual = mvActual
RaiseEvent Accessed(vbGet)
End Property
Public Property Let vActual(vNewValue As Variant)
mvActual = vNewValue
RaiseEvent Accessed(vbLet)
End Property
and obviously you can modifiy the information returned by changing the parameters (mine was just a demo)
-
Sep 12th, 2006, 05:20 AM
#13
Re: Property Get and Let in a class module
In short if you write your class propper Get only reads en Let/Set only Writes
(If you ommit the Byval argument and alter the values of the argument itself
you can change the value of the variable in the calling code. I'v constructed a litle example to test reading and writing....with some remarks of course....
Your MachineParameter class uses variants and if these aren't absolutly neccesary (i.o.w. storing a decimal) avoid them and use numeric, text or objects
if you need to use several types consider writing diffrent classes for text,numbers and or objects
Code:
Private mvDemand As Variant
Private mvActual As Variant
Event Reading(PropertyName as String)
Event Writing (Propertyame As String)
'Demand value.
Public Property Get vDemand() 'As Variant
'this is reading only
RaiseEvent Reading("vDemand")
vDemand = mvDemand
End Property
Public Property Let vDemand(Byval vNewValue As Variant)
'This is writing only
RaiseEvent Writing("vDemand")
mvDemand = vNewValue
End Property
'Actual value.
Public Property Get vActual() 'As Variant
'this is reading only
RaiseEvent Reading("vActual")
vActual = mvActual
End Property
Public Property Let vActual(ByVal vNewValue As Variant)
'this is reading only
RaiseEvent Writing("vActual")
mvActual = vNewValue
End Property
In your machin class you are auto instancing.... don't it can cause memory leakage and unsuspected value's (Dim X as New Xobject)
Code:
Option Explicit
Private WithEvents mParameter1 As MachineParameter
Private WithEvents mParameter2 As MachineParameter
Private Sub Class_Initialize()
Set mParameter1 = New MachineParameter
Set mParameter2 = New MachineParameter
'maybe you want to set default values here?
mParameter1.vActual = -1
mParameter1.vDemand = -1
mParameter2.vActual = -1
mParameter2.vDemand = -1
End Sub
'Parameter 1
Public Property Get Parameter1() As MachineParameter
Set Parameter1 = mParameter1
'Was the parameter written to or read from?
End Property
'Parameter 2
Public Property Get Parameter2() As MachineParameter
Set Parameter2 = mParameter2
'Was the parameter written to or read from?
End Property
Private Sub Class_Terminate()
'ensure memory is relaesed propperly
Set mParameter1 = Nothing
Set mParameter2 = Nothing
End Sub
Private Sub mParameter1_Reading(PropertyName As String)
'Do something parameter1 is read like
msgbox "writing to : " & PropertyName
End Sub
Private Sub mParameter1_Writing(Propertyame As String)
'Do something parameter1 is written to like
msgbox "writing to : " & PropertyName
End Sub
Private Sub mParameter2_Reading(PropertyName As String)
'Do something parameter2 is read like
msgbox "writing to : " & PropertyName
End Sub
Private Sub mParameter2_Writing(Propertyame As String)
'Do something parameter2 is written to like
msgbox "writing to : " & PropertyName
End Sub
The problem of changing argument values happens if you write a property like this:
Code:
Public Property Let vDemand(Byref vNewValue As Variant) '(byref is default for arguments in VB6.0)
if isnumeric(vNewValue) and > 0 then vNewValue = cDbl(vNewvalue/2) 'this will alter the value given for vNewValue if it was stored in a variable
mvDemand = vNewValue
End Property
I hope this will help
Grmpppffffff Bushmobile types fast!
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Sep 12th, 2006, 06:16 AM
#14
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks to both of you for continuing to help me with this one.
In order to keep my initial question simple and not cloud it with stuff that I did not think was relevant, I confined my initial question to simply asking if it was possible to detect why the Property Get was called in the main class.
I only need to raise the event if the .vDemand is changed and I am actually raising this event in the sub class.
In order to do this I set a variable which identifies the parameter being accessed in the main class.
VB Code:
Private mParameter1 As New MachineParameter
Private mParameter2 As New MachineParameter
'Parameter 1
Public Property Get Parameter1() As MachineParameter
iChangingParameterID = eParameterID.eParameter1
Set Parameter1 = mParameter1
End Property
'Parameter 2
Public Property Get Parameter2() As MachineParameter
iChangingParameterID = eParameterID.eParameter2
Set Parameter2 = mParameter2
End Property
And then use this parameter ID to raise the event in the .vDemand Property Let in the sub class.
VB Code:
Private mvDemand As Variant
Private mvActual As Variant
'Demand value.
Public Property Get vDemand() As Variant
vDemand = mvDemand
End Property
Public Property Let vDemand(vNewValue As Variant)
DoNotify neDemandChanged, byChangingMachineID, iChangingParameterID
mvDemand = vNewValue
End Property
'Actual value.
Public Property Get vActual() As Variant
vActual = mvActual
End Property
Public Property Let vActual(vNewValue As Variant)
mvActual = vNewValue
End Property
The DoNotify bit raises the event and works OK.
The problem is that I find that the sub class Property Let code is not always called immediately after the main class Property Get code.
There are a number of clients connected to the server. If another client has, say, initiated a read of Parameter2 (i.e. it wants to know the .vActual) while the Property Get Parameter1 code is being executed for a write of Parameter1 (i.e. the vDemand is being written) by another client, the iChangingParameterID has changed before the sub class code for the Property Let is executed.
Dnereb ...
I am using variants because the data can be anything
I also take your point about auto instancing. I don't think that that will be causing the problem that I am seeing but I will tidy it up anyway.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 12th, 2006, 07:11 AM
#15
Re: Property Get and Let in a class module
Is it a solution to block new actions while you are waiting for a Let after a get?
You could use a boolean InUse to determine if your class is open for changes or needs to reject new alterations or readouts. But I suspect I'm thinking way to simplistic.
BTW I didn't think the removal of autoinstancing would solve this problem, just possible problems in the future.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Sep 12th, 2006, 07:51 AM
#16
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks for coming back on this Dnereb,
The temporary solution that I have implemented is to do just what you suggest. (i.e. block changes while updates are in progress).
This at least stops the wrong parameter change event from being fired but does slow things down a bit.
I really would like to find a proper solution to this problem though.
If only there were some way of forcing the sub class code to execute immediately after the main class Property Get has executed
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 12th, 2006, 10:46 AM
#17
Re: Property Get and Let in a class module
the iChangingParameterID has changed before the sub class code for the Property Let is executed.
If the iChangingParameterID variable is declared Public in a module all clients are reading/updating the same variable. Make it a Friend Property of the Parameter class.
VB Code:
Private mvDemand As Variant
Private mvActual As Variant
Private mlngChangingParameterId as Long
Friend Property Get ChangingParameterId() as Long
ChangingParameterId = mlngChangingParameterId
End Property
Friend Property Let ChangingParameterId(ByVal ParameterId as Long )
mlngChangingParameterId = ParameterId
End Property
'Demand value.
Public Property Get vDemand() As Variant
vDemand = mvDemand
End Property
Public Property Let vDemand(vNewValue As Variant)
DoNotify neDemandChanged, byChangingMachineID, mlngChangingParameterId
mvDemand = vNewValue
End Property
'main class
Public Property Get Parameter1() As MachineParameter
mParameter1.ChangingParameterId = eParameterID.eParameter1
Set Parameter1 = mParameter1
End Property
-
Sep 13th, 2006, 02:48 AM
#18
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks for the suggestion brucevde,
The ChangingParameterID is indeed used by other classes in the project. This is just one small bit of the application. However, the sub class containing the .vActual and .vDemand properties is also used by other parameter classes.
I think that the fundamental mistake that I have made is to assume that the sub class code would be called from within the main class Property Get i.e. Set Parameter1 = mParameter1.
It appears that the sub class code is not executed until after the End Property.
Unless someone can come up with a miracle, it looks like I am going to have to restructure this big time
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 13th, 2006, 05:29 AM
#19
Re: Property Get and Let in a class module
Just out of curiosity what happens if you write it like this:
'Parameter 1
Public Property Get Parameter1() As MachineParameter
iChangingParameterID = eParameterID.eParameter1
Set Parameter1.vDemand = mParameter1.vDemand
Set Parameter1.vActual = mParameter1.vActual
End Property
Thus calling the actual propperty's instead of setting a reference to the object
Hope it helps...
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Sep 13th, 2006, 06:28 AM
#20
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks for the thought.
Tried that yesterday.
Gives RTE 91 "Object variable with block variable not set"
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 13th, 2006, 10:34 AM
#21
Re: Property Get and Let in a class module
Unless someone can come up with a miracle, it looks like I am going to have to restructure this big time
Using Friend and adding properties to the Parameter class to identify the Main class seems like it could work...
-
Sep 13th, 2006, 11:13 AM
#22
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Thanks brucevde I will give that a go.
I didn't think that it would work because there are other classes (other than the one that I have been calling the main class for the purposes of explaining the problem) that use the machine parameter sub class.
Too late to try today ... It's supermarket night
Will give it a go tomorrow
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 13th, 2006, 03:54 PM
#23
Re: Property Get and Let in a class module
 Originally Posted by A.P.Gumby
Thanks for the thought.
Tried that yesterday.
Gives RTE 91 "Object variable with block variable not set"
that seems to be a initialisation issue did you set the mParameter1
Set parmeter1 = New machine parameter
before you called it (ergo did you do that in the initiale event of the main class?)
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Sep 14th, 2006, 02:46 AM
#24
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
No, I am still auto instancing.
I will try that.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 14th, 2006, 03:11 AM
#25
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
Tried that, still getting RTE 91.
I'm going to give brucevdes Friend property idea another go.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
Sep 14th, 2006, 05:52 AM
#26
Thread Starter
Addicted Member
Re: Property Get and Let in a class module
brucevdes suggestion of adding a friend property to the parameter class worked.
A big thanks to you and all others who helped me on this one.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|