More On Class

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.
 
G

Greg Maxey

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.
 
G

Greg Maxey

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??
 
G

Greg Maxey

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
 
G

Greg Maxey

Jonathan,
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

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.
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Jonathan,


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.

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

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

"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.
 
G

Greg Maxey

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.
 
J

Jonathan West

Greg Maxey said:
"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!
 
G

Greg Maxey

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.
 
J

Jonathan West

Greg Maxey said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top