?
Solved

How can I create and use pair lists in VB.

Posted on 2003-10-21
2
Medium Priority
?
252 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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