• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Dynamically declare variable type


I will have a string passed to my function. This string contains the name of the class in a DLL that i would need to reference.

Dim rfid As New RFID.Readers.<variable>

This code shown above is what i would want to be able to do.

Does anyone know how I can acheive this

  • 2
1 Solution
adam_pedleyAuthor Commented:
I could be going about this the wrong way

If this isnt possible here is what i am actually trying to do

I have a DLL that holds protocols for talking with RFID Readers

Each class (i split each protocol into a different class) in the DLL has the same functions

But of course they all have different ways of getting this information (the code is different for each reader)

in my program i just want to be able to change the reader type in one location and the entire program will still function as normal, without me going around changing all reader types to rfid.reader.texasintrusments etc


Ahh!  Sound's like you've implemented an Interface the hard way!  To quote from the help reference:

"Interfaces are reference types that other types implement to guarantee that they support certain operations. An interface is never directly created and has no actual representation – other types must be converted to an interface type.

An interface defines a contract. A class or structure that implements an interface must adhere to its contract. Interfaces can contain methods, properties, indexers, and events as members."

Since each of your classes exposes Get_ID, Write_Block, etc..., you can create an interface with these types, and make sure each of your classes implements the interface.  In the code, you'll decide which type of class to use, and the interface tells your code that the methods and functions that it needs are available.

Example Interface:

Interface IRFIDReader
      Readonly Property Get_ID as String
      Function Write_Block(StartIndex as integer, DataToWrite as String)  as Boolean
      Function Read_Blocks(StartIndex as integer, Length as Integer) as String
      Function Check_Status() as Integer
End Interface

An Example Class that implements IRFIDReader:
Public Class RFIDTypeA
      Implements IRFIDReader
      Public  Readonly Property Get_ID as String implements IRFIDReader.Get_ID
               Return ...
           End Get
     End Property
     Public Function Write_Block(StartIndex as integer, DataToWrite as String) as Boolean Implements IRFIDReader.Write_Block
          'Write the data....
           Return ...
     End Function
End Class

Example Code that doesn't have to be re-written for each class (and can even handle new readers in the future!)
Sub ....
      dim ir as IRFIDReader
      If ConfigurationFileSays = TypeA then 'pseudocode...
          ir = new RFIDTypeA()
      else If ConfigurationFileSays = Typeb then 'pseudocode...
          ir = new RFIDTypeB()
      end if
      ir.Write_Block(0, "ABCD")
Mind you, I've never used RFID stuff, so most of my code is really a guess - it's the concept I was trying to get across!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now