PDA

View Full Version : How to implement this effectively



Kurious
08-31-2009, 05:19 AM
Here's an example of something I want to implement and I am not able to do it very efficiently (currently using Enums and Functions).

I have a class called Vehicles and in it are three members Car, Truck and Scooter. These members have properties as below

Car. Make = "Maruti"
Car. Colour = "Black"
Truck.Capacity = 1.2
Scooter.Mileage = 47.68

What i want to do is in the program be able to use a statement like Vehicles.Car.Make (with Intellisense) and get the value "Maruti".

As you can see it in not really an object oriented case ( for eg.Truck and scooter don't have property called make and colour ).

The values are fixed i.e. I don't need to change any of these values but need a convinient way to use these throughout the program.

Any suggestions.......

Benzadeus
09-01-2009, 08:29 AM
Type Vehicles
Make As String
Color As String
Capacity As Single
Mileage As Single
End Type
Sub ExampleData()
Dim Car As Vehicles
Dim Truck As Vehicles
Dim Scooter As Vehicles

Car.Make = "Maruti"
Car.Color = "Black"
Truck.Capacity = 1.2
Scooter.Mileage = 47.68

Debug.Print Car.Make
Debug.Print Car.Color
Debug.Print Truck.Capacity
Debug.Print Scooter.Mileage
End Sub

Kurious
09-02-2009, 02:37 AM
Thanks for the reply, but the problem here is that the Car.Capacity and Car.Mileage still exists and will show up in intellisense.

Benzadeus
09-02-2009, 04:07 AM
Well, you can create a class module called cmVehicle and put code:

Option Explicit
Private pVehicleType As enVehicleType
Private pMake As String
Private pColour As String
Private pCapacity As Long
Private pMileage As Long
Public Property Get VehicleType() As Integer
VehicleType = pVehicleType
End Property
Public Property Let VehicleType(Value As Integer)
pVehicleType = Value
End Property
Public Property Get Make() As String
Make = pMake
End Property
Public Property Let Make(Value As String)
If VehicleType = vtTruck Or VehicleType = vtScooter Then
pMake = vbNullString
Else
pMake = Value
End If
End Property
Public Property Get Colour() As String
Colour = pColour
End Property
Public Property Let Colour(Value As String)
If VehicleType = vtTruck Or VehicleType = vtScooter Then
pColour = vbNullString
Else
pColour = Value
End If
End Property
Public Property Get Capacity() As Long
Capacity = pCapacity
End Property
Public Property Let Capacity(Value As Long)
If VehicleType = vtCar Then
pCapacity = 0
Else
pCapacity = Value
End If
End Property
Public Property Get Mileage() As Long
Mileage = pMileage
End Property
Public Property Let Mileage(Value As Long)
If VehicleType = vtCar Then
pMileage = 0
Else
pMileage = Value
End If
End Property

So, create a module and put
Option Explicit
Public Enum enVehicleType
vtCar = 0
vtTruck = 1
vtScooter = 2
End Enum
Sub Example()
Dim Car As cmVehicle
Dim Truck As cmVehicle
Dim Scooter As cmVehicle

Set Car = New cmVehicle
Set Truck = New cmVehicle
Set Scooter = New cmVehicle

Car.VehicleType = vtCar
Truck.VehicleType = vtTruck
Scooter.VehicleType = vtScooter

Car.Make = "Maruti"
Car.Colour = "Black"
Truck.Capacity = 1.2
Scooter.Mileage = 47.68

Debug.Print Car.Make
Debug.Print Car.Colour
Debug.Print Truck.Capacity
Debug.Print Scooter.Mileage

Set Car = Nothing
Set Truck = Nothing
Set Scooter = Nothing

End Sub

The intellisense list will still appear, but if you try to set any value to, for example, Car.Capacity, the value will not be set to the variable because it was defined as a Car.

If really don't want the intellisense showing the properties you don't want for each type, the only solution I see is creating a type for each type of vehicle:
Option Explicit
Type VCar
Make As String
Color As String
End Type
Type VTruck
Capacity As Long
Mileage As Long
End Type
Type VScooter
Capacity As Long
Mileage As Long
End Type
Sub ExampleData()
Dim Car As VCar
Dim Truck As VTruck
Dim Scooter As VScooter

Car.Make = "Maruti"
Car.Color = "Black"
Truck.Capacity = 1.2
Scooter.Mileage = 47.68

Debug.Print Car.Make
Debug.Print Car.Color
Debug.Print Truck.Capacity
Debug.Print Scooter.Mileage
End Sub

Kurious
09-16-2009, 04:38 AM
Wow, thanks for such a wonderful and detailed solution.

Kurious
09-16-2009, 04:40 AM
I seriously want the intellisense to work (that's the whole reason I am going down this path), so if I go with the second option. Is there a way to put all three types in Vehicle.

So I should still be able to say Vehicle.Car.Make