class module?

S

sybmathics

Hi,

At what occasion do you create a class module?
What can class modules do?

How do you fill a class module?

I know about modules and user-functions as addinn, how to create a
user-form.

I don't know anything about class-modules.

Can anyone explain?

greets,

Sybolt
 
G

Guest

I think we have been here once before... An explanation of classes is a
little beyond the scope of this forum. Classes are more the kind of thing you
need to take a programming course in Object Oriented Programming to get. In a
nut shell however... Classes are blueprints to create objects. Objects are
things (almost tangible) which use as a programmer can interact with. Objects
have properties and methods. Properties describe the object. Methods are
things an object can do.

Classes are not terribly applicable to Excel programming. They are used very
rarely (I only use them to handle events). It is great to understand objects
and classes and they will make a lot of things in the world of the Excel
object model more understandable but they are not for the beginner or the
faint of heart.
 
G

Guest

Classes let you create objects that behave like any of the "native" objects
you are using in VBA, that is they will have properties and methods, e.g.
with a properly coded class called MyObject I could do:

MyObject.Color = vbRed
MyObject.Print
etc...

- but this does take advanced knowledge and coding skills
 
K

keepITcool

in excel you only 'NEED' class modules
if you want to set object variables and need to trap their events.
as only class modules allow Dim WITHEVENTS

excel application level events (events in multiple workbooks)
outlook application level events (search complete)
generic code for multiple controls on userforms

you MUST fully understand scope and instantiation.

else you are on a frustrating learning curve and
getting on the train at the wrong station....


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Jim Thomlinson wrote :
 
H

Henry

Jim and others,

I consider myself a fairly competent amateur programmer.
I have an above average IQ. (not quite Mensa standard).
I understand most, if not all, the principles of programming.
I've written several programs in Excel (and some Word) VBA and even written
an assembly language program which I was selling.
(Ancient history. It was in Z80 code for the Spectrum. About 5K lines of
code.)
I've mainly taught myself to do most of this, with some help from books,
this group and others.
I attended a programming course at the local college, which I failed,
because nobody could explain to ME how OOP worked.
Others in the class seemed to get it. Maybe I'm too entrenched in *normal*
coding to take it on board?
I can break a problem down into the small steps necessary to program, but I
just can't seem to make the"leap of faith" into OOP.
Now, if someone out there could explain it to me, in words of one syllable,
preferably with simple examples, I might get it.

So, come on people, try explaining it to me in a way that doesn't go 6
inches above my head.
Or at least point me to somewhere where I can try to get to grips with it.

Henry
 
G

Guest

Think of a "Class" as a series of subroutines and functions that build a car
(kinda like a dll). The Functions return values for the characteristics (or
"adjectives") that describe the car (e.g. color, trim, interior). These are
called properties.

The subroutines cause the car to do certain things (Speed up, slow down,
stop). These are called methods.

Once you write the code, to slow down, for example:

Sub Slow_down(mph)
do until car.speed = mph
apply brakes 'Which may be another "method"
loop
end sub

You never have to write this code again. Instead, you can simply invoke the
"Slow_down" method of the car class and pass it the desired speed.

Had I created a "Property" named "Speed" for the car, I could have set the
"property" with setProperty to the lower speed:

Function speed(mph)
set property car.speed = mph
end function

Now I can create any number of cars (objects) with any number of
characteristics, and have them do any number of things, just by calling the
Dll (called "instantiating" or "creating an instance of") and passing in the
specific Function (setting the properties) or Subroutine (invoking the
methods) parameters rather than writing the code whatever number of times.

Clearly an "oversimplification" but, pretty much the basics of OOP. Program
the "object" car by setting its properties and invoking its methods, rather
than programming the computer to do these things


As one of us "old timers" surely you remember writing the BASIC code to
create a string of "-"s to form a line across the screen to create a display
box. Then counting pixels and spaces to fill the box with text. Well, now, we
only need to set the properties of the TextBox object and all that code
"happens" because somebody created a TextBox class of object..

Clearer?

BAC
 
H

Henry

BAC,

Thanks for that.
It hasn't parsed yet, but looks like it will after a few read throughs.
I'm very tired at the moment, maybe at the weekend when I'm more relaxed
it'll sink in.

Henry
 
S

Stephen Bullen

Does that make it any clearer?

D'Oh. A whole post about comparing collections of classes to 2D arrays,
and it didn't get through the CDO interface! Sorry Henry.

Regards

Stephen Bullen
Microsoft MVP - Excel
 
H

Henry

Think of a "Class" as a series of subroutines and functions that build a
car
(kinda like a dll). The Functions return values for the characteristics
(or
"adjectives") that describe the car (e.g. color, trim, interior). These
are
called properties.

The subroutines cause the car to do certain things (Speed up, slow down,
stop). These are called methods.

Once you write the code, to slow down, for example:

Sub Slow_down(mph)
do until car.speed = mph
apply brakes 'Which may be another "method"
loop
end sub
Re-usable code!
I understand that and have done it many times.
Especially when programming for the Spectrum, where you've only got 48K of
memory to play with.
Put the code in a sub and call it from wherever it's needed, passing any
desired data to it.
You never have to write this code again. Instead, you can simply invoke
the
"Slow_down" method of the car class and pass it the desired speed.

Had I created a "Property" named "Speed" for the car, I could have set the
"property" with setProperty to the lower speed:

Function speed(mph)
set property car.speed = mph
end function

Got that.
Now I can create any number of cars (objects) with any number of
characteristics, and have them do any number of things, just by calling
the
Dll (called "instantiating" or "creating an instance of") and passing in
the
specific Function (setting the properties) or Subroutine (invoking the
methods) parameters rather than writing the code whatever number of times.

Sort of got that. Still not quite sunk in. I'll ponder a little longer on
that.
Clearly an "oversimplification" but, pretty much the basics of OOP.
Program
the "object" car by setting its properties and invoking its methods,
rather
than programming the computer to do these things

Now that's the bit that I never got before.
Mainly because it goes against all my programming experience.
It's a little clearer now, but not fully there yet.
As one of us "old timers" surely you remember writing the BASIC code to
create a string of "-"s to form a line across the screen to create a
display
box. Then counting pixels and spaces to fill the box with text. Well, now,
we
only need to set the properties of the TextBox object and all that code
"happens" because somebody created a TextBox class of object..

I've done that in Assembly Language.
Write a sub that accepts all the required parameters to display a textbox or
whatever.
Call it from elsewhere in the code, passing the parameters to the sub, and
the textbox or whatever appears on screen.
I never thought of it as OOP before.
Come to think of it, I've done it in Excel VBA.
I wrote a sub to show my own MsgBox so that I could have different colours,
font sizes, etc.
Call MyMsgSub ("Message","Title", Integer Variable)
The Integer Variable defines the appearance of the MsgBox.
If that's OOP, its easy.
I still don't fully understand the concept, though.
I'm probably more practical than theoretical in this sort of thing.
Quite often I find that I know *how* to make it work, but not *why* it
works.


A little clearer, thanks.
Still not fully confident that I understand.
Thanks for your time.
Henry

<SNIP>
 
K

keepITcool

Henry wrote :
Call MyMsgSub ("Message","Title", Integer Variable)
The Integer Variable defines the appearance of the MsgBox.
If that's OOP, its easy.

that is NOT oop.

oop would be more like:

dim myBox as CBox

set myBox = new Cbox
mybox.setsize(10,20,30)
mybox.type = btPizza
mybox.material = bmPlyWood

msgbox mybox.Volume, myBox.Weight
 
F

Fran D

Dear All
I am trying to develop a big class module that will include few other
smaller class modules for easy data transfer. The main module would
allow to manipulate properties easily, something like
..HR.Secretary.name
..HR.Secretary.address1
..HR.Secretary.SalaryBand.GrossAnualIncome
..HR.Secretary.SalaryBand.YearsEmployment

the model to be used is someting like
".Department.Position.PersonalDetails.FurtherDetails"
etc

Unfortunately I am having terrible problems to connect all those class
modules. I created a class module, for the FurtherDetails and another
one for Personal details.
PersonalDetails would include ".Name, .Address1, .Address2, SalaryBand,
....etc"

I am using the Property Set and Get. Although the compiler agrees with
the code and autosensing recognizes the current structure as correct
when you type in the sub, at run time I am getting a very annoying
error "not valid use of property" or "invalid use of With block".

I wonder if anyone can put some light in my confusion.
TIA
FranD
 
B

Bob Phillips

Is this the sort of thing you mean

Sub Salaries()
Dim Secretary As clsResource

Set Secretary = New clsResource
Secretary.Lastname = "Williams"
Secretary.Firstname = "Joyce"
Secretary.SalaryBand = "Grade1"
MsgBox Secretary.Salary.GrossAnnualSalary(Secretary.SalaryBand)
End Sub



Option Explicit

Private mSalary As clsSalary

Private mLastname As String
Private mFirstname As String
Private mSalaryBand As String


Property Get Salary() As clsSalary
Set Salary = mSalary
End Property
Property Let Lastname(pzLastname As String)
End Property

Property Let Firstname(pzFirstname As String)
End Property

Property Let SalaryBand(pzSalaryBand As String)
mSalaryBand = pzSalaryBand
End Property

Property Get SalaryBand() As String
SalaryBand = mSalaryBand
End Property

Public Function GetSalary()
GetSalary = cSalary.Salary(mSalaryBand)
End Function


Private Sub Class_Initialize()
Set mSalary = New clsSalary
End Sub
Property Get GrossAnnualSalary(Grade As String)
Select Case Grade
Case "Grade1": GrossAnnualSalary = "$25,000"
Case "Grade2": GrossAnnualSalary = "$30,000"
Case "Grade3": GrossAnnualSalary = "$35,000"
Case Else: GrossAnnualSalary = "N/A"
End Select
End Property

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)
 
F

Fran D

Hi Bob
Thanks for your code, it´s pretty much what I am looking for and it
works wonders!
I noticed you didn´t use the Property Set at all which I presumed it
was a basic requirement. Could you please tell me when and how would
you use it?

TIA again
FranD
 
B

Bob Phillips

Hi Fran

Property Set is used when declaring an object property, such as a range, as
against a string, number etc.

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)
 
F

Fran D

Thanks again Bob. That certainly helps a lot! I appreciate your patience
with a beginner! Please keep an eye on this thread, no doubt other
questions will arise.
Regards
FranD
 
B

Bob Phillips

Fran,

It might be best to open a new thread for another question, open it out
more.

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)
 

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