Object oriented programming guidelines stipulate that objects --
including forms -- should maintain their own internal values. With
this in mind, you'll need a way to pass values to loaded forms without
using universally global variables. Fortunately, Visual Basic provides
just such a way with its property Let() and Get() procedures.
For example, suppose you have a button Click() event on Form1 that
loads Form2. Form2 displays a value collected in Form1. In the past,
you may have created a public variable with an application scope, and
then read this variable in Form2 to obtain the proper value. Using
this technique, however, soon results in hard-to-manage, spaghetti code.
As a more elegant alternative, you can add a property to Form2 that
Form1 can then set.
For instance, in Form2 you would add the following property procedures
along with the variable declaration:
Private myVar as Variant
Public Property Get PassVar() As Variant
PassVar = myVar
End Property
Public Property Let PassVar(ByVal vNewValue As Variant)
If IsNumeric(vNewValue) Then myVar = vNewValue
End Property
For these procedures, you can change the property type, but it must
match in both the Get and Let procedures. Also notice that we included
some validation code to ensure that the passed value is a number.
Now, to read or assign a value to this property, use the following
code on Form1 Private Sub Command1_Click()
With Form2
.PassVar = 25
.Show
Debug.Print .PassVar
End With
End Sub
Passing variable values to a form
By ElementK Journals, published on 14 Jul 2001
| Filed in
You might also like...
VB 6 forum discussion
-
CorelDRAW VBA: cdrTraceLineDrawing FAILS, producing single linear path instead of Centerline trace?
by dancemanj (0 replies)
-
client/server application using activex
by beautifulheart (0 replies)
-
System Error &H8007007E. The specifed module could not be found.
by swiftsafe (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
-
Send SMS with SMPP
by mmahmoud (0 replies)
VB 6 podcasts
-
Stack Overflow Podcast: Podcast #45 – Keeping it Sharp
Published 7 years ago, running time 0h54m
Our guest this week is Eric Lippert – language architect extraordinaire and famous for all his work at Microsoft in developing their languages Eric joined Microsoft right out of college and was originally working on VB It’s time for everyone’s favorite game: Name the Worst Feature of that Microso.
Comments