Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection Return string 1

Status
Not open for further replies.

aspvbnetnerd

Programmer
May 16, 2006
278
SE
I know that it is possible to send a integer and get the string.

Code:
Imports System.Collections

Dim ht As Hashtable
ht = New Hashtable
ht.Add(1, "HI")
ht.Add(2, "BYE")
MsgBox(ht.Item(1))


I want to send a string and return the ID.
Is this possible?

George
 
I think you need to iterate to find a key from a value. Good Luck!
Code:
Dim key As Integer = -1
For Each de As DictionaryItem In ht
    If de.Value = "YourString" Then
        key = de.Key
        Exit For
    End If
Next 
If key > -1 Then
    ' Value was found so do some processing.
End If
 
Could you submit a example with a full code?
That would be nice.

George
 

What is the use of Collections if one has to iterate a collection to find an item's value! Iterative approach is only good for finding items in Arrays and not in collections.

If you are using .NET 1.0, 1.1 or 2.0 and if you always want to send the string to get the integer than you can use this approach.

Code:
   Dim ht As New Hashtable

   ht.Add("HI", 1)
   ht.Add("BYE", 2)
   MessageBox.Show(ht.Item("BYE"))

If you view the signature of HashTable.Add() methos, you will notice that its both parameters are of type System.Object i.e.

myHashTable.Add(key As Object, value As Object)

Hope it helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top