|
-
Feb 1st, 2005, 11:25 AM
#1
Thread Starter
Member
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
-
Feb 1st, 2005, 11:41 AM
#2
Thread Starter
Member
Re: Can I use a String as a Variable Name?
 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 
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?
-
Feb 1st, 2005, 11:53 AM
#3
Thread Starter
Member
Re: Can I use a String as a Variable Name?
 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"
-
Feb 1st, 2005, 06:20 PM
#4
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
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Feb 1st, 2005, 07:32 PM
#5
PowerPoster
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)
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|