How to use the Content of a List or Array as Variables

I've JUST started doodling with VB.Net and I need some advice for a small task I've given myself.

What I want is that the contents of a list can be used as variables.

I've made a "logon" thing" like this:

Sub Main()
    Dim username As String
    Dim password As Integer
    Dim nope As String = "!!!NO ACCESS!!!"
    Dim ukjent As String = "!!!UNKNOWN USER!!!"

    Console.Write("Enter your name: ")
    username = Console.ReadLine()
    Console.Write("Enter Password: ")
    password = Console.ReadLine()

    If username = "jarvis" And password = 1337 Then
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine("Welcome Jarvis")
        Console.WriteLine("Please enter numerical value")

        Dim X As Decimal = Console.ReadLine()
        Dim y As Decimal = Console.ReadLine()
        Dim z As Decimal = Console.ReadLine()
        Dim i As Decimal

        i = X + y + z

        Console.WriteLine(i)
        Console.WriteLine()

    Else
        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine(nope)

    End If
    Console.ReadLine()

If I want to use lists where I input more "usernames" and more "passwords" how should I do this?

Could I do it like this?

Dim username() As String ={"User1","User2"}
Dim password() As Integer ={ 123, 321}

How would I recall the values in the lists? I know for now I'm not considering matching user1 to password 123. but that can come at a later stage, trying to build piece by piece.

1 answer

  • answered 2018-11-08 22:32 Mary

    You don't want your user to know if it is the username or password or both that is wrong; only that the long-in is not valid. Don't help the hacker.

    TryParse takes the string that user types in. Yes, it is a string even if numbers are typed in. Look up Console.ReadLine and you will see it returns a string.

    TryParse will test the string to see if it can be converted to an integer. If it succeeds it will place the value into the password variable (in this example) and if it fails a zero will be placed in password.

    The .Keys method of the Dictionary returns a collection of all the keys. We check with the Contains method to see if the input username is present. If it is we use the key username to retrieve the value associated with it. If the username is not there we kick the bum out.

    Finally we checked the stored password against the entered password.

    All this checking is because we can't trust user to enter exactly what we expect them to enter.

    Sub Main()
            Dim X, Y, Z As Decimal
            Dim StoredPassword As Integer 'refers to what is in the Dictionary
            Dim username As String = ""
            Dim password As Integer
            Dim nope As String = "!!!NO ACCESS!!!"
            Dim Users As New Dictionary(Of String, Integer) From {{"Mathew", 123}, {"Mark", 456}, {"Luke", 321}}
            Console.Write("Enter your name: ")
            username = Console.ReadLine()
            Console.Write("Enter Password: ")
    
            Integer.TryParse(Console.ReadLine(), password)
            If Users.Keys.Contains(username) Then
                StoredPassword = Users(username)
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine(nope)
                Console.ReadLine()
                End
            End If
            If StoredPassword = password Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Successful Login")
                Console.WriteLine($"Welcome {username}!")
                Console.ForegroundColor = ConsoleColor.White
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine(nope)
                Console.ReadLine()
                End
            End If
    
            Console.WriteLine("Please enter numerical value")
            Decimal.TryParse(Console.ReadLine(), X)
            Console.WriteLine("Please enter numerical value")
            Decimal.TryParse(Console.ReadLine(), Y)
            Console.WriteLine("Please enter numerical value")
            Decimal.TryParse(Console.ReadLine(), Z)
            Dim i As Decimal
    
            i = X + Y + Z
    
            Console.WriteLine($"The sum is {i}")
    
            Console.ReadLine() 'keeps the console window open
    End Sub