Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

More On Class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 07 Dec 2006 17:46 GMT
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.
Jonathan West - 07 Dec 2006 22:29 GMT
> Last week Jonathan, Karl, and Jean-Guy where schooling me on Class
> modules.  I have read the articles that Jonathan posted and wanted to
[quoted text clipped - 57 lines]
>
> 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.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Greg Maxey - 07 Dec 2006 23:23 GMT
Jonathan,

Thanks for your comments.  It was done pretty much to learn how to write a
class.  I am interested in pursuing your other suggestions with this
example.  I don't really know where to beginning, but I will claw at it for
awhile and come back for help if I need it.

Thanks again.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> Last week Jonathan, Karl, and Jean-Guy where schooling me on Class
>> modules.  I have read the articles that Jonathan posted and wanted to
[quoted text clipped - 84 lines]
> You instantiate the class - create an instance of it. You call
> methods, and read or write properties.
Greg Maxey - 08 Dec 2006 01:31 GMT
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??

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> Last week Jonathan, Karl, and Jean-Guy where schooling me on Class
>> modules.  I have read the articles that Jonathan posted and wanted to
[quoted text clipped - 84 lines]
> You instantiate the class - create an instance of it. You call
> methods, and read or write properties.
Jonathan West - 08 Dec 2006 02:03 GMT
> Jonathan,
>
[quoted text clipped - 6 lines]
> I had no trouble using LET and GET statements, but keep get a compile
> error any time I try to use the SET statement.

You only use Property Set if your property is an object rather then a value.
Otherwise you just use Property Let.

You would only ever have both Property Let and Property Set for the same
property if you want the ability to assign both an object and its default
property to the same property name. Forget about this - the number of times
you would need this is vanishingly small.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Greg Maxey - 08 Dec 2006 03:42 GMT
Jonathan,

This is still a bit fussy.  But if I understand, then I could set the
variable values in the Class initialize procedure like this:

Option Explicit
Private mUBound As Long
Private mLBound As Long

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 Let lngLBound(pVal As Long)
 mLBound = pVal
End Property

Private Sub Class_Initialize()
Dim pObject As Object
Me.lngLBound = 5
Me.lngUBound = 10
End Sub

I guess that part I remain puzzled about is: How do you create a variable
that has a value say "5" that is then read only?  I mean that couldn't be
changed to 10 or 15, or 100 or whatever with a statement like:

Sub TestClass()
Dim myTest As clsUnderstandingSet
Set myTest = New clsUnderstandingSet
MsgBox myTest.lngLBound & " and " & myTest.lngUBound
myTest.lngLBound = 100
End Sub

Thanks

Me.lngLBount = 10

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> Jonathan,
>>
[quoted text clipped - 14 lines]
> its default property to the same property name. Forget about this -
> the number of times you would need this is vanishingly small.
Jonathan West - 08 Dec 2006 09:03 GMT
> Jonathan,
>
[quoted text clipped - 37 lines]
> myTest.lngLBound = 100
> End Sub

Within the class you can change mUBound and mLBound directly, from any
routine within the class, Therefore you could do the following

Private Sub Class_Initialize()
Dim pObject As Object
mLBound = 5
mUBound = 10
End Sub

This then gets reflected in the values returned by the properties. The
properties can then be read-only if you wish.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Greg Maxey - 08 Dec 2006 11:15 GMT
Jonathan,

> Within the class you can change mUBound and mLBound directly, from any
> routine within the class, Therefore you could do the following
[quoted text clipped - 4 lines]
> mUBound = 10
> End Sub

There lies my question.  Let's say I wish to make the variable values "read
only" after I have established them as indicated above.

How do I do that?  Here an now I can run initialization code as you show and
still the following will "write" a new value to the variable:

Sub TestClass()
Dim myTest As clsUnderstandingSet
Set myTest = New clsUnderstandingSet
MsgBox myTest.lngLBound & " and " & myTest.lngUBound
myTest.lngLBound = 100
MsgBox myTest.lngLBound 'This returns 100
End Sub

If the variable were read only I would expect some kind of error.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> Jonathan,
>>
[quoted text clipped - 48 lines]
> This then gets reflected in the values returned by the properties. The
> properties can then be read-only if you wish.
Jonathan West - 08 Dec 2006 12:49 GMT
> Jonathan,
>
[quoted text clipped - 9 lines]
> There lies my question.  Let's say I wish to make the variable values
> "read only" after I have established them as indicated above.

Read-only properties are ones that don't have a Property Let or Property Set
routine - they only have a Property Get routine.

That way, you can set the underlying variables within other routines inside
the class module, but not give the calling routine the opportunity to change
the property values directly.

Always distinguish between what you can do using code within the class (i.e.
just about anything), and what interfaces you choose to define to the
outside (which can be limited in whatever way you want).

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Jean-Guy Marcil - 08 Dec 2006 12:49 GMT
Greg Maxey was telling us:
Greg Maxey nous racontait que :

> Jonathan,
>
[quoted text clipped - 22 lines]
>
> If the variable were read only I would expect some kind of error.

The Let statements in the Class modules are the one that make your
properties both "writable". To make them read only, use only the Get
Statements:
(That is to say that Let allow the property to be written, and Get allows it
to be read).

Option Explicit

Property Get lngUBound() As Long
 lngUBound = 10
End Property

Property Get lngLBound() As Long
 lngLBound = 5
End Property

Then, if you try to run your TestClass Sub, you will get an error at compile
time.

Hre is a nice page to read up on the topic:
   http://www.cpearson.com/excel/ClassModules.htm

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg Maxey - 08 Dec 2006 13:01 GMT
JGM,

The light bulb flashed just moments ago and I understand the process
now.  Thanks for your post and I am off to read your article shortly.

> Greg Maxey was telling us:
> Greg Maxey nous racontait que :
[quoted text clipped - 55 lines]
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
Greg Maxey - 08 Dec 2006 12:57 GMT
"Plink" and the brigth light floods in removing the fog of confussion.
I misread your reply Jonathan.  I see now that you meant that the
module level variables could be set directly and I that I don't use a
LET statement.

Thanks.

> > Jonathan,
> >
[quoted text clipped - 55 lines]
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
Jonathan West - 09 Dec 2006 17:23 GMT
> "Plink" and the brigth light floods in removing the fog of confussion.
> I misread your reply Jonathan.  I see now that you meant that the
> module level variables could be set directly and I that I don't use a
> LET statement.

Exactly so!

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Greg Maxey - 09 Dec 2006 17:34 GMT
Jonathan,

While your article links are informative, they are more easily understood
after reading the one by Chip Pearson that Jean-Guy provided the link to in
his post above.  You might considers adding that one to your list of
tuitorials for instructing beginners like me.

Thanks again.  I may be back on this topic or a new one as I continue to
attempt to build on my InputBox class. I realize it may never have much
functional merit but it allows me to practice and develop skills.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> "Plink" and the brigth light floods in removing the fog of
>> confussion. I misread your reply Jonathan.  I see now that you meant
>> that the module level variables could be set directly and I that I
>> don't use a LET statement.
>
> Exactly so!
Jonathan West - 09 Dec 2006 18:07 GMT
> Jonathan,
>
> While your article links are informative, they are more easily understood
> after reading the one by Chip Pearson that Jean-Guy provided the link to
> in his post above.  You might considers adding that one to your list of
> tuitorials for instructing beginners like me.

I agree. I was unaware of it when I gave you the other links.

> Thanks again.  I may be back on this topic or a new one as I continue to
> attempt to build on my InputBox class. I realize it may never have much
> functional merit but it allows me to practice and develop skills.

Having the chance to develop skills at leaisure on a project that doesn't
matter too much is a very good idea.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.