• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

How can I create and use pair lists in VB.

How can I create paired lists in VB?
for example
myList menu
"cereal", "$4.95",
"soup", "$2.50",
"soda", "$0.99"
so that:
menu("cereal") would return "$4.95"
1 Solution
I'd do it differently depending on what it's for. If it's based on user interaction with a ListBox or ComboBox, for instance, the easiest way would be to associate each text string in the control with a value in the control's ItemData, giving you a way to immediately access the paired value using the ItemData to store the appropriate index number of an array containing the matching values.

I'll assume that's not the case, and further that it is a list that will be modified at runtime or for another reason shouldn't be hard-coded into your application. Your remaining choices, as I see them, are to either store the values in an array or in a database. Either is fairly easy, I'd make the decision based on the size of your list. If it's a few hundred or less, an array will be fine. Any more than that and searching the array may start to take some time; you'll need to go to a database for faster searches.

To store the values in the array, create either one two-dimensional array or two one-dimensional arrays and add your values normally. Then create a function that searches through the values to find the index number of the value you are looking for and then return the value that matches it.

For instance:

Dim gMyArray() as String  'global variable

Sub InitializeArray()
    Dim gMyArray(1 to 2, 1 to 1)
    gMyArray(1,1) = "Item Name"
    gMyArray(2,1) = "Item Cost"
End Sub

Sub addItem(nameIn as String, costIn as String)
    Redim Preserve gMyArray(1 to 2, 1 to Ubound(gMyArray, 2) + 1)
    gMyArray(1, Ubound(gMyArray, 2)) = nameIn
    gMyArray(2, Ubound(gMyArray, 2)) = costIn
End Sub

Function returnCost(nameIn as String) as String
    Dim tmpInt as Integer 'this will work as long as the number of elements is a number that can fit in an Integer
    returnCost = ""
    For tmpInt = 1 to Ubound(gMyArray, 2)
        If gMyArray(1, tmpInt) = nameIn Then
            returnCost = gMyArray(2, tmpInt)
            Exit Function
        End if
    Next tmpInt
End Function

In this code you'll want to be sure not to use empty strings for the "cost" side of the variable, or you'll need to use a different return value for returnCost when it does not find a matching string. You'll want to trap for whatever that value is using whatever error trapping your program has. Also, you'll want to make sure that any added names don't already exist. To do that, you could simply add a line in the addItem routine that calls returnCost and exits with an error if it doesn't receive an empty string.

onamelessoneAuthor Commented:
Thanks a lot.  That's exactly what I needed.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now