Solved

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

Posted on 2006-10-25
7
190 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 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

8 Experts available now in Live!

Get 1:1 Help Now