tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,834
- Office Version
- 365
- 2019
- Platform
- Windows
I'm not sure if the answer to my following question is the same as the answer to this thread:
Instantiating variables
so I'll ask it here.
This is ClsCar:
This is ClsMotorCars:
This is in a standard module:
I have two questions:
1. I am uncomfortable with this line of code:
in that I expected to see something like this first:
ie declaring before instantiating.
2. The function FuelBudget takes two arguments and consists of three components:
namely FuelCost, Distance and Mileage, wouldn't it be better to create FuelCost and Distance properties instead of passing them as arguments, or is that just a matter of choice?
Also, is this an example of composition, ie class relating to another class?
Thanks
Instantiating variables
so I'll ask it here.
This is ClsCar:
Code:
Private varCar As clsMotorCars
Public Property Set Car(objCar As clsMotorCars)
Set varCar = objCar
End Property
Public Property Get Car() As clsMotorCars
Set Car = varCar
End Property
This is ClsMotorCars:
Code:
Private strColor As String
Private strName As String
Private mG As Double
Property Let Color(clr As String)
strColor = clr
End Property
Property Get Color() As String
Color = strColor
End Property
Property Let Name(nm As String)
strName = nm
End Property
Property Get Name() As String
Name = strName
End Property
Property Let Mileage(milesGallon As Double)
mG = milesGallon
End Property
Property Get Mileage() As Double
Mileage = mG
End Property
Function FuelBudget(FuelCost As Double, Distance As Double) As Double
FuelBudget = (Distance / Mileage) * FuelCost
End Function
This is in a standard module:
Code:
Sub propSetCars()
Dim dist As Double
Dim cost As Double
Dim ownCar As clsCar
Set ownCar = New clsCar
'**************************************
Set ownCar.Car = New clsMotorCars
'**************************************
ownCar.Car.Color = "Yellow"
ownCar.Car.Name = "Ford"
ownCar.Car.Mileage = 50
dist = InputBox("Enter Distance in miles, covered by car in a month")
cost = InputBox("Enter Cost of Fuel per gallon")
MsgBox "Car Color is " & ownCar.Car.Color
MsgBox "Car Model is " & ownCar.Car.Name
MsgBox "Gives a Mileage of " & ownCar.Car.Mileage & " miles per gallon"
MsgBox "$" & ownCar.Car.FuelBudget(dist, cost) & " is the monthly cost of fuel"
End Sub
I have two questions:
1. I am uncomfortable with this line of code:
Code:
'**************************************
Set ownCar.Car = New clsMotorCars
'**************************************
in that I expected to see something like this first:
Code:
Dim owncar.Car = clsMotorCars
ie declaring before instantiating.
2. The function FuelBudget takes two arguments and consists of three components:
Code:
Function FuelBudget(FuelCost As Double, Distance As Double) As Double
FuelBudget = (Distance / Mileage) * FuelCost
End Function
namely FuelCost, Distance and Mileage, wouldn't it be better to create FuelCost and Distance properties instead of passing them as arguments, or is that just a matter of choice?
Also, is this an example of composition, ie class relating to another class?
Thanks