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.
.net vb.net
|
show 2 more comments
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.
.net vb.net
Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into theDictionary
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
withUserName
andPassword
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, aList(Of String)
and aList(Of Integer)
would be the way to go, although aDictionary(Of String, Integer)
is also a collection that can grow and shrink, as would be aList(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 aDictionary
is that the key is just that: a key. It is what you use to access the corresponding value. If you useUsers.Add("User", 123)
thenUsers("User")
will return 123. You can also lop through theDictionary
to getKeyValuePair
objects where, in that example, theKey
property would be "User" and theValue
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
|
show 2 more comments
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.
.net vb.net
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
.net vb.net
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 theDictionary
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
withUserName
andPassword
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, aList(Of String)
and aList(Of Integer)
would be the way to go, although aDictionary(Of String, Integer)
is also a collection that can grow and shrink, as would be aList(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 aDictionary
is that the key is just that: a key. It is what you use to access the corresponding value. If you useUsers.Add("User", 123)
thenUsers("User")
will return 123. You can also lop through theDictionary
to getKeyValuePair
objects where, in that example, theKey
property would be "User" and theValue
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
|
show 2 more comments
Firstly, look into "concurrent arrays", which is what you're talking about there. After that, look into theDictionary
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
withUserName
andPassword
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, aList(Of String)
and aList(Of Integer)
would be the way to go, although aDictionary(Of String, Integer)
is also a collection that can grow and shrink, as would be aList(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 aDictionary
is that the key is just that: a key. It is what you use to access the corresponding value. If you useUsers.Add("User", 123)
thenUsers("User")
will return 123. You can also lop through theDictionary
to getKeyValuePair
objects where, in that example, theKey
property would be "User" and theValue
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
|
show 2 more comments
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
withUserName
andPassword
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 aList(Of Integer)
would be the way to go, although aDictionary(Of String, Integer)
is also a collection that can grow and shrink, as would be aList(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 useUsers.Add("User", 123)
thenUsers("User")
will return 123. You can also lop through theDictionary
to getKeyValuePair
objects where, in that example, theKey
property would be "User" and theValue
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