A
Alex Pierson
Is there no easier way to convert a string into the variable that the
string represents?
string represents?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Alex Pierson said:Is there no easier way to convert a string into the variable that the
string represents?
Alex Pierson said:Sorry that was meant to be a reply to another thread, but my question
is this.
I need to convert a string into a value that is represented by a
variable that has the same name as the string.
EG.
Dim str as string = "var1"
dim var1 as integer = 5
I want to convert str in this case to 5.
Thanks.
x = 0
y = 1
z = 2 etc
An outside source, eg. a database, tells me which variable to use in a
particular instance.
So the external source says use variable x in the equation.
The equation is something like:
Ans = 50 + [the applicable variable], or in this case 50 + x
Alex said:Sorry that was meant to be a reply to another thread, but my question
is this.
I need to convert a string into a value that is represented by a
variable that has the same name as the string.
EG.
Dim str as string = "var1"
dim var1 as integer = 5
I want to convert str in this case to 5.
Alex said:Sorry that was meant to be a reply to another thread, but my question
is this.
I need to convert a string into a value that is represented by a
variable that has the same name as the string.
EG.
Dim str as string = "var1"
dim var1 as integer = 5
I want to convert str in this case to 5.
Thanks.

Jay B. Harlow said:Dim aFieldName As String = "x"
Dim aSomething As New Something
Dim fields() As System.Reflection.FieldInfo =
aSomething.GetType().GetFields()
For Each field As System.Reflection.FieldInfo In fields
If field.Name = aFieldName Then
field.SetValue(aSomething, aValue)
End If
Next
Public Class Something
Public x As Integer = 0
Public y As Integer = 1
Public z As Integer = 2
End Class
The "problem" is that GetFields return an array of FieldInfo objects, you
need to search for the field with the name you want. In this case I will
consider using System.ComponentModel.PropertyDescriptor instead as
System.ComponentModel.PropertyDescriptorCollection allows indexing by
name.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.