Results 1 to 29 of 29

Thread: [RESOLVED] Array problem.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [RESOLVED] Array problem.

    I would like to form an array and store the values(Numbers) in it. Then i would also like to assign a letter to each number in an array, so that when i choose the number, i can get its corresponding letter. How can i achieve this.
    Please help me, i dont know anything about arrays and this seems to be a multidimentional one.
    The Pic of what i want to create the array from is below.
    Attached Images Attached Images  
    Last edited by maps; Jul 18th, 2006 at 03:24 AM.

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

    Re: Array problem.

    Don't use multidimensional arrays for that. The Framework provides classes specifically for this type of thing. You would create a new Dictionary(Of Integer, Char) and then add the number/letter pairs to it. The idea of a Dictionary is that you specify a key and it returms the corresponding value.
    VB Code:
    1. Dim myDictionary As New Dictionary(Of Integer, String)
    2.  
    3. myDictionary.Add(-90, "A"c)
    4. myDictionary.Add(-84, "C"c)
    5. '...
    6. myDictionary.Add(84, "Z"c)
    7.  
    8. MessageBox.Show(String.Concat(myDictionary(-8), _
    9.                               myDictionary(-64), _
    10.                               myDictionary(32), _
    11.                               myDictionary(32), _
    12.                               myDictionary(-90), _
    13.                               myDictionary(-48), _
    14.                               myDictionary(-64)) 'Displays "MESSAGE".
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: Array problem.

    Thanks. Let me give it a go.
    BTW. Why did you change your Avatar.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: Array problem.

    Thanks. It worked fine.

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

    Re: Array problem.

    Quote Originally Posted by maps
    Why did you change your Avatar.
    Coz that baby panda is soooooooooo cute! He's cuter at full size but he's still cute at 64x64.
    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
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    By the way...if i choose a value between two numbers, i would like to get a letter corresponding to the number that starts the range. For example if i choose 2, i would like to bring up an answer "N" and if i choose -10, i would like to bring up an answer "M".
    How do i do this?

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

    Re: [RESOLVED] Array problem.

    Gee, that's not a very important detail. There's nothing built into arrays or collections to do something like that. You probably should add items to the dictionary for all those intermediate values as well, so no matter what number you choose it will return a letter. The keys must be unique but the values don't have to be. The other alternative would be to just check every key to find the closest value if you don't get an exact match.
    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    JMC, thanks. For the first suggestion, as you see the dictionery would contain too much as you see the values span from -90 to +84. I would rather go for the alternative approach but still, it wouldnt give what i need since for a value of 7 will give me 8 since its the closest, yet for a value of 7 should give me N.
    Any work around will be highly appreciated.

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

    Re: [RESOLVED] Array problem.

    Quote Originally Posted by maps
    I would rather go for the alternative approach but still, it wouldnt give what i need since for a value of 7 will give me 8 since its the closest, yet for a value of 7 should give me N.
    You what? You look for the closest key and then get the value that corresponds to that key. 8 is the closest key to 7 and the value that corresponds to 8 is N.
    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    8 is P.
    And anything between 8 and 16 should also be P. Anyway, how do i go ahead to get the closest value in anycase.

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

    Re: [RESOLVED] Array problem.

    OK, think outside the square a bit. When I say the "closest value", it's up to you what that means. If you want the closest value that's less than the specified value then use that. This is your requirement so you set the rules.
    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

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    Ok.Thanks. How do i check every Key To get the closest match?

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

    Re: [RESOLVED] Array problem.

    You have been pointed in the right direction. Time to do some leg work yourself. Read the documentation for the Dictionary class and its member listing. Get an understanding of how it works and what members it has. Then you can answer that question yourself. Remember that square.
    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

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    Ha ha...it depends on what is IN the Square.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    Plzz Help. i cant see anything, i cant get going.

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

    Re: [RESOLVED] Array problem.

    You can't see anything? You can't see that the Dictionary class has a property named Keys that is described thusly: Gets a collection containing the keys in the Dictionary. If you can't see that then you haven't looked.
    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

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    i Have seen it. But how can i use it? How can i reference myDictionary here?

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

    Re: [RESOLVED] Array problem.

    Keys is just a collection. If you have a collection of numbers that doesn't contain 7 and you want to find the number closest to 7 that it does contain, what do you suppose you should do? The help topic for the Dictionary.Keys property even has a code example that uses a loop to get each key. Really...
    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

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    Thanks JMC, this is the furthest i have gone. I just dont know how to get a value close to a given value.
    VB Code:
    1. Dim strvalue As String = ""
    2.         Dim keyColl As Dictionary(Of Integer, String).KeyCollection = _
    3.             myDictionery.Keys
    4.  
    5.         If myDictionery.TryGetValue(2, strvalue) Then
    6.             MsgBox("Value Found")
    7.         Else
    8.             MsgBox("Value Not Found")
    9.             For Each i As Integer In keyColl
    10.                 MsgBox(i)
    11.             Next i
    12.  
    13.             Exit Sub
    14.         End If

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    I still need help. Anyone please.
    I just need to know how to get the closest number contained in a collection that is closest to a specified number that is not in the collection.

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

    Re: [NOT YET RESOLVED] Array problem.

    I really think that you're trying to make this difficult when it's extremely easy. The keys are just a series of numbers. How would find the number in a series that was closest to a target number? You'd just make sure they were in numeric order and then check each one until you found the two that were on either side of the target. You can then choose either of those two, depending on whether you want the absolute closest, the closest without going over, the closest without going under, the closest in the direction of zero or whatever. You've got your series of numbers so just use a loop to test each one until you find the two the enclose the target. I suggested a Dictionary before I knew about this requirement. Given that you need to keys in order you should probably use a SortedList instead, which is otherwise almost the same a s a Dictionary. You could have sorted the keys from a Dictionary easily enough but if the collection will do it for you you might as well let it.

    Can you tell me what the index of the number closest to but not greater than 15 is from the following series:

    1,3,6,8,9,12,13,17,19,23

    If you can then you know how to solve your current issue.
    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

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [NOT YET RESOLVED] Array problem.

    Quote Originally Posted by jmcilhinney
    the index of the number closest to 15 from the following series:

    1,3,6,8,9,12,13,17,19,23

    If you can then you know how to solve your current issue.
    JMC..now this is my problem.
    This is what i have so far.
    VB Code:
    1. Private mySortedList As New SortedList(Of Integer, String)
    2.         mySortedList.Clear()
    3.           With mySortedList
    4.             .Add(-90, "A"c)
    5.             .Add(-84, "C"c)
    6.             .Add(-72, "D"c)
    7.             .Add(-64, "E"c)
    8.             .Add(-56, "F"c)
    9.             .Add(-48, "G"c)
    10.             .Add(-40, "H"c)
    11.             .Add(-32, "J"c)
    12.             .Add(-24, "K"c)
    13.             .Add(-16, "L"c)
    14.             .Add(-8, "M"c)
    15.             .Add(0, "N"c)
    16.             .Add(8, "P"c)
    17.             .Add(16, "Q"c)
    18.             .Add(24, "R"c)
    19.             .Add(32, "S"c)
    20.             .Add(40, "T"c)
    21.             .Add(48, "U"c)
    22.             .Add(56, "V"c)
    23.             .Add(64, "W"c)
    24.             .Add(72, "X"c)
    25.             .Add(84, "Z"c)
    26.         End With
    27.  
    28.  For Each key As String In mySortedList.Keys
    29.             If mySortedList.Keys(mySortedList.IndexOfKey(key)) < 16 Then
    30.               'Show value "P" as the answer.  
    31.               'mySortedList(key)                  
    32.             End If
    33.  Next
    Last edited by maps; Jul 18th, 2006 at 02:35 AM.

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

    Re: [NOT YET RESOLVED] Array problem.

    *sighs heavily*
    VB Code:
    1. Dim mySortedList As New SortedList(Of Integer, Char)
    2.  
    3. With mySortedList
    4.     .Add(-90, "A"c)
    5.     .Add(-84, "C"c)
    6.     .Add(-72, "D"c)
    7.     .Add(-64, "E"c)
    8.     .Add(-56, "F"c)
    9.     .Add(-48, "G"c)
    10.     .Add(-40, "H"c)
    11.     .Add(-32, "J"c)
    12.     .Add(-24, "K"c)
    13.     .Add(-16, "L"c)
    14.     .Add(-8, "M"c)
    15.     .Add(0, "N"c)
    16.     .Add(8, "P"c)
    17.     .Add(16, "Q"c)
    18.     .Add(24, "R"c)
    19.     .Add(32, "S"c)
    20.     .Add(40, "T"c)
    21.     .Add(48, "U"c)
    22.     .Add(56, "V"c)
    23.     .Add(64, "W"c)
    24.     .Add(72, "X"c)
    25.     .Add(84, "Z"c)
    26. End With
    27.  
    28. Dim targetKey As Integer = 44 'Find the key closest to but not greater than 44.
    29. Dim lastKey As Integer
    30.  
    31. For Each key As Integer In mySortedList.Keys
    32.     If key > target Then
    33.         'This key is greater than the target so the previous key is the one we want.
    34.         Exit For
    35.     Else
    36.         'This key is still less than the target.
    37.         lastValue = key
    38.     End If
    39. Next key
    40.  
    41. MessageBox.Show(String.Format("The key closest to but not greater than {0} is {1}.", _
    42.                               target, _
    43.                               lastValue))
    44. MessageBox.Show(String.Format("The value that corresponds to {0}, and therefore also {1}, is {2}.", _
    45.                               lastValue, _
    46.                               target, _
    47.                               mySortedList(lastValue)))
    You could speed that up using a binary search but if straight iteration was such a chore let's not even go there.
    Last edited by jmcilhinney; Jul 18th, 2006 at 03:08 AM.
    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

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [NOT YET RESOLVED] Array problem.

    Yah...i know that was a bitter pill for you. But Thanks A Million, you got some ignorance out of me.
    Last edited by maps; Jul 18th, 2006 at 03:31 AM.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [NOT YET RESOLVED] Array problem.

    JMC....i have been going through how to implement a binarysearch. I came up with this code which uses a binarysearch from a one dimentional array.
    i am just curious how we can use this kind of search over a sortedlist.
    VB Code:
    1. Dim myNumbers() As Integer = {0, 2, 4, 6, 8, 10, 12}
    2.         Array.Sort(myNumbers)
    3.         Dim i As Integer
    4.         Dim target As Integer = 7
    5.         Dim NextNo As Integer
    6.         For Each i In myNumbers
    7.             If i > target Then
    8.                 NextNo = i
    9.                 Exit For
    10.             Else
    11.             End If
    12.         Next i
    13.         MessageBox.Show(myNumbers.GetValue(Array.BinarySearch(myNumbers, NextNo)))
    Last edited by maps; Jul 19th, 2006 at 07:23 AM.

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

    Re: [RESOLVED] Array problem.

    The Keys and Values properties are just collections themselves that can be treated in most cases as though they were 1D arrays, so basically you'd do it exactly the same way. Once you have the key you want you index the SortedList by that key to get the corresponding value.
    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

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    You mean like this! It doesnt work.
    VB Code:
    1. Dim mySortedList As New SortedList(Of Integer, Char)
    2.  
    3. With mySortedList
    4.     .Add(-90, "A"c)
    5.     .Add(-84, "C"c)
    6.     .Add(-72, "D"c)
    7.     .Add(-64, "E"c)
    8.     .Add(-56, "F"c)
    9.     .Add(-48, "G"c)
    10.     .Add(-40, "H"c)
    11.     .Add(-32, "J"c)
    12.     .Add(-24, "K"c)
    13.     .Add(-16, "L"c)
    14.     .Add(-8, "M"c)
    15.     .Add(0, "N"c)
    16.     .Add(8, "P"c)
    17.     .Add(16, "Q"c)
    18.     .Add(24, "R"c)
    19.     .Add(32, "S"c)
    20.     .Add(40, "T"c)
    21.     .Add(48, "U"c)
    22.     .Add(56, "V"c)
    23.     .Add(64, "W"c)
    24.     .Add(72, "X"c)
    25.     .Add(84, "Z"c)
    26. End With
    27.  
    28. Dim targetKey As Integer = 44 'Find the key closest to but not greater than 44.
    29. Dim lastKey As Integer
    30.  
    31. For Each key As Integer In mySortedList.Keys
    32.     If key > target Then
    33.         'This key is greater than the target so the previous key is the one we want.
    34.         Exit For
    35.     Else
    36.         'This key is still less than the target.
    37.         lastValue = key
    38.     End If
    39. Next key
    40. Messagebox.Show(mySortedList.Item(Array.BinarySearch(mySortedList.Keys,lastValue)))'<-----Unable to cast object of type KeyList to type system.array

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

    Re: [RESOLVED] Array problem.

    Ok, I didn't really read your previous post properly. It would stand to reason that you cannot use Array.BinarySearch to perform a binary search on something that is not an array. I just quickly read the words "implement a binarysearch" and I assumed that you had actually implemented a binary search yourself. If you want to implement a binary search you have two options. Firstly you could copy the contents of the Keys property to an array and then use Array.BinarySearch on that. After all this back and forth you obviously know how important it is to read the documentation, so I know that you have read about the CopyTo method of the Keys property. Your second option is to implement your own binary search. It's quite easy to do. You just keep cuttin the list in half. You start in the middle and then your new list becomes the half that contains the target. You then go to the middle of that list and your new list becomes the half of that that contains the target. You just keep cuttin the list in half until you either find the target or you end up with two items side by side that the target would fall between. Those would be the same last two items that you would have found the other way but generally you will get to them more quickly this way.
    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

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Array problem.

    Ok. Thanks a great deal.

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