Results 1 to 5 of 5

Thread: Can I use a String as a Variable Name?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    48

    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

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    48

    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:
    1. 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?

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    48

    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"

  4. #4
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    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:
    1. Option Explicit On
    2.  
    3. Imports System.Reflection
    4.  
    5. Public Class Form1
    6.     Inherits System.Windows.Forms.Form
    7.  
    8.     Private MyVariable1 As String = "Value1"
    9.     Private MyVariable2 As String = "Value2"
    10.  
    11. ' FORM DESIGNER CODE OMMITED
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.  
    15.         Dim t As Type = Me.GetType ' Since the values are contained within this form
    16.         Dim Field As FieldInfo = t.GetField(InputBox("Type Variable Name"), BindingFlags.NonPublic Or BindingFlags.Instance)
    17.  
    18.         If Not IsNothing(Field) Then
    19.  
    20.             MsgBox(Field.GetValue(Me))
    21.  
    22.         Else
    23.             MsgBox("Invaild Variable Name, IT IS CASE SENSTIVE!")
    24.  
    25.         End If
    26.  
    27.     End Sub
    28. 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

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    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:
    1. Dim arrTest() As Integer = {0, 1, 2, 3, 4, 5}
    2.  TextBox1.Text = "3"
    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
  •  



Click Here to Expand Forum to Full Width