generating variable names

Sorry to be so stupid but I'm new and as yet have been unable to find a post that answers my question by searching.

What I am trying to do sounds straight forward to me - but I can't do it.

I want to be able to define and access variables from a string.

Eg

Instead of

dim variable1 as integer
dim variable2 as integer
dim variable3 as integer
dim variable4 as integer
dim variable5 as integer
dim variable6 as integer

I want to be able write a for loop that defines all something like

dim counter as integer

for counter = 1 to 6
dim variable & counter as integer
next counter

Needless to say there aren't 6 variables (and for that matter nor is the code quite so simple)

Similarly I might want this so that the program user can input a variable name into a text box and the code could then return the value of the variable corresponding to that variable name.

Any help greatfully received




Answer this question

generating variable names

  • Chase Mosher

    i think what you're aiming for is not exactly possible. otherwise you would be able to dynamically write code and execute it via executing a string.

    i agree with using hash tables, I love them

  • White Hawk

    Sound like you want to use an array.

    Dim variables(5) As Integer

    For counter As Integer = 0 To 5

    ' do things with variables(counter)

    End users don't normally have knowledge of the variable names used inside a program so taking variable names as user input seems a bit odd.



  • sybaselu

    I would just create a hash table.  You can put whatever kind of object in there.  (Not just integers)

    Public Class Form1< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

        Private Variables As Hashtable

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            'Create the hastable

            Variables = New Hashtable()

     

            'add a few items to the hastable with a random value.

            For i As Integer = 0 To 9

                Variable("Item" & i) = Rnd()

            Next

     

            'Print item from hashtable.  Note how it's referenced as a string.

            Debug.Print(Variable("Item3"))

        End Sub

     

        Private Property Variable(ByVal Item As String) As Object

            Get

                Return Variables(Item)

            End Get

            Set(ByVal value As Object)

                Variables(Item) = value

            End Set

        End Property

    End Class

     

     


    Dustin.



  • LeeM

    Thanks for that but it doesn't really resolve the issue. Aside from the fact that each variable is an array (I know I used integer in the example) it is really about trying to get variable names from strings. It may not be possible - I'll rethink the whole array thing.

    Among other things I'm actually trying to vary the number number of radio buttons dependent upon user input - but possibly I can have an array of radio buttons. Off to go an investigate. Thanks a bunch.

    Still if anyone else know of a way of dealing with the original problem ie generating a variable name from a string I'd be most greatful.



  • HansS4

    wolfr wrote:
    i think what you're aiming for is not exactly possible. otherwise you would be able to dynamically write code and execute it via executing a string.

    i agree with using hash tables, I love them

    This can already be done. You can generate code and compile and run during run time. I use that from time to time to generate dynamic apps.

    However, it's not always quick to generate code and compile it during run time. That would be overkill for what was requested.. If all you need to do is reference a variable by passing a string, then a hash table is perfect for this.

    Yes there are other methods of doing this, but hashtables are quick and efficient, and you don't need to spend time writting code to do variable lookups.



  • generating variable names