CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2000
    Location
    Jhb, Gauteng RSA
    Posts
    21

    Run-Time Interpretation of Vars

    Hi All,

    Is there a way of interpreting a string ?

    str = "bb = 10"
    interpret(str) <- this is what I want to be able to do!
    debug.print bb (will print out 10)

    many thanks
    RobZ



  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Run-Time Interpretation of Vars

    dim bb as string

    bb= "10"

    debug.print bb

    that will print 10 in the debug window

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Run-Time Interpretation of Vars

    Here is something I ran across. I did not write it. Start a new Project, Add a Module. Add a ComboBox, A command Button, two labels and a textBox using the default names to the form.


    http://www.vb-helper.com/HowTo/execline.zip

    Purpose
    Execute a line of code stored in a string

    Method
    Use the EbExecuteLine API function.

    Thanks to Sergio Perciballi ([email protected]).

    Disclaimer
    This example program is provided "as is" with no warranty of any kind. It is
    intended for demonstration purposes only. In particular, it does no error
    handling. You can use the example in any form, but please mention
    www.vb-helper.com.



    ' Copy the following to a form


    '------------------------------------------
    ' (c) 1999 Trigeminal Software, Inc. All Rights Reserved
    '------------------------------------------
    'Thanks to ----------michka - Michael Kaplan
    'Rest of code by Sergio Perciballi -oigres P (Aug-6-2000)
    'Email: [email protected]
    'Uses the function used by the immediate window
    'to execute a line of code.
    option Compare Text
    option Explicit

    private Declare Function EbExecuteLine Lib "vba6.dll" _
    (byval pStringToExec as Long, byval Foo1 as Long, _
    byval Foo2 as Long, byval fCheckOnly as Long) as Long

    ' for VB5 IDE
    'Declare Function EbExecuteLine Lib "vba5.dll" _
    (byval pStringToExec as Long, byval Foo1 as Long, _
    byval Foo2 as Long, byval fCheckOnly as Long) as Long

    ' for Access 97/VBE.dll clients like Word 97 and Excel 97
    'Declare Function EbExecuteLine Lib "vba332.dll" _
    (byval pStringToExec as Long, byval Foo1 as Long, _
    byval Foo2 as Long, byval fCheckOnly as Long) as Long

    Function FExecuteCode(stCode as string, _
    optional fCheckOnly as Boolean) as Boolean

    FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
    End Function

    private Sub Combo1_Click()
    Text1.Text = Combo1.List(Combo1.ListIndex)
    End Sub

    private Sub Command1_Click()
    Dim res as Boolean
    res = FExecuteCode(Text1.Text)
    Label1.Caption = "Status = " & res
    End Sub

    private Sub Form_Load()
    Combo1.AddItem "?secret"
    Combo1.AddItem "msgbox secret"
    Combo1.AddItem "secret2"
    Combo1.AddItem "for x=0 to 5:?" & Chr$(34) & "hello " & Chr$(34) & "&x:next:beep"
    Combo1.AddItem "sendkeys " & Chr$(34) & "{TAB}" & Chr$(34) & ":sendkeys " & Chr$(34) & "{up}" & Chr$(34)
    Combo1.AddItem "shell " & Chr$(34) & "calc.exe" & Chr$(34) & ",vbNormalFocus"
    Combo1.AddItem "shell " & Chr$(34) & "c:\windows\explorer.exe ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" & Chr$(34) & ",vbNormalFocus"
    Combo1.AddItem "form1.text1.visible=false"
    Combo1.AddItem "form1.text1.visible=true"
    Combo1.AddItem "form1.combo1.listindex=1"
    Combo1.AddItem "msgbox app.Title"
    Combo1.AddItem "form1.combo1.listindex=1:form1.command1.value=true"
    Combo1.ListIndex = 0
    Show

    End Sub



    ' Copy the following to a module

    option Explicit
    'need to be public functions/subs to be available
    '/******************************************************************************
    public Function secret() as string
    '/******************************************************************************

    secret = "this is a secret subroutine"
    End Function
    '/******************************************************************************
    public Sub secret2()
    '/******************************************************************************
    MsgBox "this is a secret subroutine 2"

    End Sub




    John G

  4. #4
    Join Date
    Jun 2000
    Location
    Jhb, Gauteng RSA
    Posts
    21

    Re: Run-Time Interpretation of Vars

    Sorry Perhaps i didn't make myself clear! apologies

    What I require is the generation of variables at runtime,

    i.e. say that the variable bb has never been defined.
    in a textbox I type bb=10.( or any other variable I require)
    I would like to be able to later in the program use the variable

    Another way of putting it is as follows:
    in a running EXE enter into a textbox the following: aa = mid("Hello",2,1)
    then followed by a function to extract the value of aa ( remember this is all run-time)
    what it boils down to is an exe which can be used to demonstrate all the VB functions without actually writing code !

    Many Thanks
    RobZ




  5. #5
    Join Date
    Jun 2000
    Location
    Jhb, Gauteng RSA
    Posts
    21

    Re: Run-Time Interpretation of Vars

    John, You are the MAN !!!!

    That was exactly what i was looking for ( AWESOME !!!)
    (ok excuse spelling i'm really excited)

    Once again Thanks
    RobZ



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