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











up vote
-1
down vote

favorite












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.










share|improve this question
























  • Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into the Dictionary class in VB, which is a better option than two concurrent arrays when one array would be the natural identifiers. Also, you could consider creating a type (class or structure) that represents a single record (User with UserName and Password properties) and then create a single array or collection of objects of that type.
    – jmcilhinney
    Nov 8 at 8:11










  • Also note that arrays are fixed-length, so you should consider using collections rather than arrays if you want to add and/or remove items. In that case, a List(Of String) and a List(Of Integer) would be the way to go, although a Dictionary(Of String, Integer) is also a collection that can grow and shrink, as would be a List(Of User).
    – jmcilhinney
    Nov 8 at 8:13










  • ahh.. thank you. Managed to get it working ish whith the dictionary function. But in a dictionary when you use: Users.Add("User",Password) - could you give this a name. so that the key and value has to come from the same place in the dictionary ?
    – Ehau83
    Nov 8 at 11:09












  • They already do come from the same place. The whole point of a Dictionary is that the key is just that: a key. It is what you use to access the corresponding value. If you use Users.Add("User", 123) then Users("User") will return 123. You can also lop through the Dictionary to get KeyValuePair objects where, in that example, the Key property would be "User" and the Value property would be 123.
    – jmcilhinney
    Nov 8 at 11:13










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    Nov 8 at 11:59















up vote
-1
down vote

favorite












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.










share|improve this question
























  • Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into the Dictionary class in VB, which is a better option than two concurrent arrays when one array would be the natural identifiers. Also, you could consider creating a type (class or structure) that represents a single record (User with UserName and Password properties) and then create a single array or collection of objects of that type.
    – jmcilhinney
    Nov 8 at 8:11










  • Also note that arrays are fixed-length, so you should consider using collections rather than arrays if you want to add and/or remove items. In that case, a List(Of String) and a List(Of Integer) would be the way to go, although a Dictionary(Of String, Integer) is also a collection that can grow and shrink, as would be a List(Of User).
    – jmcilhinney
    Nov 8 at 8:13










  • ahh.. thank you. Managed to get it working ish whith the dictionary function. But in a dictionary when you use: Users.Add("User",Password) - could you give this a name. so that the key and value has to come from the same place in the dictionary ?
    – Ehau83
    Nov 8 at 11:09












  • They already do come from the same place. The whole point of a Dictionary is that the key is just that: a key. It is what you use to access the corresponding value. If you use Users.Add("User", 123) then Users("User") will return 123. You can also lop through the Dictionary to get KeyValuePair objects where, in that example, the Key property would be "User" and the Value property would be 123.
    – jmcilhinney
    Nov 8 at 11:13










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    Nov 8 at 11:59













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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.










share|improve this question















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.







.net vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 8:29









Lankymart

11.4k43898




11.4k43898










asked Nov 8 at 8:00









Ehau83

1




1












  • Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into the Dictionary class in VB, which is a better option than two concurrent arrays when one array would be the natural identifiers. Also, you could consider creating a type (class or structure) that represents a single record (User with UserName and Password properties) and then create a single array or collection of objects of that type.
    – jmcilhinney
    Nov 8 at 8:11










  • Also note that arrays are fixed-length, so you should consider using collections rather than arrays if you want to add and/or remove items. In that case, a List(Of String) and a List(Of Integer) would be the way to go, although a Dictionary(Of String, Integer) is also a collection that can grow and shrink, as would be a List(Of User).
    – jmcilhinney
    Nov 8 at 8:13










  • ahh.. thank you. Managed to get it working ish whith the dictionary function. But in a dictionary when you use: Users.Add("User",Password) - could you give this a name. so that the key and value has to come from the same place in the dictionary ?
    – Ehau83
    Nov 8 at 11:09












  • They already do come from the same place. The whole point of a Dictionary is that the key is just that: a key. It is what you use to access the corresponding value. If you use Users.Add("User", 123) then Users("User") will return 123. You can also lop through the Dictionary to get KeyValuePair objects where, in that example, the Key property would be "User" and the Value property would be 123.
    – jmcilhinney
    Nov 8 at 11:13










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    Nov 8 at 11:59


















  • Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into the Dictionary class in VB, which is a better option than two concurrent arrays when one array would be the natural identifiers. Also, you could consider creating a type (class or structure) that represents a single record (User with UserName and Password properties) and then create a single array or collection of objects of that type.
    – jmcilhinney
    Nov 8 at 8:11










  • Also note that arrays are fixed-length, so you should consider using collections rather than arrays if you want to add and/or remove items. In that case, a List(Of String) and a List(Of Integer) would be the way to go, although a Dictionary(Of String, Integer) is also a collection that can grow and shrink, as would be a List(Of User).
    – jmcilhinney
    Nov 8 at 8:13










  • ahh.. thank you. Managed to get it working ish whith the dictionary function. But in a dictionary when you use: Users.Add("User",Password) - could you give this a name. so that the key and value has to come from the same place in the dictionary ?
    – Ehau83
    Nov 8 at 11:09












  • They already do come from the same place. The whole point of a Dictionary is that the key is just that: a key. It is what you use to access the corresponding value. If you use Users.Add("User", 123) then Users("User") will return 123. You can also lop through the Dictionary to get KeyValuePair objects where, in that example, the Key property would be "User" and the Value property would be 123.
    – jmcilhinney
    Nov 8 at 11:13










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    Nov 8 at 11:59
















Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into the Dictionary class in VB, which is a better option than two concurrent arrays when one array would be the natural identifiers. Also, you could consider creating a type (class or structure) that represents a single record (User with UserName and Password properties) and then create a single array or collection of objects of that type.
– jmcilhinney
Nov 8 at 8:11




Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into the Dictionary class in VB, which is a better option than two concurrent arrays when one array would be the natural identifiers. Also, you could consider creating a type (class or structure) that represents a single record (User with UserName and Password properties) and then create a single array or collection of objects of that type.
– jmcilhinney
Nov 8 at 8:11












Also note that arrays are fixed-length, so you should consider using collections rather than arrays if you want to add and/or remove items. In that case, a List(Of String) and a List(Of Integer) would be the way to go, although a Dictionary(Of String, Integer) is also a collection that can grow and shrink, as would be a List(Of User).
– jmcilhinney
Nov 8 at 8:13




Also note that arrays are fixed-length, so you should consider using collections rather than arrays if you want to add and/or remove items. In that case, a List(Of String) and a List(Of Integer) would be the way to go, although a Dictionary(Of String, Integer) is also a collection that can grow and shrink, as would be a List(Of User).
– jmcilhinney
Nov 8 at 8:13












ahh.. thank you. Managed to get it working ish whith the dictionary function. But in a dictionary when you use: Users.Add("User",Password) - could you give this a name. so that the key and value has to come from the same place in the dictionary ?
– Ehau83
Nov 8 at 11:09






ahh.. thank you. Managed to get it working ish whith the dictionary function. But in a dictionary when you use: Users.Add("User",Password) - could you give this a name. so that the key and value has to come from the same place in the dictionary ?
– Ehau83
Nov 8 at 11:09














They already do come from the same place. The whole point of a Dictionary is that the key is just that: a key. It is what you use to access the corresponding value. If you use Users.Add("User", 123) then Users("User") will return 123. You can also lop through the Dictionary to get KeyValuePair objects where, in that example, the Key property would be "User" and the Value property would be 123.
– jmcilhinney
Nov 8 at 11:13




They already do come from the same place. The whole point of a Dictionary is that the key is just that: a key. It is what you use to access the corresponding value. If you use Users.Add("User", 123) then Users("User") will return 123. You can also lop through the Dictionary to get KeyValuePair objects where, in that example, the Key property would be "User" and the Value property would be 123.
– jmcilhinney
Nov 8 at 11:13












yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
– Ehau83
Nov 8 at 11:59




yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
– Ehau83
Nov 8 at 11:59












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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





share|improve this answer





















  • Thanks Mary. but your code got the same problem as i do. you get "access" regardless of wich password in the dictionary you use. so if you use user "mark" and password "123" it will stillgrant acces. instead of denying.
    – Ehau83
    Nov 9 at 8:03










  • The Contains method is case sensitive. mark will not match Mark. This code is tested and works correctly for me. Mark or mark and password 123 will get No Access. Start a new project and copy and paste my code to see if it works for you.
    – Mary
    Nov 9 at 17:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203502%2fhow-to-use-the-content-of-a-list-or-array-as-variables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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





share|improve this answer





















  • Thanks Mary. but your code got the same problem as i do. you get "access" regardless of wich password in the dictionary you use. so if you use user "mark" and password "123" it will stillgrant acces. instead of denying.
    – Ehau83
    Nov 9 at 8:03










  • The Contains method is case sensitive. mark will not match Mark. This code is tested and works correctly for me. Mark or mark and password 123 will get No Access. Start a new project and copy and paste my code to see if it works for you.
    – Mary
    Nov 9 at 17:15















up vote
0
down vote













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





share|improve this answer





















  • Thanks Mary. but your code got the same problem as i do. you get "access" regardless of wich password in the dictionary you use. so if you use user "mark" and password "123" it will stillgrant acces. instead of denying.
    – Ehau83
    Nov 9 at 8:03










  • The Contains method is case sensitive. mark will not match Mark. This code is tested and works correctly for me. Mark or mark and password 123 will get No Access. Start a new project and copy and paste my code to see if it works for you.
    – Mary
    Nov 9 at 17:15













up vote
0
down vote










up vote
0
down vote









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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 22:32









Mary

2,5602618




2,5602618












  • Thanks Mary. but your code got the same problem as i do. you get "access" regardless of wich password in the dictionary you use. so if you use user "mark" and password "123" it will stillgrant acces. instead of denying.
    – Ehau83
    Nov 9 at 8:03










  • The Contains method is case sensitive. mark will not match Mark. This code is tested and works correctly for me. Mark or mark and password 123 will get No Access. Start a new project and copy and paste my code to see if it works for you.
    – Mary
    Nov 9 at 17:15


















  • Thanks Mary. but your code got the same problem as i do. you get "access" regardless of wich password in the dictionary you use. so if you use user "mark" and password "123" it will stillgrant acces. instead of denying.
    – Ehau83
    Nov 9 at 8:03










  • The Contains method is case sensitive. mark will not match Mark. This code is tested and works correctly for me. Mark or mark and password 123 will get No Access. Start a new project and copy and paste my code to see if it works for you.
    – Mary
    Nov 9 at 17:15
















Thanks Mary. but your code got the same problem as i do. you get "access" regardless of wich password in the dictionary you use. so if you use user "mark" and password "123" it will stillgrant acces. instead of denying.
– Ehau83
Nov 9 at 8:03




Thanks Mary. but your code got the same problem as i do. you get "access" regardless of wich password in the dictionary you use. so if you use user "mark" and password "123" it will stillgrant acces. instead of denying.
– Ehau83
Nov 9 at 8:03












The Contains method is case sensitive. mark will not match Mark. This code is tested and works correctly for me. Mark or mark and password 123 will get No Access. Start a new project and copy and paste my code to see if it works for you.
– Mary
Nov 9 at 17:15




The Contains method is case sensitive. mark will not match Mark. This code is tested and works correctly for me. Mark or mark and password 123 will get No Access. Start a new project and copy and paste my code to see if it works for you.
– Mary
Nov 9 at 17:15


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203502%2fhow-to-use-the-content-of-a-list-or-array-as-variables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)