Usefulness of class modules

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have been going through classes and wondered if there's any point to it.

The following example is taken from the Mr. Excel Book, VBA And Macros For Microsoft Excel.

This is in module1:

Code:
Sub EmpPayOverTime()

Dim Employee As New clsEmployee

With Employee

    .EmpName = "Joe Bloggs"
    .EmpRate = 100
    .EmpWeeklyHrs = 45
    MsgBox .EmpName & Chr(10) & Chr(9) & _
    "Normal Hours: " & .EmpNormalHrs & Chr(10) & Chr(9) & _
    "OverTime Hours: " & .EmpOverTimeHrs & Chr(10) & Chr(9) & _
    "Weekly Pay : $" & .EmpWeeklyPay

End With

End Sub

This is in the class, clsEmployee:

Code:
Public EmpName As String
Public EmpRate As Double

Private NormalHrs As Double
Private OverHrs As Double

Property Let EmpWeeklyHrs(Hrs As Double)

NormalHrs = WorksheetFunction.Min(35, Hrs)
OverHrs = WorksheetFunction.Max(0, Hrs - 35)

End Property

Property Get EmpWeeklyHrs() As Double

EmpWeeklyHrs = NormalHrs + OverHrs

End Property

Property Get EmpNormalHrs() As Double

EmpNormalHrs = NormalHrs

End Property

Property Get EmpOverTimeHrs() As Double

EmpOverTimeHrs = OverHrs

End Property

Public Function EmpWeeklyPay() As Double

EmpWeeklyPay = (EmpNormalHrs * EmpRate) + (EmpOverTimeHrs * EmpRate * 2)

End Function

[ASIDE: There's a slight quirk here in that if I step into the module, when I get to the Function EmpWeeklyPay, it jumps to the Property Get EmpOverTimeHrs first. I expected it to read from left to right and so should go to the Property EmpNormalHrs first].

My main question is, what's the diference if I did this?

Put this in module2:

Code:
Sub EmpPayOverTimeChanged()

Dim Employee As New clsEmployeechanged

With Employee

    .EmpName = "Joe Bloggs"
    .EmpRate = 100
    .EmpWeeklyHrs = 45
    
    MsgBox .EmpName & Chr(10) & Chr(9) & _
    "Normal Hours: " & .NormalHrs & Chr(10) & Chr(9) & _
    "OverTime Hours: " & .OverHrs & Chr(10) & Chr(9) & _
    "Weekly Pay : $" & .EmpWeeklyPay

End With

End Sub

This is in a new class, clsEmployeechanged:

Code:
Public EmpName As String
Public EmpRate As Double

Public NormalHrs As Double
Public OverHrs As Double

Property Let EmpWeeklyHrs(Hrs As Double)

NormalHrs = WorksheetFunction.Min(35, Hrs)
OverHrs = WorksheetFunction.Max(0, Hrs - 35)

End Property

Property Get EmpWeeklyHrs() As Double

EmpWeeklyHrs = NormalHrs + OverHrs

End Property

Public Function EmpWeeklyPay() As Double

EmpWeeklyPay = (NormalHrs * EmpRate) + (OverHrs * EmpRate * 2)

End Function

It seems it does the same thing by simply changing the variables NormalHrs and OverHrs from Private to Public.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
In your example, nothing; there's no tangible difference; it's more convention than anything else. Generally however, you want your classes to be self contained (or group of classes to be self contained) and you'd want to limit the access outside procedures have as well as controlling how the data is returned.

Getting and Letting/Setting allow you to process inputs and outputs to give you better control over your classes, you can be sure that all the private variables are sanitised and in a format that your code expects - you can't do this with public variables, outside procedures could set them to be whatever they like; this would mean you'd have to check the value in the class each and every time you use it.
 
Upvote 0
In your example, nothing; there's no tangible difference; it's more convention than anything else. Generally however, you want your classes to be self contained (or group of classes to be self contained) and you'd want to limit the access outside procedures have as well as controlling how the data is returned.

Getting and Letting/Setting allow you to process inputs and outputs to give you better control over your classes, you can be sure that all the private variables are sanitised and in a format that your code expects - you can't do this with public variables, outside procedures could set them to be whatever they like; this would mean you'd have to check the value in the class each and every time you use it.

Thanks for your reply.

Do have a couple of related question on classes.

Question 1:

This is in a module:

Code:
Sub clsStudentRun()

Dim iStudent As clsStudent
Set iStudent = New clsStudent

iStudent.Name = "Peter"

iStudent.Marks = 45

MsgBox iStudent.Name & " has got " & iStudent.Marks & " percent marks with a Grade " & iStudent.Grade

End Sub

This is in clsStudent:

Code:
Private strStuName As String
Private dblStuMarks As Double

Public Property Let Name(strN As String)

strStuName = strN

End Property

Public Property Get Name() As String

Name = strStuName

End Property

Public Property Let Marks(iMarks As Double)

dblStuMarks = (iMarks / 80) * 100

End Property

Public Property Get Marks() As Double

Marks = dblStuMarks

End Property

Public Function Grade() As String

Dim strGrade As String

If dblStuMarks >= 80 Then

strGrade = "A"

ElseIf dblStuMarks >= 60 Then

strGrade = "B"

ElseIf dblStuMarks >= 40 Then

strGrade = "C"

Else

strGrade = "Fail"

End If

Grade = strGrade

End Function

I noticed if I changed Public Function Grade to Public Propoerty Get Grade, it still works, so what's the difference?

Question 2:

This is in a module:

Code:
Sub clsRectangleRun()

Dim l As Double
Dim w As Double

Dim rect As New clsRectangle

l = InputBox("Enter Length of rectangle")
w = InputBox("Enter Width of rectangle")

rect.Area(l, w) = l * w

a = rect.Area(l, w)

MsgBox "Area of Rectangle with length " & l & ", width " & w & ", is " & a

End Sub

This is in clsRectangle:

Code:
Private dblA As Double

Public Property Let Area(lngth As Double, wdth As Double, ar As Double)

dblA = ar

MsgBox "Arguments received - lngth: " & lngth & ", wdth: " & wdth & ", ar: " & ar

End Property

Public Property Get Area(lngth As Double, wdth As Double) As Double

Area = dblA

End Property

What puzzles me in the second question is that stepping into the module, it asks the user for the length and width and as soon as they are entered, this line is reached:

Code:
rect.Area(l, w) = l * w

it then jumps into the class and immediately the variable ar is assigned the value of l * w.

How does it do that?

Thanks again
 
Upvote 0
The last argument of a property with multiple arguments is passed after the equals sign - though it's a bit confusing at first (not to mention rarely useful) ;) - keep your properties to one parameter

Though if it were me, I'd probably have separate properties for length and width, then a method/read only property to return the area.
 
Upvote 0
The last argument of a property with multiple arguments is passed after the equals sign - though it's a bit confusing at first (not to mention rarely useful) ;) - keep your properties to one parameter

Though if it were me, I'd probably have separate properties for length and width, then a method/read only property to return the area.

Thanks, I do admit it looked weird, though in my defence, it wasn't written by me!

Any ideas about my first question, ie difference between using method v property?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,058
Messages
6,128,532
Members
449,456
Latest member
SammMcCandless

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top