How do I execute a constructed string as a statement?

  • 12 years ago
    I want to be able to change the text in any one of a number of labels by creating a statement then executing it in VB 2008. How do I execute a constructed string as a statement?  Where variable values used to create the string change during program execution.  EXAMPLE:Dim _setlbl as stringDim _num as integerDim _numText as string CODE ………………. 

    _setlbl = "label" & Trim(Str(_num)) & ".text = '" & _numText & "'"

     

     

      (the variable _setlbl may contain “label9.text=’hello world’”  as an example.)  How do I execute the text cotained in the vairable _setlbl as thought it were the statement to change the label text?

    I have not been able to find a method that takes a string and executes it as a statement.  Is there such a thing in VB 2008?

     

  • 12 years ago

    If I understand you ask for equivalent of EVAL. No, you just must implement it. At least simplest parsing needed. If you just want to parse and have only this staff to, don't make it so 'word-by-word' symmetric to VB. Just make string divided by vbCrLf, each row divided by vbTab and you can just use 'LBL SET TEXT vbtab Label1 vbtab MY HAPPY LABEL vbcrlf LBL ADD vbtab Label2. Parsing may look like Dim lines$() = strings.split(mycode$,vbcrlf); for each line$ in lines do: statement$  = strings.split(line,vbtab): select case statement(0); case "LBL SET .... etc

  • 12 years ago

    And also you can iterate through all controls with egForm1.Controls.Item(index).Name equals your query (which is to be needed when you are setting attribute by such statement)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”