0

How can you, in VB.Net, evaluate the value of controls referenced in a string? Suppose you have a textbox N containing some alphanumeric content and it were referenced in a string variable like this:

Dim s As String = """Hello "" & N.Text & ""!"""

I'd like to be able to reevaluate the contents of the s variable elsewhere in the code (where there's no direct knowledge of the existence of N), for example, like this:

Dim e As String = Eval(s)

This isn't possible with VB.Net; no such Eval function is available. I've seen some nearly plausible solutions online such as using the old COM ScriptControl:

Private Shared _scriptControl As MSScriptControl.ScriptControl = New ScriptControl()
_scriptControl.Language = "VBScript"
Dim e As String = _scriptControl.Eval(s)

However, the reference to the N.Text property of a Windows Forms control is alien to the ScriptControl object, and therefore it throws an error.

Is there any other quick fix that won't require the purchase of a third-party component?

ShieldOfSalvation
  • 1,297
  • 16
  • 32
  • Do you need to consider an entire expression, or are you just worried about evaluating a string identifying a variable/control? The latter should be fairly straightforward to do using reflection. – Craig Jul 18 '17 at 18:00
  • Sounds like a common ordinary string variable. Resolve the contents upfront, once – Ňɏssa Pøngjǣrdenlarp Jul 18 '17 at 18:01
  • Does it need to be a string? If not, a delegate to a lambda expression would be preferable, e.g. `Dim s As Func(Of String) = Function() "Hello " & N.Text & "!"` – Steven Doggart Jul 18 '17 at 18:03
  • 1
    sounds a bit like a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Slai Jul 18 '17 at 18:19
  • The problem is distinctly that I need access to N.Text's value from a method that doesn't know what N might be, only that its contents are alphanumeric. Really, N can really be any other control inside the form--so I cannot resolve the contents upfront because that can vary. I need to evaluate my string variable s at runtime! I only mentioned the ScriptControl idea to fend off answers that might involve that approach since I already tried it and don't want to waste our time. – ShieldOfSalvation Jul 18 '17 at 18:31
  • you can loop the form `.Controls` to find the ones with the alphanumeric text https://stackoverflow.com/questions/27777200/find-a-label-in-winforms-which-contains-certain-text – Slai Jul 18 '17 at 18:44

2 Answers2

3

However, the reference to the N.Text property of a Windows Forms control is alien to the ScriptControl object, and therefore it throws an error. ... I only mentioned the ScriptControl idea to fend off answers that might involve that approach I mentioned since I already tried it and don't want to waste our time.

You can add the TextBox to the Script Control. First, get a reference to "N" using it's Name. Not sure if you've got the name of the control by itself already; if not, you may need to parse your string to get the name. Then use Controls.Find() to get a reference and pass that to AddObject(). Now your string can be evaluated as expected:

Private Shared _scriptControl As MSScriptControl.ScriptControl = New ScriptControl()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _scriptControl.Language = "VBScript"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ctlName As String = "N"
    Dim ctl As Control = Me.Controls.Find(ctlName, True).FirstOrDefault
    If Not IsNothing(ctl) Then
        Try
            _scriptControl.AddObject(ctlName, ctl)
        Catch ex As Exception
        End Try

        Dim code As String = """Hello "" & N.Text & ""!"""
        Dim result As String = _scriptControl.Eval(code)
        MessageBox.Show(result)
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Eureka! This answer shed more light for me on the applicability of the ScriptControl object inside a Winforms project. I had dismissed this approach before--but with your assistance I've made it work! Thank you very much! – ShieldOfSalvation Jul 18 '17 at 20:29
1

String Interpolation was introduced in Visual Basic 14. Since the control is declared as a Friend variable in the Form's designer, then presumably you could use the reference to the control in the String like this:

Dim s As String = $"Hello {N.Text}!"
David
  • 5,877
  • 3
  • 23
  • 40
  • I misunderstood the question. My only other thought would be to create a Class with two properties, one to store the value of the message and the other to store the alphanumeric value. – David Jul 18 '17 at 18:40