I have written this function to auto correct gender to M or F from different values in a string array. It works fine but my manager told me to use Dictionary which he said is more efficient. But I have no idea. Anyone like to help me to understand how this can be done ? Thanks.
Public Function AutoGender(ByVal dt As DataTable) As DataTable
Dim Gender As String = ""
Dim Mkeywords() As String = {"boy", "boys", "male", "man", "m", "men", "guy"}
Dim Fkeywords() As String = {"girl", "girls", "female", "woman", "f", "women", "chick"}
Dim row As DataRow
For Each row In dt.Rows
If Mkeywords.Contains(row("Gender").ToString.ToLower) Then
Gender = "M"
row("Gender") = Gender
ElseIf Fkeywords.Contains(row("Gender").ToString.ToLower) Then
Gender = "F"
row("Gender") = Gender
End If
Next
Return dt
End Function
Here is an example how you could implement the
Dictionary(Of String, String)to lookup whether this synonym is known or not:Note that i’ve used the collection initializer to fill the Dictionary that is a convenient way to use literals to initialize collections. You could also use the
Addmethod.Edit: Just another approach that might be more concise is using two
HashSet(Of String), one for the male synonyms and one for the female:A
HashSetmust also be unique, so it cannot contain duplicateStrings(like the key in theDictionary), but it’s not a key-value pair but only a set.