Solved

How can I create and use pair lists in VB.

Posted on 2003-10-21
2
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

688 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