Multi-indexed collection

S

Samuel R. Neff

What's the best way to create a collection where a value can be looked
up by multiple keys?

Say I have a custom object,

Class Whatever
Public A As String
Public B As String
Public C As String
End Class

What is the best way to add this object to a collection such that I
can look up an instance of Whatever by any value, A, B, or C?

TA custom collection class backed by three hashtables?

I don't want to use a DataSet/DataTable because the real object is
quite a bit more complex with dozens or properties, nested objects,
and nested collections of custom objects--wouldn't map well to a data
table row.

The order of objects doesn't matter--they're never referenced by
numeric index, only by a key value.

Thanks,

Sam
 
P

Peter Huang [MSFT]

Hi

In addition to Scott's suggestion, you may try to add the reference to the
object in the hashtable, so that the three reference will pointed to one
object.
e.g.

Public Class Test
Public i As Integer
End Class

Module Module1
Sub Main()
Dim o As New Test
Dim a As Test
Dim b As Test
a = o
b = o
Dim ht As New Hashtable
ht.Add("A", a)
ht.Add("B", b)
ht.Add("C", o)

Console.WriteLine(CType(ht("A"), Test).i)
Console.WriteLine(CType(ht("B"), Test).i)
Console.WriteLine(CType(ht("C"), Test).i)
o.i += 1
Console.WriteLine(CType(ht("A"), Test).i)
Console.WriteLine(CType(ht("B"), Test).i)
Console.WriteLine(CType(ht("C"), Test).i)
End Sub

End Module

You may have a try.


Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top