Results 1 to 4 of 4

Thread: Help with array searching

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    6

    Help with array searching

    Hello again I need some help with searching through arrays. Here is what I have to do. I have a list box with names and id numbers. When I choose a persons name and corresponding id number the result is the persons address. The addresses are preloaded into the program. I am having trouble figuring out where to start. Can anyone help me as to where to start in writing the code to start the search?

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Help with array searching

    Can you post the code where you are declaring the array that holds the addresses and where they are "preloaded" ?

    Also, have you considered using a database to store these addresses as it would make things much easier to add/modify new names & addresses in the future
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: Help with array searching

    The easiest way would be to put all the information into a Person object and add the Persons to the combobox. If you override the ToString method (which is what the combobox displays by default), you can have it show the name and id. Then where you add the addresses, you add them to the Person objects and then just grab them from that object when it's selected:

    Code:
    Public Class Person
        private _id as String
        private _name as String
        private _address as String
    
        Public Property ID as String
            Get
                Return _id
            End Get
            Set (Value as String)
                _id = Value
            End Set
        End Property
    
        Public Property Name as String
            Get
                Return _name
            End Get
            Set (Value as String)
                _name = Value
            End Set
        End Property
    
        Public Property Address as String
            Get
                Return _address
            End Get
            Set (Value as String)
                _address = Value
            End Set
        End Property
    
        Public Overrides Function ToString() As String
            Return _name + " - " + _id
        End Function
        
    End Class
    Then to load them into the combobox:

    Code:
    Dim p as new Person
    p.ID = "1"
    p.Name = "Joe Smith"
    p.Address = "123 Main St"
    
    ComboBox1.Items.Add(p)
    Now ComboBox1 will display "Joe Smith - 1"

    To get the person and the corresponding address:

    Code:
    Dim sAddress as String = DirectCast(ComboBox1.SelectedItem, Person).Address
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    6

    Re: Help with array searching

    I am unable to show toe code for declaring the addresses because that code was hidden by my instructor and this vb specific, not dealing with databases. Guess that doesn't help me any further.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width