1
votes

I wanted to test if I could get this to work: I have two textboxes with IDs Textbox1 and Textbox2. I enter the name of a variable I have stored in my program in Textbox1 and then I enter any value in Textbox2. After clicking on a confirm button I want to have the value of the variable name that I've written in Textbox1 changed to the value I wrote in Textbox2.

Something like this (in pseudocode)

GetVariable(Textbox1.Text) = Textbox2.Text

Is there an easy way to get this done or will I be forced to create other type of functions to get around this kind of problem?

3
The only way to do this would be to simply use a case statement, checking the value entered and setting the related variable. Since you have to know the variable name ahead of time anyway to enter it into TextBox1, this seems to be a somewhat meaningless question - it's a horrific idea from a UI standpoint, and there are much better ways to accomplish the same thing. - Ken White
You have a good imagination. What led you to believe that such a thing was possible? Even if it were possible, you would have to specify TextBox1, not TextBox1.Text. - John Saunders
I want to use this sort of function in a larger tool I am going to create in the coming months - for myself to use. I will need to be able to change any type of Variable I have created through a simple user interface so I quite easily can translate the results to the program in itself I am aiming to create - which will not be relying on a tool like this one. So this sort of function would be created to save me some time to avoid making a new "Select Case" for every new variable I need to check until I know how it should look like. Properties was the answer I needed. Thanks for answers, though! - Viktor Ståhl

3 Answers

2
votes

Yes, you can do this using reflection.

dim property = this.GetType().GetProperty(Textbox1.Text)
property.SetValue(this, Textbox2.Text)

This will not work on local variable, but it will work on properties.


Of course the better way would be to just use a Dictionary(of string, string) instead of loose variable. Then you can just write myValues(Textbox1.Text) = Textbox2.Text.

1
votes

Another example of using Reflection. This will work on Fields or Properties. They can be Public or Private, and you don't have to exactly match the Case:

    Try
        Dim FI As System.Reflection.FieldInfo = Me.GetType.GetField(TextBox1.Text, Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
        If Not IsNothing(FI) Then
            FI.SetValue(Me, TextBox2.Text)
        Else
            Dim PI As System.Reflection.PropertyInfo = Me.GetType.GetProperty(TextBox1.Text, Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
            If Not IsNothing(PI) Then
                PI.SetValue(Me, TextBox2.Text)
            Else
                MessageBox.Show(TextBox1.Text, "Field or Property not found!")
            End If
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Unable to Set Value")
    End Try
0
votes

Or you can also create a label control at runtime with the value of TextBox1 Text and assign a value according to the Text of TextBox2. This label will be invisible:

Dim MyNewObject As Control
MyNewObject = New Label
MyNewObject.Name = Textbox1.Text
MyNewObject.Text = Textbox2.Text
MyNewObject.Visible = False
Me.Controls.Add(MyNewObject)

And you can use it as a variable in any part of the form. Example to show the value should be as follows:

MsgBox(Me.Controls(Textbox1.Text).Text)