0

I want to be able to reference a variable by what the user enters: For example:

Dim str1 As String = "sherrifs"
Dim str2 As String = "cowboys"
Dim str3 As String = "aliens"
Dim strUserInput As String
    Console.WriteLine("Please enter a variable name: ")
    strUserInput = Console.ReadLine()

If the user enters str1 then the output should be sherrifs etc.. How would I go about doing this in vb.net? Thanks

1
  • The usual way, in a console mode app, to give a user a choice between 3 options is to let him enter a number between 1 and 3. Commented Feb 4, 2018 at 13:40

1 Answer 1

1

Best way would be to keep your items in a Dictionary<string, string> object. Keep key as variableName, value as variableValue.

Dictionary<string, string> variableList = new Dictionary<string, string>()
variableList.Add("str1", "sherrifs")
variableList.Add("str2", "cowboys")
variableList.Add("str3", "aliens")

Dim strUserInput As String
Console.WriteLine("Please enter a variable name: ")
strUserInput = Console.ReadLine()

If variableList.ContainsKey(strUserInput) Then
   Console.WriteLine(variableList(strUserInput))
Else
   Console.WriteLine ("No luck")
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome @CameronWickes, if it helped you can upvote and mark as answer. Accepted answers will help others also.

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.