position in array using a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim

    #1

    position in array using a string

    hi all,

    not quite sure how to ask this, but here goes...

    I have an array of values

    a(0)="SMITH"
    a(1)="JOHN"

    I need to be able to reference each element using a string...

    a("lastname") should return "SMITH"
    a("firstname" ) should return "JOHN"

    in much the same way as referencing a control in a collection of
    controls, or an item in a collection of items.

    any clues on how to achieve this?

    (of course, everything is dynamic. I don't know that "lastname" and
    "firstname" even exist before runtime)

    cheers

  • Larry Lard

    #2
    Re: position in array using a string


    Tim wrote:[color=blue]
    > hi all,
    >
    > not quite sure how to ask this, but here goes...
    >
    > I have an array of values
    >
    > a(0)="SMITH"
    > a(1)="JOHN"
    >
    > I need to be able to reference each element using a string...
    >
    > a("lastname") should return "SMITH"
    > a("firstname" ) should return "JOHN"
    >
    > in much the same way as referencing a control in a collection of
    > controls, or an item in a collection of items.
    >
    > any clues on how to achieve this?[/color]

    The general name for the type of collection that achieves this is
    "dictionary ". A real-world dictionary maps 'words' to 'meanings' - in
    ..NET we say a dictionary maps 'keys' to 'values'. Both keys and values
    can be of any type you like. Here both keys and values would be of type
    String.

    In 1.x (VB2002/3) you could just use a Hashtable, or to enforce strict
    typing derive your own class from DictionaryBase (both in
    System.Collecti ons).

    In 2.0 (VB2005) use Dictionary(Of String,String) from
    System.Collecti ons.Generic.

    --
    Larry Lard
    Replies to group please

    Comment

    • Tim

      #3
      Re: position in array using a string

      dictionary heh (2005), I'll investigate further.

      many thanks.

      any sample code?

      cheers

      Comment

      • Larry Lard

        #4
        Re: position in array using a string


        Tim wrote:[color=blue]
        > dictionary heh (2005), I'll investigate further.
        >
        > many thanks.
        >
        > any sample code?[/color]

        There's an example in the main doc topic for Dictionary(Of K,V) at

        <http://msdn2.microsoft .com/en-us/library/xfhwa508(VS.80) .aspx>

        --
        Larry Lard
        Replies to group please

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: position in array using a string

          Tim,

          As alternative not so nice done

          \\\
          Public Class Test
          Public Shared Sub Main()
          Dim a As New List(Of names)
          a.Add(New names("John", "Smith"))
          MessageBox.Show (a(0).mFirstnam e & " " & a(0).mLastname)
          End Sub
          End Class
          Public Class names
          Public mLastname As String 'should be as properties
          Public mFirstname As String 'should be as properties
          Public Sub New(ByVal firstname As String, _
          ByVal lastname As String)
          mLastname = lastname
          mFirstname = firstname
          End Sub
          End Class
          ///

          I hope this helps,

          Cor


          "Tim" <Citizen10Bears @gmail.com> schreef in bericht
          news:1145948457 .981755.228670@ e56g2000cwe.goo glegroups.com.. .[color=blue]
          > hi all,
          >
          > not quite sure how to ask this, but here goes...
          >
          > I have an array of values
          >
          > a(0)="SMITH"
          > a(1)="JOHN"
          >
          > I need to be able to reference each element using a string...
          >
          > a("lastname") should return "SMITH"
          > a("firstname" ) should return "JOHN"
          >
          > in much the same way as referencing a control in a collection of
          > controls, or an item in a collection of items.
          >
          > any clues on how to achieve this?
          >
          > (of course, everything is dynamic. I don't know that "lastname" and
          > "firstname" even exist before runtime)
          >
          > cheers
          >[/color]


          Comment

          • Tim

            #6
            Re: position in array using a string

            thanks guys, dictionary works a treat. I think this will get a lot of
            use!

            Comment

            Working...