Solved

How can I create and use pair lists in VB.

Posted on 2003-10-21
2
245 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 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

16 Experts available now in Live!

Get 1:1 Help Now