Dynamically declare variable type

Posted on 2005-04-13
Last Modified: 2008-01-09

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

Question by:adam_pedley
    LVL 4

    Author Comment

    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


    LVL 9

    Accepted Solution

    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")
    LVL 9

    Expert Comment

    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now