Reputation: 17
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?
Upvotes: 0
Views: 46
Reputation: 117064
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:
The bottom-line is that everything is known at compile-time, but you can store data based on the user input at run-time.
Upvotes: 1
Reputation: 19
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
Upvotes: 0
Reputation: 2154
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)
Upvotes: 0