1

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?

4
  • 2
    Might want to check out this question: Access a methods local variable value using a string containing the variable name in .Net Commented Dec 4, 2014 at 1:29
  • 1
    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. Commented Dec 4, 2014 at 1:31
  • 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. Commented Dec 4, 2014 at 1:55
  • 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! Commented Dec 4, 2014 at 10:47

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer! I managed to solve the problem I had through using Reflection and this answer pointed me in the right direction and then Idle_Mind's answer made me really solve my issue so thank you very much!
1

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

1 Comment

Great! Thank you! I got it to work through this! I didn't even think of using properties and yet it solves the problem I have perfectly! Thank you!
0

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)

1 Comment

While I do agree this would solve the problem in itself I asked about here it would not be translatable to where I really want to use it! But thank you for the answer anyways - it still gave me a new way to come around problems such as this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.