Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-10-25
7
Medium Priority
?
195 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 64

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

718 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