G
Greg Maxey
Last week Jonathan, Karl, and Jean-Guy where schooling me on Class
modules. I have read the articles that Jonathan posted and wanted to
try to put theory to practice.
Submitted for comments by those or any other master.
With the input of a few others, I have put together a routine that
ensures the input to a VBA input box is a numeric whole number greater
than five. It is several lines of code and I thought perhaps that it
could be a "Class" and if I need a whole number greater than 5 several
times in a procedure that I could call the class.
Here is my testing procedure:
Sub TestOfClass()
Dim myNum As myNumClass
Dim i As Long
Set myNum = New myNumClass
For i = 1 To myNum.lngNumber
Debug.Print "Test1"
Next i
Set myNum = New myNumClass
For i = 1 To myNum.lngNumber
Debug.Print "Test2"
Next i
End Sub
I created a Class named myNumClass and entered this code:
Private mLngNumber As Long
Private Sub Class_Initialize()
Dim strInput As String
Dim bInValid As Boolean
Do
bInValid = False
strInput = InputBox("Enter a whole number greater than 5", _
"Number Input", "0")
If strInput = vbNullString Then Exit Sub
On Error Resume Next
If strInput <> CLng(strInput) Or CLng(strInput) <= 5 Then
If Err.Number = 13 Then
bInValid = True
On Error GoTo 0
Else
bInValid = True
End If
End If
If bInValid Then
MsgBox "You must enter a valid whole number that is > 5."
End If
Loop While bInValid
mLngNumber = strInput
End Sub
Property Get lngNumber() As Long
lngNumber = mLngNumber
End Property
When I run TestofClass, I see the results that I expect. My questions
are:
1. Is this a sensible use of a class?
2. Is it constructed in the conventional manner?
3. Do I have my terms correct? I mean is it correct to say "Call the
class?"
Thanks.
modules. I have read the articles that Jonathan posted and wanted to
try to put theory to practice.
Submitted for comments by those or any other master.
With the input of a few others, I have put together a routine that
ensures the input to a VBA input box is a numeric whole number greater
than five. It is several lines of code and I thought perhaps that it
could be a "Class" and if I need a whole number greater than 5 several
times in a procedure that I could call the class.
Here is my testing procedure:
Sub TestOfClass()
Dim myNum As myNumClass
Dim i As Long
Set myNum = New myNumClass
For i = 1 To myNum.lngNumber
Debug.Print "Test1"
Next i
Set myNum = New myNumClass
For i = 1 To myNum.lngNumber
Debug.Print "Test2"
Next i
End Sub
I created a Class named myNumClass and entered this code:
Private mLngNumber As Long
Private Sub Class_Initialize()
Dim strInput As String
Dim bInValid As Boolean
Do
bInValid = False
strInput = InputBox("Enter a whole number greater than 5", _
"Number Input", "0")
If strInput = vbNullString Then Exit Sub
On Error Resume Next
If strInput <> CLng(strInput) Or CLng(strInput) <= 5 Then
If Err.Number = 13 Then
bInValid = True
On Error GoTo 0
Else
bInValid = True
End If
End If
If bInValid Then
MsgBox "You must enter a valid whole number that is > 5."
End If
Loop While bInValid
mLngNumber = strInput
End Sub
Property Get lngNumber() As Long
lngNumber = mLngNumber
End Property
When I run TestofClass, I see the results that I expect. My questions
are:
1. Is this a sensible use of a class?
2. Is it constructed in the conventional manner?
3. Do I have my terms correct? I mean is it correct to say "Call the
class?"
Thanks.