Select class

Fredrik76

New Member
Joined
May 27, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
I’m writing some VBA code that will communicate with different instruments (signal generator, power meter etc.). For each instrument type, I want the user to be able to select between different instrument models.
My thought was that I could create a class for each model. In the beginning of the code, I want to declare an object to one of these classes. I want to do this to avoid having my code full of “if” and “case”.
I have written this simple sample code and it seem to work without declaration of the object. If I add a declaration, I get error message “Type Mismatch” or “Duplicate declaration in current scope” deepening on where I put the declaration. Is there any downside running the code without declaration or is it any alternative method I can use doing the same thing?


'Dim Inst As InstrumentType1

Sub Test()
Dim i As Integer
i = Worksheets("Sheet1").Range("A1").Value

If i = 1 Then
'Dim Inst As InstrumentType1
Set Inst = New InstrumentType1
Else
'Dim Inst As InstrumentType2
Set Inst = New InstrumentType2
End If

Receive = Inst.SendString("Hello")
End Sub


‘Class module InstrumentType1
Function SendString(OutBuffer As String) As String
OutBuffer = "AAA" & OutBuffer
Worksheets("Sheet1").Range("A10").Value = OutBuffer
End Function


‘Class module InstrumentType2
Function SendString(OutBuffer As String) As String
OutBuffer = "BBB" & OutBuffer
Worksheets("Sheet1").Range("A10").Value = OutBuffer
End Function
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What you want is an interface. Each of your classes should implement the interface, you would then dim the inst as the interface.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top