Can I use a String as a Variable Name?
Hello
Is it possible to get a variable from a string?
Example:
myVar = 9
lablel1.text = "myVar"
Debug.WriteLine( SuperCoolParseFunction(label1.text))
//Now i want the debugwindow to say: 9
Is that possible, to write the name of a variable and get its value?
Thanks
casmang
Re: Can I use a String as a Variable Name?
Quote:
Originally Posted by kulrom
Inside quotation you can put whatever you want, no matter if it's name of control or even if it's reserved word.
But, in your question i found that you are not asking about this but rather to get value of declared variable, and in that case it should be:
VB Code:
lablel1.text = myVar 'without quotation
Happy coding :thumb:
No No...I really just want to use the value of a string as a variable name...
So, if I had a string like this
myString = "myVar"
I want to be able to say
Debug.Writeline (somefunction(myString))
and get the VALUE of myVar rather than it printing out "myVar" as a string.
Another example would be
myVar = 9
myString = "myVar" //as a text value
now, I want to use the value of myString as the Variable Name in an expression. Is that possible to convert a string value to use as a variable name?
Re: Can I use a String as a Variable Name?
Quote:
Originally Posted by kulrom
Ok i see but in example to the above your Label would have text 9 ... means value of myVar would be transfered to label. is that what you want?
I really don't want anything to do with the example, I just want to know if it is possible to use the value of a string as a variable name...for example, if I gave every letter in the alphabet a numeric value. Then I had a textbox on my form that I typed in the letter "m" and hit a button, I want to print out what the numeric value of "m" is based upon the value I gave "m" in my code.
for example
Code:
Dim a, b, c, d, m as Integers
a = 55
b = 32
c = 2
d = 35
m = 16
After this, I enter "m" into the textbox and hit a button and I want to write the value of my entered letter to the debug console. In this case I would have to assign the text of the textbox to a variable, but now I don't know how to use the text as a variable name to print out the value of "m"
Re: Can I use a String as a Variable Name?
He means get the value of a Field Dynamicly by the text value of its name.
Yes this is possible thru relflection, but is not very good practice.
VB Code:
Option Explicit On
Imports System.Reflection
Public Class Form1
Inherits System.Windows.Forms.Form
Private MyVariable1 As String = "Value1"
Private MyVariable2 As String = "Value2"
' FORM DESIGNER CODE OMMITED
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As Type = Me.GetType ' Since the values are contained within this form
Dim Field As FieldInfo = t.GetField(InputBox("Type Variable Name"), BindingFlags.NonPublic Or BindingFlags.Instance)
If Not IsNothing(Field) Then
MsgBox(Field.GetValue(Me))
Else
MsgBox("Invaild Variable Name, IT IS CASE SENSTIVE!")
End If
End Sub
End Class
Re: Can I use a String as a Variable Name?
Hi,
You could also do this by using a single dimension array and use the element number in the way you want to use the variable name. e.g
VB Code:
Dim arrTest() As Integer = {0, 1, 2, 3, 4, 5}
TextBox1.Text = "3"
MessageBox.Show(arrtest(Integer.Parse(TextBox1.Text)).ToString)