创建用户定义的名称变量

Create a variable of name that user defines

我有一个只需要一个字符输入的textbox。我想创建一个该字符的变量。

例如
如果用户在文本框中键入 a 并单击 "Go" 按钮,则它应该创建一个名称为 'a' 且类型为整数的变量:

Dim a as integer

假设您有这样的表单:

那么后面的代码可能是这样的:

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