Results 1 to 9 of 9

Thread: Looking for the best way of toing thinks

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    33

    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.

  2. #2
    Fanatic Member CodedFire's Avatar
    Join Date
    Aug 2007
    Location
    In Cog Neato
    Posts
    568

    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
    Languages: Visual Basic 05/08, C# 08
    IDE: Express Editions
    Framework: 2.0, 3.0, 3.5


    Lesson 5: Don't take domestic advice from perpetual singles. - Mendhak

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    33

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim lookup As New Dictionary(Of Integer, Integer)
    2.  
    3. lookup.Add(123456, 1)
    4. lookup.Add(123457, 2)
    5. lookup.Add(123435, 3)
    6.  
    7. Dim key as Integer = 123457
    8. Dim value As Integer = lookup(key)
    9.  
    10. MessageBox.Show(value.ToString())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    33

    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.

  7. #7
    Junior Member
    Join Date
    Aug 2007
    Posts
    19

    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:
    1. Dim key As Integer = txtCode.Text
    2. Dim value As String = lookup(key)
    3.  
    4. lblResult.Text = value
    Last edited by Napalm_God; Aug 30th, 2007 at 06:22 AM.

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    33

    Re: Looking for the best way of toing thinks

    Thanks. I found the solution.

    Dim value As Double = lookup(key)

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width