String indexed collection

Posted on 2006-03-20
Last Modified: 2010-04-23
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.
Question by:Chaffe
    LVL 10

    Expert Comment

    LVL 8

    Assisted Solution

    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.

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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.

    Author Comment

    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?
    LVL 85

    Accepted Solution

    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

    Author Comment

    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.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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?..

    Author Comment

    Thanks Idle_Mind  for the help.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now