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









New contributor




Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    2 days ago










  • 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
    2 days ago










  • 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
    2 days ago












  • 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
    2 days ago










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    2 days ago















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









New contributor




Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    2 days ago










  • 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
    2 days ago










  • 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
    2 days ago












  • 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
    2 days ago










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    2 days ago













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









New contributor




Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Lankymart

11.4k43896




11.4k43896






New contributor




Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Ehau83

1




1




New contributor




Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ehau83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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
    2 days ago










  • 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
    2 days ago










  • 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
    2 days ago












  • 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
    2 days ago










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    2 days ago


















  • 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
    2 days ago










  • 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
    2 days ago










  • 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
    2 days ago












  • 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
    2 days ago










  • yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
    – Ehau83
    2 days ago
















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
2 days ago




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
2 days ago












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
2 days ago




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
2 days ago












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
2 days ago






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
2 days ago














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
2 days ago




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
2 days ago












yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
– Ehau83
2 days ago




yes. but if i have added: User1, 123 User2, 321 and type inn user1 - 321 i will get a true return.
– Ehau83
2 days ago












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
    yesterday










  • 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
    yesterday











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
});


}
});






Ehau83 is a new contributor. Be nice, and check out our Code of Conduct.










 

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
































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
    yesterday










  • 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
    yesterday















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
    yesterday










  • 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
    yesterday













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 yesterday









Mary

2,4902617




2,4902617












  • 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
    yesterday










  • 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
    yesterday


















  • 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
    yesterday










  • 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
    yesterday
















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
yesterday




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
yesterday












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
yesterday




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
yesterday










Ehau83 is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Ehau83 is a new contributor. Be nice, and check out our Code of Conduct.













Ehau83 is a new contributor. Be nice, and check out our Code of Conduct.












Ehau83 is a new contributor. Be nice, and check out our Code of Conduct.















 


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




















































































Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check