Greg
Thu Dec 07 19:31:40 CST 2006
Jonathan,
Sometimes I just hate this stuff gggrrrr.
After struggling for nearly two hours, I simply can't get the grasp of
Propert Set.
I wanted to try to incorporate a LBound and UBound property of the class. I
had no trouble using LET and GET statements, but keep get a compile error
any time I try to use the SET statement.
I created a new class modules "clsUnderstandingSet" with this code:
Option Explicit
Private mUBound As Long
Private mLBound As Long
Private mModuleObject As Object
Property Get lngUBound() As Long
lngUBound = mUBound
End Property
Property Let lngUBound(pVal As Long)
mUBound = pVal
End Property
Property Get lngLBound() As Long
lngLBound = mLBound
End Property
'Property Set lngLBound(pVal As Long)
' Set mLBound = pVal
'End Property
Private Sub Class_Initialize()
Dim pVal As Long
Dim pObject As Object
pVal = 6
'Set Me.lngLBound = pVal
Set Me.MyObject = New Word.Application
Set pObject = Me.MyObject
End Sub
Public Property Get MyObject() As Object
Set MyObject = mModuleObject
End Property
Public Property Set MyObject(NewValue As Object)
Set mModuleObject = NewValue
End Property
and a calling macro with this code:
Sub TestClass()
Dim myTest As clsUnderstandingSet
Set myTest = New clsUnderstandingSet
myTest.lngUBound = 10
MsgBox myTest.lngLBound & " and " & myTest.lngUBound
End Sub
The stetted out statements in the Class module are what is giving me fits.
You can run the code as is and see that the UBound property is "LET" to 10,
and I copied a working SET statement from Google to set the MyOject
variable, but I can't get SET to work to set the LBound variable.
What am I doing wrong??
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jonathan West wrote:
> "Greg Maxey" <gmaxey@mvps.org> wrote in message
> news:1165513614.334792.165000@j44g2000cwa.googlegroups.com...
>> 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?
>
> For a single property, probably not, except as a means of learning
> how to write a class.
>
> For a class to be worthwhile, you would probably want one or more of
> the following to be true
>
> - two or more related properties
> - properties that have some effect on each other
> - some kind of internal state (e.g. the outputs are dependent on
> internal variables as well as on inputs
> - events raised by changes within the class
>
> For instance, you could extend the class by having UpperBound and
> LowerBound properties defining the range of numbers which the user
> can enter into the inputbox.
>
>
>> 2. Is it constructed in the conventional manner?
>
> I would have a Show method which displays the InputBox and returns the
> number. That way you can use the class repeatedly without having to
> set it to Nothing and then re-assign it.
>
>
>> 3. Do I have my terms correct? I mean is it correct to say "Call
>> the class?"
>
> You instantiate the class - create an instance of it. You call
> methods, and read or write properties.