?
Solved

How would I compile a string in VB.NET and then run it as code?

Posted on 2006-11-23
9
Medium Priority
?
273 Views
Last Modified: 2010-04-23
Hi,

I have the following code which is in an event handler for a button:

            With DirectCast(sender, Button)
                Dim index As Integer
                index = CInt(Microsoft.VisualBasic.Right(.Name, 1))
            End With

and then I would like to run something like this:

            tbRFID8.Text = frmAssignTag.assignBox()

where the "8" in the "tbRFID8" is the index variable. So something like

            tbRFID(index).Text = frmAssignTag.assignBox()

although I know that will not work. From my excel work I think i need the function which compiles the express something like

            UnknownFunction("tbRFID" & index & ".Text") = frmAssignTag.assignBox()

Any help would be greatly appreciated. Thanks.
0
Comment
Question by:eadam-uk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:eadam-uk
ID: 18004834
Like the excel INDIRECT() function
0
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 18004930
can you explain a bit more about what you are trying to do? there is probably a better way than to create a string containing code, then parse that string to execute the code, which is what you are wanting if i am reading the question right?
0
 

Author Comment

by:eadam-uk
ID: 18004969
Sure thing,

I have eight text boxes names tbRFID1 ... to tbRFID8 and eight button's names btRFID1... to tbRFID8. I have an event handler on the buttons which is below:

    Private Sub btnAssn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssn1.Click, btnAssn2.Click, _
    btnAssn3.Click, btnAssn4.Click, btnAssn5.Click, btnAssn6.Click, btnAssn7.Click, btnAssn8.Click
             With DirectCast(sender, Button)
                Dim index As Integer
                index = CInt(Microsoft.VisualBasic.Right(.Name, 1))
                MessageBox.Show(CStr(index))
            End With
     End Sub

the index value represents the last number in the button, which is 1 to 8. I then want to assign the results of

   frmAssignTag.assignBox()

to the corresponding text box 1 to 8, instead of displaying it in a message box as it does now. I could do an event handler for every text box, but I would like to create one that handles all buttons and boxes if possible.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:eadam-uk
ID: 18004974
Sorry, that should be btRFD1... to btRFID8 on the third line down, not btRFID1... to tbRFID8
0
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 2000 total points
ID: 18005174
ok so instead of MessageBox.Show(CStr(index)) you want to set the text of the textbox whose name is tbRFID[index]

you can search for a control by its name, so you would use

Dim tb as textbox = CType(Me.FindControl("tbRFID" & index), Textbox)
tb.text = frmAssignTag.assignBox()
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 18005196
i think you are overcomplicating. you are already handling the button click event, and in there you will find that the parameter "sender" has in store already the button that was pressed. so if you modify your code to look like this may be sufficient:

Private Sub btnAssn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssn1.Click, btnAssn2.Click, _
    btnAssn3.Click, btnAssn4.Click, btnAssn5.Click, btnAssn6.Click, btnAssn7.Click, btnAssn8.Click
             With DirectCast(sender, Button)
                .Text = frmAssignTag.assignBox()
            End With
End Sub
0
 

Author Comment

by:eadam-uk
ID: 18005225
Genius, thanks!

Me.FindControl didn't work, but  your advice led me to Me.Controls.Find which produces an array of all the items it finds with that name. As I know I only have one item under that name, i just took the first index in the array which seems to work a treat.

                Dim find As Array = Me.Controls.Find("tbRFID" & index, True)
                Dim tb As TextBox = CType(find(0), TextBox)
                tb.Text = frmAssignTag.assignBox()

Thanks again!
0
 

Author Comment

by:eadam-uk
ID: 18005230
Hi newyuppie,

Wouldn't that assign my value to the button's text then and not the text box?

Thanks.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 18005233
<<Wouldn't that assign my value to the button's text then and not the text box?>>
exactly, but i misunderstood your question!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

718 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