Consulting

Results 1 to 6 of 6

Thread: How to implement this effectively

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    7
    Location

    How to implement this effectively

    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.......

  2. #2
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    [VBA]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
    [/VBA]

  3. #3
    VBAX Regular
    Joined
    Aug 2009
    Posts
    7
    Location
    Thanks for the reply, but the problem here is that the Car.Capacity and Car.Mileage still exists and will show up in intellisense.

  4. #4
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Well, you can create a class module called cmVehicle and put code:

    [VBA]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[/VBA]

    So, create a module and put
    [VBA]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[/VBA]

    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:
    [VBA]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[/VBA]

  5. #5
    VBAX Regular
    Joined
    Aug 2009
    Posts
    7
    Location
    Wow, thanks for such a wonderful and detailed solution.

  6. #6
    VBAX Regular
    Joined
    Aug 2009
    Posts
    7
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •