Results 1 to 3 of 3

Thread: [RESOLVED] Composite key with class collection

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Resolved [RESOLVED] Composite key with class collection

    I want to be able to get an instance of a class based on two fields in that class. I was hoping to use something like a dictionary, because I thought indexing would be the best for performance. Should I just use LINQ or go back to a strongly typed dataset? Here's an example of what I'm trying to do:

    Code:
    Dim n As New Name("wild", "bill")
    
    Dim dict As New Dictionary(Of String(), Name)
    dict.Add(New String() {"wild", "bill"}, n)
    
    Dim key() = New String() {"wild", "bill"}
    Debug.WriteLine(dict.ContainsKey(key)) 'false
    
    ...
    
    Public Class Name
        Public FirstName As String
        Public LastName As String
        Public NameCount As Integer
    
        Public Sub New(ByVal first As String, ByVal last As String)
            Me.FirstName = first
            Me.LastName = last
        End Sub
    End Class
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Composite key with class collection

    Using a Dictionary would probably perform better but that would only really be a benefit if you are accessing the collection a lot or it is very large. If you are going to use a Dictionary then I would suggest against using a String array as the key. I would suggest defining a type specifically for the purpose with two String properties and its own GetHashCode function override based on those two properties.

    A simpler option would be to just use a List and a LINQ query, e.g.
    vb.net Code:
    1. Dim name = names.FirstOrDefault(Function(n) n.FirstName = firstName AndAlso n.LastName = lastName)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Composite key with class collection

    Thanks for the input, I went ahead and used LINQ. My collection will typically never be over 1,000 items.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

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