0

I want to be able to allow the user to input a string and then use that string to create a different variable. For example:

Dim UserInput As String
UserInput = Console.Readline()
Dim (UserInput) As String

So if the user input "Hello" a new variable called 'Hello' would be made.

Is this possible and if not, are there any alternatives?

2
  • 3
    Why would you do such a thing? Why would the user need to know the name of a variable? What you're suggesting is ludicrous. What is the end game here, so that we can suggest a better way to achieve it? Commented Jun 27, 2017 at 1:22
  • The names of variables must be known at compile-time. What you're asking for here is to wait until run-time to know the name. That just doesn't work. Commented Jun 27, 2017 at 13:34

3 Answers 3

1

The names of variables must be known at compile-time. What you're asking for here is to wait until run-time to know the name. That just doesn't work

What you can do is use a Dictionary(Of String, String) to hold your data.

This code works:

Dim UserInput As String
UserInput = Console.Readline()
Dim UserInputData As New Dictionary(Of String, String)
UserInputData(UserInput) = "Some Value"
Console.WriteLine(UserInputData(UserInput))

So if you enter Bar then the dictionary has this value in it:

Dictionary

The bottom-line is that everything is known at compile-time, but you can store data based on the user input at run-time.

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

Comments

0

It is currently not possible to dynamically create single variables in VB.net. However, you can use lists or dictionaries (source: Dynamically create variables in VB.NET)

Comments

0

The name of the variable does not matter to the user because they will never see it. They will only ever see the data it holds. There really is no reason for this

Comments

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.