Solved

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

Posted on 2006-11-23
9
240 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 500 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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