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 / Excel / Programming / February 2006

Tip: Looking for answers? Try searching our database.

class module?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sybmathics - 15 Jun 2005 18:43 GMT
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
Dick Kusleika - 15 Jun 2005 18:49 GMT
Sybolt

Here are some examples

http://www.dicks-blog.com/archives/category/vba/classes/

Signature

Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com

> Hi,
>
[quoted text clipped - 13 lines]
>
> Sybolt
Jim Thomlinson - 15 Jun 2005 19:00 GMT
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.
Signature

HTH...

Jim Thomlinson

> Hi,
>
[quoted text clipped - 13 lines]
>
> Sybolt
keepITcool - 15 Jun 2005 21:45 GMT
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 :

> 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
[quoted text clipped - 9 lines]
> the world of the Excel object model more understandable but they are
> not for the beginner or the faint of heart.
Henry - 16 Jun 2005 00:18 GMT
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

>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
[quoted text clipped - 32 lines]
>>
>> Sybolt
Stephen Bullen - 16 Jun 2005 18:30 GMT
Does that make it any clearer?

Regards

Stephen Bullen
Take your Excel development to the highest levels
with "Professional Excel Development",
www.oaltd.co.uk/ProExcelDev

> Jim and others,
>
[quoted text clipped - 58 lines]
> >>
> >> Sybolt
Stephen Bullen - 17 Jun 2005 21:07 GMT
> 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
BAC - 16 Jun 2005 19:49 GMT
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

> Jim and others,
>
[quoted text clipped - 58 lines]
> >>
> >> Sybolt
Henry - 17 Jun 2005 00:31 GMT
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

> Think of a "Class" as a series of subroutines and functions that build a
> car
[quoted text clipped - 125 lines]
>> >>
>> >> Sybolt
Henry - 22 Jun 2005 01:36 GMT
> Think of a "Class" as a series of subroutines and functions that build a
> car
[quoted text clipped - 14 lines]
> 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
[quoted text clipped - 6 lines]
> 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
[quoted text clipped - 3 lines]
> 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
[quoted text clipped - 3 lines]
> 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.

> Clearer?

A little clearer, thanks.
Still not fully confident that I understand.

> BAC

Thanks for your time.
Henry

<SNIP>
keepITcool - 22 Jun 2005 08:12 GMT
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

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam
Fran D - 19 Feb 2006 18:30 GMT
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

Signature

Fran D

Bob Phillips - 19 Feb 2006 19:37 GMT
Is this the sort of thing you mean

>>>>>>>>> Standard code module

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

>>>>>>>> Secretary Class - named clsResource

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
>>>>>>>> Salart Class - named clsSalary

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)

> Dear All
> I am trying to develop a big class module that will include few other
[quoted text clipped - 29 lines]
> Fran D's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=28365
> View this thread: http://www.excelforum.com/showthread.php?threadid=379451
Fran D - 21 Feb 2006 22:10 GMT
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

Signature

Fran D

Bob Phillips - 21 Feb 2006 22:49 GMT
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)

> Hi Bob
> Thanks for your code, it´s pretty much what I am looking for and it
[quoted text clipped - 11 lines]
> Fran D's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=28365
> View this thread: http://www.excelforum.com/showthread.php?threadid=379451
Fran D - 25 Feb 2006 13:49 GMT
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

Signature

Fran D

Bob Phillips - 25 Feb 2006 14:29 GMT
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)

> 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
[quoted text clipped - 7 lines]
> Fran D's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=28365
> View this thread: http://www.excelforum.com/showthread.php?threadid=379451
K Dales - 15 Jun 2005 20:42 GMT
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

> Hi,
>
[quoted text clipped - 13 lines]
>
> Sybolt
 
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.