473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Multi-indexed collection


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

Nov 21 '05 #1
2 1257
A quick and dirty way would be to create 3 hash tables. One for each value
that you effectively want indexed.

Scott Swigart
blog.swigartconsulting.com
www.swigartconsulting.com

"Samuel R. Neff" wrote:

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

Nov 21 '05 #2
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.

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.