Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can iterate in Dictionary in vb.net?

I create a dictionary

    Dim ImageCollection As New Dictionary(Of ConvensionImages, Integer) 

and I fill that

 For Each dr As DataRow In dt.Rows             Dim obj As New ConvensionImages             obj.ImageID = dr("ID")             obj.Name = dr("Name")             obj.Description = dr("Description")             obj.CategoryID = dr("CategoryID")             obj.CategoryName = dr("CategoryName")             obj.CategoryDescription = dr("CatDescription")             obj.EventID = dr("EventID")             obj.Image = dr("img")             obj.DownloadImage = dr("DownLoadImg")             ImageCollection.Add(obj, key)             key = key + 1 

now I want to search ImageID and key how can I do this

like image 420
Andy Avatar asked Sep 05 '13 06:09

Andy


People also ask

How Do We iterate through a dictionary?

To iterate through the dictionary's keys, utilise the keys() method that is supplied by the dictionary. An iterable of the keys available in the dictionary is returned. Then, as seen below, you can cycle through the keys using a for loop.

What is dictionary in VB net?

How to VB.Net Dictionary. A Dictionary class is a data structure that represents a collection of keys and values pair of data. Each item is a combination of a key and a value. ( key-value pair)


1 Answers

Make Integer as key for your Dictionary:

Dim ImageCollection As New Dictionary(Of Integer, ConvensionImages) 

Change ImageCollection.Add(obj, key) to ImageCollection.Add(key, obj)

And use this loop:

For Each kvp As KeyValuePair(Of Integer, ConvensionImages) In ImageCollection      Dim v1 As Integer = kvp.Key        Dim v2 As ConvensionImages = kvp.Value        'Do whatever you want with v2:      'If v2.ImageID = .... Then Next   
like image 186
Andrey Gordeev Avatar answered Sep 20 '22 18:09

Andrey Gordeev