PDA

View Full Version : Solved: Passing variables through properties



jtrowbridge
06-16-2008, 11:57 AM
I decided to take a swing at using properties and I'm ver quickly finding myself running into trouble. I've worked out a little code that is designed (more like intended) to have subroutine 1 gather and store data and then have subroutine 2 read and display the data with a message box.

It seems to be writing the data well enough. The trouble is that when the msgbox1 sub is called the data in the public properties seems to get zeroed out. I'm sure I'm missing something simple, does anyone know?

Also, is there a way to set up a property like an array? In a perfect world I'd like to declare something like this:


Public Property Let PassVar1(ByVal vNewValue As Variant), PassVar2(ByVal iNewValue as Integer), etc...



Here's the code that I'm struggling with: :think:

---------------------------------------------------
Public CalcVar As Integer
Public PromptVar As Date
'''''''

Public Property Get CalcIDPassVar() As Integer
PassVar = CalcVar
End Property
'''''''
Public Property Let CalcIDPassVar(ByVal NewValue As Integer)
CalcVar = NewValue
End Property

'''''''

Public Property Get PromptPassVar() As Date
PassVar = PromptVar
End Property
'''''''
Public Property Let PromptPassVar(ByVal NewValue As Date)
PromptVar = NewValue
End Property

'''''''

Sub test()

CalcIDPassVar = 100
PromptPassVar = "05/30/2008"
Call msgbox1

End Sub


Sub msgbox1()
MsgBox (CalcIDPassVar)
MsgBox (PromptPassVar)
End Sub


---------------------------------------------

Thanks!

rory
06-16-2008, 04:23 PM
Is that code in a class where it belongs? I suspect not looking at your calling subs.
Also, there is no point having the variables public and having properties to read write them - defeats the purpose if you can write to the variables directly and bypass the property procedures.
Finally, returning a value from a property is just like a function - the return assignment must match the property name:
Public Property Get PromptPassVar() As Date
PromptPassVar = PromptVar
End Property

Bob Phillips
06-16-2008, 11:34 PM
You can have properties in a normal module, but there is very little point. The point is to abstract the object in a class, and manage how the properties of that object interface to the application. In your example you don't need Let/Get properties, a simple Public property suffices, because you are doing nothing other than setting and reading them, and you have them both as read/write properties. However, whilst that is the case, I always recommend having Let and Get properties in a class, makes changing later more easily achieved.

jtrowbridge
06-17-2008, 02:11 PM
I did some additional research and figured out how to properly use public variables. You two are definately right about that being a simpler approach.

thanks!

jtrowbridge
06-17-2008, 02:11 PM
I did some additional research and figured out how to properly use public variables. You two are definately right about that being a simpler approach.

thanks!