CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2008
    Posts
    4

    Using a String as Variable Name

    Once upon a time, I found a code example that allowed you to use a string as a variable name in Visual Basic 6.0.

    The example was extremely simple and worked fine, so I documented it for later use. Then like all good engineers, I placed the document somewhere that it could never be found again.

    The example was like SOMETHING("STRING"). So, SOMETHING("MyVar") would be the same as a variable named MyVar.

    ...
    dim tempStr as String
    for i = 0 to 5
    tempStr = "MyVar" & i
    SOMETHING(tempStr) = i 'not an array
    next
    ...

    I AM NOT LOOKING FOR OTHER WAYS TO DO THIS.
    I AM OBSESSED WITH FINDING THIS EXACT FUNCTION AGAIN.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Using a String as Variable Name

    You can write script code to kind of do what you want. Normally, used for MATH.

    Warning! Dangerous to leave in code. User can do ANYTHING!

    Code:
    Option Explicit
    
    ' Add a reference to Microsoft Script Control 1.0
    
    Private Sub Form_Load()
    Dim str1 As String
    Dim dbl1 As Double
    Dim x As New ScriptControl
    Dim z%, a%, q$
    
    z = 55
    a = 24
    q = "*"
    x.Language = "vbScript"
    MsgBox Format(x.Eval(z & q & a), "standard")
     
    ' ===============================================
    
     str1 = "(5+50)*2/3"
     dbl1 = (x.Eval(str1))
     MsgBox dbl1
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: Using a String as Variable Name

    Have a look at CallByName function or as suggested by dglienna use script control.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured