2

In my project, I have several modules, all with the same set of Subs. Each module I use for a different brand of RFID card reader, as access methods and reader output is different, but the exact same steps are followed. In my application I want to make generic calls to these steps, but have a application setting that determines the module to call it from, to prevent having to change all of the calls project to project. I want to do something like:

Public CARD_READER_MODULE As ModMTI   'Which doesn't work

Then

CARD_READER_MODULE.Connect()
... etc

Or is there a better way to do this?

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Alex
  • 509
  • 1
  • 11
  • 26
  • Do *not* name your module like that. Use upper camel case AKA pascal case: `CardReaderModule`. – rory.ap Jul 11 '16 at 12:19
  • ModMTI is my module, CARD_READER_MODULE is the constant that tells the application which module to use – Alex Jul 11 '16 at 12:22
  • My bad. Don't name constants that way either. Pascal case. – rory.ap Jul 11 '16 at 12:23
  • And while we're on the topic, it should be `ModMti` not `ModMTI`. Here, you can start with this: https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx – rory.ap Jul 11 '16 at 12:24
  • And this: https://msdn.microsoft.com/en-us/library/ms229002(v=vs.110).aspx – rory.ap Jul 11 '16 at 12:25
  • MTI is an acronym for "Microelectronics Technology Inc." so that is why it is all caps, habit for acronyms I guess. – Alex Jul 11 '16 at 12:26
  • Acronyms should not be in all caps in identifiers. GUID is an acronym too, but it's spelled [Guid](https://msdn.microsoft.com/en-us/library/system.guid(v=vs.110).aspx). – rory.ap Jul 11 '16 at 12:28
  • When using a static class you are doing functional programming, while .net is largely object-oriented. For this application, it would be best to use objects (classes) instead of functions alone. See http://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-programming – djv Jul 11 '16 at 14:16

2 Answers2

5

For the design you have described you should use an interface to describe the methods you want to use and then implement this interface in a new class for each type of device.

So for example your interface looks like this:

Public Interface IDevice
    Sub Connect()
End Interface

Then create a class for each device that has the specific implementation for each device in here:

Public Class DeviceType1
    Implements IDevice

    Public Sub Connect() Implements IDevice.Connect
        'connect to this type of device here
    End Sub
End Class

The beauty of this design is that you can define your variable of Type 'IDevice' and then instantiate it as a particular type:

Dim dev As IDevice
dev = New DeviceType1
dev.Connect()
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
0

I think you should try to find a better way to achieve what you want to do.

I would create a public class for your RFID Reader(s), and inside that class, you can have your properties and functions relating to that specific reader.

Public class Readers

 Public function sharedFunction()
 End function

   Public class Reader1
   End Class

End class

If you just want to access functions without creating an instance of a class (object),I.E. Sharing code, then use "shared" functions inside your class.

I would add a parameter in the constructor of my class, to tell it which reader (child class) I want to work with. Your parent class can have many child (reader) classes, and a set of shared functions that all of those classes can use.

Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
  • What's wrong with a module? It's the exact equivalent of a `static` class in C#. I don't think you'd find many arguments saying you should avoid `static` classes in C#, would you? What is a "newer, better" way to force all members of the class to be `shared`, if not a module? – rory.ap Jul 11 '16 at 12:30
  • Modules or Classes, I would have 8 all with the same Subs, I want to make generic calls and have a single place/variable to tell the application which Module/Class to make calls to. – Alex Jul 11 '16 at 12:32
  • 1
    Write an intelligent class? Pass it a parameter to tell it which reader to work with? Inside the class you can have child classes for different readers, and a single set of shared functions that all of them can use(re-use) – Louis van Tonder Jul 11 '16 at 12:36
  • 1
    Not exactly sure how you would implement that. These readers all have a similar progression of steps, "Activate", "Connect", "Read", "Write", "RSSI"... etc, but very different ways of actually completing these steps. There is not much in terms of reusable code between the readers other than the step. – Alex Jul 11 '16 at 12:41
  • Regarding your previous comment about 8 readers, you can achieve exactly that with my suggestion. – Louis van Tonder Jul 11 '16 at 12:44
  • Perhaps a smart class that delegates to other classes based on the reader setting? I would like to avoid combining any of these modules as the file would end up being 21k+ lines. – Alex Jul 11 '16 at 12:45
  • Fair enough. But I think a smart class handling all of this for you, is the way to go then. You would then work externally with only 1 class, and it would do the heavy lifting of talking to the appropriate reader objects that are defined somewhere else. – Louis van Tonder Jul 11 '16 at 12:46