プロパティは、保護された変数 (アプリケーションが直接アクセスできないユーザー定義クラスの Private メンバ) を扱うのに適しています。
詳しくは、「ユーザー定義のデータ型とクラス」を参照してください。
次の例では、サブルーチン KeepGoing は、プロパティ theCube# を使用して、アプリケーションからは直接参照されない 3 つの変数 (anInt%、aDouble#、bigNum#) を操作します。
%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.
プロパティの代わりにサブルーチンと関数を使用して、同じ演算を実行できます。
%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.