Using properties

Properties are good for manipulating protected variables, that is, Private members of a user-defined class to which the application has no direct access.

For more information see "User-defined Data Types and Classes."

Example 1

In the following example, the sub KeepGoing uses the property theCube# to manipulate three variables (anInt%, aDouble#, and bigNum#) that are not referred to directly by the application.

%Include "LSCONST.LSS"

Dim anInt As Integer
Dim aDouble As Double
Dim bigNum As Double

Property Set theCube As Double
   anInt% = theCube#
End Property
Property Get theCube As Double
   aDouble# = anInt% ^ 3
   If aDouble# > bigNum# Then
       bigNum# = aDouble#
  End If
  theCube# = anInt%
End Property

Sub KeepGoing
  Dim goAgain As Boolean
  Dim msg As String
  Dim msgSet As Integer
  Dim more As Integer
  goAgain = TRUE
  msg$ = "Want to go again?"
  msgSet% = MB_YESNO + MB_ICONQUESTION
 ' Prompt the user to enter a number; assign that number to 
 ' the property theCube# (by executing Property Set theCube#); 
 ' calculate the cube of that number (by executing 
 ' Property Get theCube#), assign it to the variable aDouble#,
 ' and compare it to the current value of bigNum#, resetting
 ' the latter if aDouble# is greater. Prompt the user to
 ' repeat the process or quit.
  While goAgain = True
    ' Execute Property Set theCube# by assigning it
    ' a value. This assigns a value to anInt%.
    theCube# = CInt(InputBox$("Enter an integer:"))
    ' Execute Property Get theCube# by including theCube#
    ' in a Print statement. This assigns a value to aDouble#,
    ' may assign a value to bigNum#, and returns the current
    ' value of anInt%.
    Print theCube# & " cubed = " & aDouble# & "."
    Print bigNum# & " is the biggest cube so far."
    ' See if the user would like to do all this again or quit.
    more% = MessageBox(msg$, msgSet%)
      If more% = IDNO Then
         goAgain = FALSE
      End If
  Wend
  Print "All Done."
End Sub
Call KeepGoing

' Output: The user types 3 and selects Yes, then
' 4 and selects Yes, then 2 and selects No.
' 3 cubed = 27.
' 27 is the biggest cube so far.
' 4 cubed = 64.
' 64 is the biggest cube so far.
' 2 cubed = 8.
' 64 is the biggest cube so far.
' All Done.

Example 2

You can perform the same operations using a sub and a function instead of a property.

%Include "LSCONST.LSS"

Dim anInt As Integer
Dim aDouble As Double
Dim bigNum As Double

Sub SetTheCube
   anInt% = CInt(InputBox$("Enter an integer:"))
End Sub

Function GetTheCube(anInt As Integer) As Double
   aDouble# = anInt% ^ 3
   If aDouble# > bigNum# Then
       bigNum# = aDouble#
  End If
  GetTheCube# = anInt%
End Function

Sub KeepGoing
  Dim goAgain As Boolean
  Dim msg As String
  Dim msgSet As Integer
  Dim more As Integer
  goAgain = TRUE
  msg$ = "Want to go again?"
  msgSet% = MB_YESNO + MB_ICONQUESTION
 
  While goAgain = True  
    Call SetTheCube
    Print GetTheCube#(anInt%) & " cubed = " & aDouble# & "."
    Print bigNum# & " is the biggest cube so far."
    ' See if the user would like to do all this again or quit.
    more% = MessageBox(msg$, msgSet%)
      If more% = IDNO Then
         goAgain = FALSE
      End If
  Wend
  Print "All Done."
End Sub

Call KeepGoing

' Output: The user types 3 and selects Yes, then
'         4 and selects Yes, then 2 and selects No.
' 3 cubed = 27.
' 27 is the biggest cube so far.
' 4 cubed = 64.
' 64 is the biggest cube so far.
' 2 cubed = 8.
' 64 is the biggest cube so far.
' All Done.