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
Solved

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

Posted on 2006-10-25
7
191 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 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