Results 1 to 3 of 3

Thread: Simple Code lookup to get Description

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    50

    Simple Code lookup to get Description

    Simple Code lookup to get Description

    I have a static list of Street Type Abbreviations and their associated Street Type Descriptions: RD to Road. I have the complete list needed. There are 37 elements in the list - 37 Street Type Abbreviations with their associated Descriptions.

    The lookup will only happen once. The input lookup field is in a single field: stType.

    Not trying to get fancy. but what is the best approach? Dictionary, List, two dimensional array, etc. Started with Dictionary but the .Add was getting redundant. Two dimensional array let me initialize the values, but searching an old school loop.

    What do you think?

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Simple Code lookup to get Description

    You could place your information into an XML file then read the data back using LINQ into a dictionary for each list.

    So the following does not have your specs but instead is a template for consideration.

    Example for XML
    file name Codes.xml
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <Codes>
       <Code ID="1" Name="One" />
       <Code ID="2" Name="Two"/>
       <Code ID="3" Name="Three"/>
       <Code ID="4" Name="Four"/>
       <Code ID="5" Name="Five"/>
       <Code ID="6" Name="Six"/>
       <Code ID="7" Name="Seven"/>
    </Codes>
    Then using the following code read the data into a Dictionary

    Code:
       Public Sub DemoCodeInDict()
          Dim Doc As New XDocument
          Doc = XDocument.Load("Codes.xml")
    
          Dim Dict = ( _
              From Item In Doc...<Code> _
              Let name = Item.@Name _
              Order By name _
              Select New With _
                { _
                   Key .ID = Item.@ID, _
                   Key .Name = name}).ToDictionary( _
                      Function(a) a.ID, Function(a) a.Name)
    
          For Each C In Dict
             Console.WriteLine("{0}={1}", C.Key, C.Value)
          Next
    
          Console.WriteLine()
          If Dict.ContainsKey("7") Then
             Console.WriteLine("Value for 7 is [{0}]", Dict.Item("7"))
          End If
          If Dict.ContainsKey("22") Then
             Console.WriteLine("Value for 7 is [{0}]", Dict.Item("22"))
          Else
             Console.WriteLine("Key 22 does not exists")
          End If
       End Sub

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Simple Code lookup to get Description

    I would say that the dictionary is the most "proper" way, and I like the route KI took, but it really comes down to personal preference. The use of a 2D array would be constraining, but if the list never changes in size, then the constraint wouldn't matter all that much. Accessing the items in an array would require a loop, so the dictionary would be more consise, but the speed would probably be about the same.
    My usual boring signature: Nothing

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