user113948
user113948

Reputation: 13

Visual Basic 6, modules that are variable dependent

I am not sure what I am trying to do is possible, I have an older application, that I use to run hardware over a com port. We have just brought in a newer piece of equipment, that does the same thing as the older stuff. I am trying to update the application so it can be used on either piece of equipment. The problem I am having is that the com commands on the newer hardware are different.

How I would like to solve this: All of my com functions are in a module, I have made a second module with all identically named com functions, that have been tailored to support the new machine. The initialization and ident steps are the same, so on form load, I can connect and ask what machine I am connected too. Once I know, I can decide which module to use, and go from there. My problem is, they way I am trying to implement this is not working, and I am starting to wonder if it is possible at all.

(this method doesn't work, but here is a sample of what I am tring to do)

dim Machine(1) as string
dim Number as integer

Number = 0
Machine(0) = Machine1
Machine(1) = Machine2

ComWrite ("*IDN?")
sleep(100)
Response = ComRead

if ComRead = A Then
     Number = 0
Elseif ComRead = B Then
     Number = 1
End if
Machine(Number).somecomfunction

the first module would be named Machine1 and the second would be Machine2 obviously this method returns complier error for invalid qualifier, but I think this gives you an idea of what I am trying to do. Is there any way to do this?

Upvotes: 1

Views: 190

Answers (2)

Holistic Developer
Holistic Developer

Reputation: 2536

What you should do is to define an interface and then make two different classes that implement that interface. It shouldn't be too difficult to take all the existing functions in your module and turn that into a class instead.

Here is an example. I created an interface class named IMachine and two classes that implement that interface: MachineA and MachineB. Then I created a form Form1 with two radio buttons (for selecting which machine) and a button to call the Reset method.

Obviously you shouldn't have MsgBox statements inside a class but this was a quick way to demonstrate. Also, you would supply your own code, similar to in your question, for decided which machine class to instantiate.


IMachine.cls:

Option Explicit

Public Sub Reset()

End Sub

MachineA.cls:

Option Explicit

Implements IMachine

Private Sub IMachine_Reset()
    MsgBox "RESET to MachineA"
End Sub

MachineB.cls:

Option Explicit

Implements IMachine

Private Sub IMachine_Reset()
    MsgBox "*RST to MachineB"
End Sub

Form1.frm:

Option Explicit

Private Sub ResetButton_Click()
    Dim m As IMachine

    If optMachine(0).Value = True Then
        Set m = New MachineA
    Else
        Set m = New MachineB
    End If

    Call m.Reset

End Sub

Upvotes: 4

Matt Wilko
Matt Wilko

Reputation: 27342

You would be able to get a more elegant solution to this using VB.NET, but as you are using VB6 and you want to use modules I think the best way you can do this is something like this:

Put all common routines in one module ModuleCommon, put device specific routines in separate modules Machine1, Machine2 etc.

Public MachineType As Integer

Sub Main
    Call CommonInit 'this method is in ModuleCommon
    MachineType = FindType
    If MachineType = 1 Then
        Call Machine1Reset 'this method is in Module1
    Else
        Call Machine2Reset 'this method is in Module2
    End If
End Sub

Your only other option is to call a single function with a variable as an argument which tells it which command to send:

Public MachineType As Integer

Sub Main
    Call CommonInit 'this method is in ModuleCommon
    MachineType = FindType
    Call MachineReset(MachineType)
End Sub

Then in the module:

Public Sub MachineReset(machineType as Integer)
    If MachineType = 1 Then
         'send "RESET"
    Else
         'send "RST"
    End If
End Sub

Upvotes: 0

Related Questions