Solved

How can I create and use pair lists in VB.

Posted on 2003-10-21
2
246 Views
Last Modified: 2010-05-03
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"
0
Comment
Question by:onamelessone
2 Comments
 
LVL 1

Accepted Solution

by:
PuckJunkie earned 50 total points
ID: 9594105
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.

Puck
0
 

Author Comment

by:onamelessone
ID: 9599937
Thanks a lot.  That's exactly what I needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now