Solved

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

Posted on 2006-10-25
7
188 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 62

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

13 Experts available now in Live!

Get 1:1 Help Now