开发者

How can I get a hashtable key name from a value in VB.NET?

I have a hashtable in VB.NET and I need 开发者_JAVA技巧to get the string value of a key from it's value. For example, if I do:

hashtable.add("string1","string2")

How would I get the value "string1" if I had "string2"?


You can't (at least not without simply looping through every value). Consider the fact that multiple keys can map to the same value:

hashtable.Add("string1", "string2")
hashtable.Add("string3", "string2")

Now given "string2" what would you expect to be returned?

If you really need to do a "reverse" lookup, then the simplest solution is to probably have two hashtable, one for the "forward" lookup and one for the "reverse" lookup.


As Dean / codeka says you can't strictly do this.

However you can do something like this as the Keys and Values of a Hashtable are in the same (unspecified) order:

Hashtable ht = new Hashtable();
ht.Add("string1", "str2");
ht.Add("string2", "str2");

List<string> keys = new List<string>(ht.Keys.OfType<string>());

string key = ht.Values.OfType<string>()
  .Select((htI, i) => new { Key = keys[i], Value = htI })
  .Where(htKVP => htKVP.Value == "str2")
  .Select(htKVP => htKVP.Key)
  .FirstOrDefault();

However, you would be better using a Dictionary<string, string> just because it is generically typed and lets you get to Linq easier.

Converted for VB.NET that is:

Dim ht as new Hashtable()
ht.Add("string1", "str2")
ht.Add("string2", "str2")

Dim keys as new List(Of String)(ht.Keys.OfType(Of String)())

Dim key as String = ht.Values.OfType(Of String)() _
  .Select(Function(htI, i) New With { .Key = keys(i), .Value = htI }) _
  .Where(Function(htKVP) htKVP.Value = "str2") _
  .Select(Function(htKVP) htKVP.Key) _
  .FirstOrDefault()

But again I'd start with:

Dim dt as New Dictionary(Of String, String)

You could add this as an extension method like so:

Imports System.Runtime.CompilerServices

Module StringExtensions

    <Extension()> 
    Public Function FirstKeyForValue(ByVal Hashtable as ht, ByVal value As String) As String
      return ht.Values.OfType(Of String)() _
      .Select(Function(htI, i) New With { .Key = keys(i), .Value = htI }) _
      .Where(Function(htKVP) htKVP.Value = "str2") _
      .Select(Function(htKVP) htKVP.Key) _
      .FirstOrDefault()        
    End Function

End Module


There is a much easier way than Matt's answer. You can perform linq on a hashtable. Of course the below sample code can be modified to use the proper types used in your Hashtable variable as both Key and Value can be any type:

Public Class HashtableTest
    Private Lookup As New Hashtable
    
    Private Sub New()
        Lookup("Key1") = "Value1"
        Lookup("Key2") = "Value2"
    End Sub
    
    Public Sub Test()
        MsgBox(GetKey("Value2"))
    End Sub
    
    Public Function GetKey(Value As String) As String
        Dim FoundKey As String = ""
        
        If Lookup.ContainsValue(Value) Then
            FoundKey = (From Key As String In Lookup.Keys.Cast(Of String) Where Lookup(Key).ToString() = Value Select Key).FirstOrDefault()
        End If
    
        Return FoundKey
    End Function
End Class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜