Adding Additional Code Components
I want to include the ability to add new code components to a VB6 client app that I am writing now. Suppose for example, my EXE client is a 'car driver program', and suppose I have created 3 'car' components... Then at run time, I could do something like:
Dim objCar As Object
Select Case iCar
Case 1
Set objCar = CreateObject("Ford.Car")
Case 2
Set objCar = CreateObject("RollsRoyce.Car")
Case Else
Set objCar = CreateObject("Volkswagen.Car")
End Select
objCar.StartEngine
iGasLevel = objCar.GetGasLevel
...
This all looks fine... for the 'cars' in my garage today. But here's my problem... Suppose I create my client.exe program, and sell millions of copies, and now want to add a Lamborghini, but WITHOUT modifying my program. I have the new car, but there's no code that says 'CreatOpject("Lamboghini.car")
How do I do this? Should I not be using Code Components?
I guess this is also like the world of printers and printer drivers: I anticipate making new driver files to support new printers.. thats no problem. But how do I create a program that allows me to 'install' new printers in the future?
Easy to explain... but I dont know how to implement this flexability.
Thanks for any advice...
bb
Re: Adding Additional Code Components
You can create a class module that has the basic "Car" functions that are common to all the cars. Then you can create class modules that implement this class module.
Code:
' Car.cls
Option Explicit
Public Property Get Speed() As Long
End Property
Public Property Let Speed(ByVal NewValue As Long)
End Property
Code:
' Ford.cls
Option Explicit
Implements Car
Private Property Let Car_Speed(ByVal RHS As Long)
Debug.Print "Ford: speed was set"
End Property
Private Property Get Car_Speed() As Long
Debug.Print "Ford: speed was queried"
End Property
Code:
' RollsRoyce.cls
Option Explicit
Implements Car
Private Property Let Car_Speed(ByVal RHS As Long)
Debug.Print "RollsRoyce: speed was set"
End Property
Private Property Get Car_Speed() As Long
Debug.Print "RollsRoyce: speed was queried"
End Property
Code:
' in Form1: add Command1
Option Explicit
Private Sub Command1_Click()
Dim Cars As Car
Set Cars = New Ford
Cars.Speed = 100
Set Cars = New RollsRoyce
Cars.Speed = 200
If TypeOf Cars Is Car Then Debug.Print "It is a car"
If TypeOf Cars Is Ford Then Debug.Print "It is a Ford"
If TypeOf Cars Is RollsRoyce Then Debug.Print "It is a RollsRoyce"
Set Cars = Nothing
End Sub
Edit!
As an addition, I think you could create an ActiveX DLL for each car and then just include these DLLs from a certain directory. However, I'm not sure how you'd know the name of the car: maybe by filename. All in all, you're requesting for something that is on the complex side. For example, currently you're ignoring all the possible special features that a car could have (= own specialized procedures).
Re: Adding Additional Code Components
Your implementation of the car class will work, however I'm still stuck with the 'form1' code, where I have to already know the name of the car:
Code:
Set Cars = New Ford
...
If TypeOf Cars Is Ford Then Debug.Print "I have to know FORD here"
I guess what I want is the ability to load and unload an instance somehow, based on a FILENAME (defined perhaps in a Cars.ini text file)
Code:
' Cars.ini
[Car]
SystemCarName=Ford.dll
SystemCarName=Ford.cls
Code:
' Form2
dim SystemCarName As String
Private Sub ReadCarINIFile()
SystemCarName = ... 'filename parsed from text file'
End Sub
Private Sub cmdLoadCar_Click()
Set SystemCar = load SystemCarName ' What really goes here???
SystemCar.Speed = 200
End Sub
How can I dynamically load (and unload) this 'file' ? And should it be a class, activeX dll, or ???
Re: Adding Additional Code Components
You can add the name of the car as a procedure, so Car.Model or Car.Name returns what you want.
Necessarily you don't need an INI file: you could have the DLLs in a folder that is within the application folder. Here are a few links that should be helpful:
Understanding plugins (in VB6)
Load DLLs at runtime in VB6