0

I have a textbox that takes only one character input. I want to create a variable of that character.

For e.g.
If user types a in textbox and clicks "Go" button, then it should create a variable of name 'a' of type integer:

Dim a as integer
6
  • 3
    under what circumstances would users be in charge of defining variable names? You cant do that - the compiler needs to know the name and variables types in order to compile. Commented Mar 18, 2015 at 14:19
  • Well i actually have 2 textboxes. The first one as described in the above problem and the second one for values like 1,2,3,4....n (any value).when user presses "Go" then the value from second textbox is to be stored in a variable that has name from first textbox. Commented Mar 18, 2015 at 14:23
  • 1
    the user cannot see your code, its compiled, so why would they care what you name it? Commented Mar 18, 2015 at 14:28
  • Perhaps what you want is a Dictionary(Of String, Integer), then you could add an integer "called" a to the dictionary with myDictionary.Add("a", 999). Commented Mar 18, 2015 at 14:39
  • I didn't get you I tried Dictionary(Of String, Integer)() Dictionary.Add("a", 999) but it gives error : Dictionary is a type and cannot be used as expression. Sorry i am a newbie Commented Mar 18, 2015 at 14:48

1 Answer 1

1

Assuming you have a Form like this:

The form

Then the code behind could look like:

Public Class Form1

    Private _integerVariables As New Dictionary(Of String, Integer)
    Private _stringVariables As New Dictionary(Of String, String)

    Private Sub btnSaveInteger_Click(sender As Object, e As EventArgs) Handles btnSaveInteger.Click
        Dim newInteger As Integer
        'check if key is there and Text of Value is a valid integer
        If Not String.IsNullOrWhiteSpace(txtIntegerKey.Text) And _
            Integer.TryParse(txtIntegerValue.Text, newInteger) Then
            'check if the key is in the dictionary
            If Not _integerVariables.ContainsKey(txtIntegerKey.Text) Then
                _integerVariables.Add(txtIntegerKey.Text, newInteger)
            Else
                _integerVariables(txtIntegerKey.Text) = newInteger
            End If
        End If
    End Sub

    Private Sub btnSaveString_Click(sender As Object, e As EventArgs) Handles btnSaveString.Click
        'check if key is there
        If Not String.IsNullOrWhiteSpace(txtStringKey.Text) Then
            'check if the key is in the dictionary
            If Not _stringVariables.ContainsKey(txtStringKey.Text) Then
                _stringVariables.Add(txtStringKey.Text, txtStringValue.Text)
            Else
                _stringVariables(txtStringKey.Text) = txtStringValue.Text
            End If
        End If
    End Sub

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

1 Comment

you can access the variables by _whateverVariables(key) whereas key is the string the user entered before

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.