Looking for the best way of toing thinks
I have an application with one text box and one label. I need to figure out the best way for the following situation.
I have thirty code which will be enter into the text box and according with the input code the combines associated number to be shown in the label.
Eg. 123456 = 1
123457 = 2
123435 = 3
......
Meaning when I enter the code (123456) in the text box the the code (1) to be shown in the label.
I can do this with case statement and IF statement. I thought if there was a best way of practice for this to be done like arrays. And if you can show me any small example with this situation because I haven´t used arrays before.
Thanks in advance.
Re: Looking for the best way of toing thinks
Sorry buddy, i think your going to have to rewrite that, i have no idea what your looking to do
Re: Looking for the best way of toing thinks
What makes you select a specific output?
How much different outputs do you have?
Without knowing that, we can't suggest anything!
Re: Looking for the best way of toing thinks
I have 30 codes that each code represents a specific value. When I enter the code in one text box and press a button then the value of that code should be shown in a label.
I think should help you understand what I want to do.
Re: Looking for the best way of toing thinks
Put all the codes and values into a Dictionary, then you simply specify the code and the Dictionary will return the appropriate value. You still have to write a line to add each code/value pair though.
vb.net Code:
Dim lookup As New Dictionary(Of Integer, Integer)
lookup.Add(123456, 1)
lookup.Add(123457, 2)
lookup.Add(123435, 3)
Dim key as Integer = 123457
Dim value As Integer = lookup(key)
MessageBox.Show(value.ToString())
Re: Looking for the best way of toing thinks
Thanks for your solution.
I have tried this and is working except from a problem. The key has decimal places and the label is rounding up the number. I have tried to solve this problem but I didn´t manage it. Can you help me pls?
Dim lookup As New Dictionary(Of Integer, Double)
lookup.Add("05021990", 12.5)
Dim key As Integer = txtCode.Text
Dim value As Integer = lookup(key)
lblResult.Text = value
Thanks in advance.
Re: Looking for the best way of toing thinks
Simply declare the value field as string:
Code:
Dim lookup As Dictionary(Of Integer, String)
Following your example, you would then have the following:
vb.net Code:
Dim key As Integer = txtCode.Text
Dim value As String = lookup(key)
lblResult.Text = value
Re: Looking for the best way of toing thinks
Thanks. I found the solution.
Dim value As Double = lookup(key)
Re: Looking for the best way of toing thinks
The idea is that you declare the Dictionary with the types of the values you're going to put in it. I used Dictionary(Of Integer, Integer) as an example because every value you showed in the first post was an Integer. You've done the righ thing and changed the type of the values to Double but you have left the type of the keys as Integer when you appear to be adding strings. If the keys are Strings and the Values are Doubles then that's how you should declare the Dictionary: Dictionary(Of String Double).