next
01-10-2014, 03:54 PM
I'm trying to write a service calculator in VBA using objects, but I'm pretty noob when it comes to OOP :(
I have several classes:
cService - service / job
Option Explicit
Private description As String
Private crew As cCrew
' get / set service description
Public Property Let setDescription(value As String)
description = value
End Property
Public Property Get getDescription() As String
getDescription = description
End Property
'set service crew
Public Property Let setCrew(value As cCrew)
crew = value
End Property
cCrew - a company that performed the service. Responsible for holding data such as service time, number of workers and calculations of time
Option Explicit
Private code As String
Private techCount As Single
Private serviceStart As Date
Private serviceEnd As Date
' get / set crew code
Public Property Let setCode(value As String)
code = value
End Property
Public Property Get getCode() As String
getCode = code
End Property
' get / set number of techs
Public Property Let setTechCount(value As Single)
techCount = value
End Property
Public Property Get getTechCount() As Single
getTechCount = techCount
End Property
' get set service start
Public Property Let setServiceStart(value As Date)
serviceStart = value
End Property
Public Property Get getServiceStart() As Date
getServiceStart = serviceStart
End Property
' get set service end
Public Property Let setServiceEnd(value As Date)
serviceEnd = value
End Property
Public Property Get getServiceEnd() As Date
getServiceEnd = serviceEnd
End Property
Public Property Get getHours() As Integer
Dim rawTime As Single, result As Single, roundedTime As Double
Dim base As Single, remainder As Single
'calculate service time
rawTime = DateDiff("n", serviceStart, serviceEnd)
result = Round(rawTime / 60, 2)
base = WorksheetFunction.Floor(result, 1)
remainder = result - base
'round to the nearest quarter
Select Case (remainder)
Case 0 To 0.12
roundedTime = base
Case 0.12 To 0.37
roundedTime = base + 0.25
Case 0.38 To 0.62
roundedTime = base + 0.5
Case 0.63 To 0.86
roundedTime = base + 0.75
Case Else
roundedTime = base + 1
End Select
getHours = roundedTime
End Property
Public Property Get getTotalServiceTime() As Integer
getTotalServiceTime = getHours * techCount
End Property
Then I have my sandbox. What I'm trying to do here is create a new service and crew objects, then assign the crew to the service, but it doesn't seem to work. I get invalid use of property error:
Option Explicit
Sub test()
Dim crew As cCrew
Dim service As cService
Set crew = New cCrew
Set service = New cService
crew.setCode = "XY-000"
service.setCrew crew
Debug.Print service.crew.getCode
End Sub
How do I go about coding this?
P.S. I tried:
service.setCrew crew
service.setCrew(crew)
service.setCrew = crew
All give me an error
I have several classes:
cService - service / job
Option Explicit
Private description As String
Private crew As cCrew
' get / set service description
Public Property Let setDescription(value As String)
description = value
End Property
Public Property Get getDescription() As String
getDescription = description
End Property
'set service crew
Public Property Let setCrew(value As cCrew)
crew = value
End Property
cCrew - a company that performed the service. Responsible for holding data such as service time, number of workers and calculations of time
Option Explicit
Private code As String
Private techCount As Single
Private serviceStart As Date
Private serviceEnd As Date
' get / set crew code
Public Property Let setCode(value As String)
code = value
End Property
Public Property Get getCode() As String
getCode = code
End Property
' get / set number of techs
Public Property Let setTechCount(value As Single)
techCount = value
End Property
Public Property Get getTechCount() As Single
getTechCount = techCount
End Property
' get set service start
Public Property Let setServiceStart(value As Date)
serviceStart = value
End Property
Public Property Get getServiceStart() As Date
getServiceStart = serviceStart
End Property
' get set service end
Public Property Let setServiceEnd(value As Date)
serviceEnd = value
End Property
Public Property Get getServiceEnd() As Date
getServiceEnd = serviceEnd
End Property
Public Property Get getHours() As Integer
Dim rawTime As Single, result As Single, roundedTime As Double
Dim base As Single, remainder As Single
'calculate service time
rawTime = DateDiff("n", serviceStart, serviceEnd)
result = Round(rawTime / 60, 2)
base = WorksheetFunction.Floor(result, 1)
remainder = result - base
'round to the nearest quarter
Select Case (remainder)
Case 0 To 0.12
roundedTime = base
Case 0.12 To 0.37
roundedTime = base + 0.25
Case 0.38 To 0.62
roundedTime = base + 0.5
Case 0.63 To 0.86
roundedTime = base + 0.75
Case Else
roundedTime = base + 1
End Select
getHours = roundedTime
End Property
Public Property Get getTotalServiceTime() As Integer
getTotalServiceTime = getHours * techCount
End Property
Then I have my sandbox. What I'm trying to do here is create a new service and crew objects, then assign the crew to the service, but it doesn't seem to work. I get invalid use of property error:
Option Explicit
Sub test()
Dim crew As cCrew
Dim service As cService
Set crew = New cCrew
Set service = New cService
crew.setCode = "XY-000"
service.setCrew crew
Debug.Print service.crew.getCode
End Sub
How do I go about coding this?
P.S. I tried:
service.setCrew crew
service.setCrew(crew)
service.setCrew = crew
All give me an error