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
Solved

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

Posted on 2006-11-23
9
249 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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