Solved

Getting a Key (the String value) out of a HashTable

Posted on 2006-10-25
7
194 Views
Last Modified: 2010-04-23
Friends,

Okay, I am trying to get the String Value of a key out of a HashTable, so that I can find if it needs to be removed.

Here is the For Loop that I have created, and the logic with it.  I need to know what needs to be inserted where the ??? marks are, please.


                    For Each DriverEntrantHT.??? In DriverEntrantHT

                        SearchOnKey = DriverEntrantHT.??? 'The String Value of the Key
                        SplitUpKey = Split(SearchOnKey, "-")

                        DriverEntrantHT_CarNumberTemp = SplitUpKey(0)
                        DriverEntrantHT_FirstNameTemp = SplitUpKey(1)
                        DriverEntrantHT_LastNameTemp = SplitUpKey(2)

                        If e_CarNumber = DriverEntrantHT_CarNumberTemp And _
                        ((e_Driver_FirstName = DriverEntrantHT_FirstNameTemp) Or (e_Driver_FirstName = DriverEntrantHT_FirstNameTemp)) Then
                            DriverEntrantHT.Remove(SearchOnKey)
                        Else
                            DriverEntrantKey = (e_CarNumber & "-" & e_Driver_FirstName & "-" & e_Driver_LastName)

                            DriverEntrantProperty.FirstName = e_Driver_FirstName
                            DriverEntrantProperty.LastName = e_Driver_LastName
                            ' Add info to Hashtable
                            DriverEntrantHT.Add(DriverEntrantKey, DriverEntrantProperty)
                        End If

                    Next

Thanks in advance!
Regards,
Eric
   
If you would like to see the rest of the code, and where this for loop is in the code, it is below:

    Dim DriverEntrantHT As New Hashtable
    Dim oDriverEntrant As DriverEntrant

Public Sub ProcessValidString(ByVal AMBMLPValidString As String) 'Handles RecieveValidAMBMLPString.ValidAMBMLPString

        Dim SplitUpString() As String
        SplitUpString = Split(AMBMLPValidString, "¦")

        If SplitUpString.GetUpperBound(0) = 0 Then
            Exit Sub
        End If

        Dim Record As String
        Record = SplitUpString(0)
        'Split()
        Dim CarNumberKey As Integer

        Dim RecordType As String
        RecordType = SplitUpString(1)

        Dim DriverEntrantKey As String = Nothing
        Select Case Record

            Case "E"

                Dim DriverEntrantProperty As New DriverEntrant

                If RecordType = "N" Or "R" Then
                    'If CarNumber doesn't exist in array, add (3) values to array as record
                    e_CarNumber = AMBParser_Entrant.GetValue(AMBMLPValidString, 4) 'Just know that this returns the CarNumber of this particular record...
                    e_Driver_FirstName = AMBParser_Entrant.GetValue(AMBMLPValidString, 61)
                    e_Driver_LastName = AMBParser_Entrant.GetValue(AMBMLPValidString, 62)


                    DriverEntrantKey = (e_CarNumber & "-" & e_Driver_FirstName & "-" & e_Driver_LastName)

                    CarNumberExistsInList = False
                    If DriverEntrantHT.ContainsKey(DriverEntrantKey) Then
                        CarNumberExistsInList = True
                    End If

                    If CarNumberExistsInList = False Then


                        DriverEntrantProperty.FirstName = e_Driver_FirstName
                        DriverEntrantProperty.LastName = e_Driver_LastName

                        ' Add info to Hashtable
                        DriverEntrantHT.Add(DriverEntrantKey, DriverEntrantProperty)

                    End If

                ElseIf RecordType = "U" Then

                    Dim SearchOnKey As String
                    Dim SplitUpKey() As String

                    Dim DriverEntrantHT_CarNumberTemp As String
                    Dim DriverEntrantHT_FirstNameTemp As String
                    Dim DriverEntrantHT_LastNameTemp As String

                    e_CarNumber = AMBParser_Entrant.GetValue(AMBMLPValidString, 4) 'Just know that this returns the CarNumber of this particular record...
                    e_Driver_FirstName = AMBParser_Entrant.GetValue(AMBMLPValidString, 61)
                    e_Driver_LastName = AMBParser_Entrant.GetValue(AMBMLPValidString, 62)

                    For Each DriverEntrantHT.??? In DriverEntrantHT 'This is the aforementioned for loop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                        SearchOnKey = DriverEntrantHT.???
                        SplitUpKey = Split(SearchOnKey, "-")

                        DriverEntrantHT_CarNumberTemp = SplitUpKey(0)
                        DriverEntrantHT_FirstNameTemp = SplitUpKey(1)
                        DriverEntrantHT_LastNameTemp = SplitUpKey(2)

                        If e_CarNumber = DriverEntrantHT_CarNumberTemp And _
                        ((e_Driver_FirstName = DriverEntrantHT_FirstNameTemp) Or (e_Driver_FirstName = DriverEntrantHT_FirstNameTemp)) Then
                            DriverEntrantHT.Remove(SearchOnKey)
                        Else
                            DriverEntrantKey = (e_CarNumber & "-" & e_Driver_FirstName & "-" & e_Driver_LastName)

                            DriverEntrantProperty.FirstName = e_Driver_FirstName
                            DriverEntrantProperty.LastName = e_Driver_LastName
                            ' Add info to Hashtable
                            DriverEntrantHT.Add(DriverEntrantKey, DriverEntrantProperty)
                        End If

                    Next

                End If
0
Comment
Question by:indy500fan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 500 total points
ID: 17803491
You can either loop using DictionaryEntry as your object type:

      For Each DriverEntrant As DictionaryEntry In DriverEntrantHT
          'Code    
      Next

OR you can use an Enumerator:

      Dim iEnum As IEnumerator = DriverEntrantHT.GetEnumerator
      Dim DriverEntrant As DictionaryEntry

      While iEnum.MoveNext
            DriverEntrant = CType(iEnum, DictionaryEntry)
                'Code
      End While
0
 

Author Comment

by:indy500fan
ID: 17803555
ZeonFlash,

With regards to the first example what would i set SearchOnKey equal to?

For Each DriverEntrant as DictionaryEntry

       SearchOnKey = DriverEntrantHT.???

Thanks,

Eric
0
 

Author Comment

by:indy500fan
ID: 17803604
SearchOnKey = DriverEntrant.ToString  ???
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17804445
The DictionaryEntry data type has the properties Key and Value, which are the same properties you used to create the entry in the hashtable.  So you would use:

For Each DriverEntrant as DictionaryEntry In DriverEntrantHT

       SearchOnKey = DriverEntrant.Key
0
 

Author Comment

by:indy500fan
ID: 17804462
Ah, thanks ZeonFlash!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17804882
Hi indy500fan;

I went through the first part of the code, to remove an Item from the HashTable. The code now has the correct syntax but has a logic error in it.

1. To be able to remove an item from the HashTable the collection of items cannot be in use. In other words you can not use the DriverEntrantHT in the for statement to get a copy of an object in the HashTable. So we need to make a copy of the DriverEntrantHT and use that.

2. The logic error, if the values in e_Driver_FirstName, e_CarNumber and e_Driver_LastName do not change in each iteration of the loop then one part of the if statement will remove and entry and on the next loop will add it back but then if there are any entries left in the HashTable the next loop will throw an error because it will try to add it again and that is not allowed.

        Dim TempHT As Hashtable = CType(DriverEntrantHT.Clone, Hashtable)
        For Each obj As DictionaryEntry In TempHT
            SplitUpKey = Split(obj.Key.ToString(), "-")

            DriverEntrantHT_CarNumberTemp = SplitUpKey(0)
            DriverEntrantHT_FirstNameTemp = SplitUpKey(1)
            DriverEntrantHT_LastNameTemp = SplitUpKey(2)

            If e_CarNumber = DriverEntrantHT_CarNumberTemp And _
            ((e_Driver_FirstName = DriverEntrantHT_FirstNameTemp) Or _
            (e_Driver_FirstName = DriverEntrantHT_FirstNameTemp)) Then
                DriverEntrantHT.Remove(obj.Key.ToString())
            Else
                DriverEntrantProperty = New DriverEntrant
                DriverEntrantKey = (e_CarNumber & "-" & e_Driver_FirstName & _
                    "-" & e_Driver_LastName)
                DriverEntrantProperty.CarNumber = e_CarNumber
                DriverEntrantProperty.FirstName = e_Driver_FirstName
                DriverEntrantProperty.LastName = e_Driver_LastName
                ' Add info to Hashtable
                DriverEntrantHT.Add(DriverEntrantKey, DriverEntrantProperty)
            End If
        Next

Fernando
0
 

Author Comment

by:indy500fan
ID: 17804914
Thanks Fernando.  I've added it into my code, and as soon as I can get my Event Handles, Handler, etc. working, I can test this!

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

623 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