• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

String indexed collection

I'm looking for a tutorial in VB.NET (instead of VB6) on something similar to the project in this link "A Fast Index-Based Collection"
I'm intersted in building a collection with a string index that I can use to store values in it's objects.  Thanks.
2 Solutions
Hi there,

I think you should be able to use a hashtable to achieve what you want.

Look it up in the MSDN.

Hope to help.

Mike TomlinsonMiddle School Assistant TeacherCommented:
You can't access a HashTable by Index though.  It is possible to access the Keys/Values collections by Index, but they are sorted based on the hash value of the Key...not the insertion order.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

ChaffeAuthor Commented:
just to give you a little idea on what I'm tyring to do, I get  a query string in my URL of my web application (?pageid=1) and I use that id do everything in my application.  I got to a point where I have a select case in every function that checks for the PageId.
my goal is to try and get away from all these select cases by creating some sort of collection that I can reference by pageid (similar to a primary key in a sql table) and be able to store some values (such as function names, lables, etc...) by this index.
I considered having a SQL table to store all this and query it on every request, but if I can accomplish the same thing without a trip to the DB, it'll be better.  Any thoughts?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Then I think doobdave was correct after all in his suggestion of a HashTable.

Create a Class to hold the info you want and store that in the HashTable using the pageid as the Key.

Here is a simple example...

    Public Class Dog
        Public Name As String
        Public Breed As String
        Public Naughty As Boolean
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ht As New Hashtable

        Dim d As New Dog
        d.Name = "Fido"
        d.Breed = "Wire Haired Fox Terrier"
        d.Naughty = True
        ht.Add("dog1", d)

        d = New Dog
        d.Name = "Lassie"
        d.Breed = "Labrador"
        d.Naughty = False
        ht.Add("dog2", d)

        d = ht.Item("dog1")
        MsgBox(d.Name & ", " & d.Breed & ", " & d.Naughty)

        d = ht.Item("dog2")
        MsgBox(d.Name & ", " & d.Breed & ", " & d.Naughty)
    End Sub
ChaffeAuthor Commented:
Idle_Mind & doobdave,
Thanks.  I think this is what I'm looking for.  Since I already know what everthing is going to be based on my id, I will be "hardcoding" these properties (.breed, .name, etc...)
Do you think I should have these properties in a structure and have a function inside that structure (similar to the Button1_Click in your example) that would do all the assignments.  And just call this function on every request.  Or is there a better way.
Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't do web based development unfortunately so I don't know the ins and outs of variable scope/lifetime across page requests etc...

Perhaps doobdave has some input on that aspect?..
ChaffeAuthor Commented:
Thanks Idle_Mind  for the help.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now