Click here to Skip to main content
15,890,512 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
satc18-Jun-15 19:46
satc18-Jun-15 19:46 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
Dave Kreskowiak19-Jun-15 2:25
mveDave Kreskowiak19-Jun-15 2:25 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
satc19-Jun-15 11:33
satc19-Jun-15 11:33 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
Dave Kreskowiak19-Jun-15 12:52
mveDave Kreskowiak19-Jun-15 12:52 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
satc19-Jun-15 14:51
satc19-Jun-15 14:51 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
Dave Kreskowiak19-Jun-15 16:14
mveDave Kreskowiak19-Jun-15 16:14 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
satc19-Jun-15 16:49
satc19-Jun-15 16:49 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
Dave Kreskowiak19-Jun-15 17:26
mveDave Kreskowiak19-Jun-15 17:26 
Man you make this difficult. "Just tell me how to do this" without ever explaining why you think you need to do it.

No, a Dictionary<> can't be indexed like that. As you already know, a List<> can. The solution is to make your own List<> class, inheriting from List<KeyValuePair<TKey, TValue>>. You're going to have to implement your own Keys property to expose a Contains for it. Also, you need to expose your own Add methods to add values to the collection. It's really very simple.
Public Class KeyValueList(Of TKey, TValue)
	Inherits List(Of KeyValuePair(Of TKey, TValue))

	Private _keys As New List(Of TKey)()
	Private _values As New List(Of TValue)()

	Public ReadOnly Property Keys() As List(Of TKey)
		Get
			Return _keys
		End Get
	End Property

	Public Overridable Sub Add(key As TKey, value As TValue)
		_keys.Add(key)
		_values.Add(value)

		MyBase.Add(New KeyValuePair(Of TKey, TValue)(key, value))
	End Sub

	Public Shadows Sub Add(value As KeyValuePair(Of TKey, TValue))
		_keys.Add(value.Key)
		_values.Add(value.Value)

		MyBase.Add(value)
	End Sub
End Class


You use this class instead of the List<KeyValuePair<TKey, TValue>> you used. Since it inherits from List<>, it automatically exposes the index property and you get Keys and Values collections that are also indexed and support Contains since they are Lists too.
A guide to posting questions on CodeProject

Click this: Asking questions is a skill.
Seriously, do it.

Dave Kreskowiak

GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
satc19-Jun-15 18:05
satc19-Jun-15 18:05 
GeneralRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
Dave Kreskowiak20-Jun-15 4:52
mveDave Kreskowiak20-Jun-15 4:52 
AnswerRe: A short way to detect if a list of KeyPairValues contains a specific Key Pin
Richard Deeming19-Jun-15 1:52
mveRichard Deeming19-Jun-15 1:52 
Questionvb.net 2010 datepicker Pin
sc steinhayse18-Jun-15 7:05
sc steinhayse18-Jun-15 7:05 
AnswerRe: vb.net 2010 datepicker Pin
Ralf Meier18-Jun-15 8:05
mveRalf Meier18-Jun-15 8:05 
AnswerRe: vb.net 2010 datepicker Pin
Dave Kreskowiak18-Jun-15 9:28
mveDave Kreskowiak18-Jun-15 9:28 
QuestionMimic a Undo/Redo on a VB.net + Database application Pin
satc18-Jun-15 0:41
satc18-Jun-15 0:41 
AnswerRe: Mimic a Undo/Redo on a VB.net + Database application Pin
Eddy Vluggen18-Jun-15 1:04
professionalEddy Vluggen18-Jun-15 1:04 
AnswerRe: Mimic a Undo/Redo on a VB.net + Database application Pin
Sascha Lefèvre18-Jun-15 1:30
professionalSascha Lefèvre18-Jun-15 1:30 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
Richard Deeming18-Jun-15 3:27
mveRichard Deeming18-Jun-15 3:27 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
Dave Kreskowiak18-Jun-15 4:20
mveDave Kreskowiak18-Jun-15 4:20 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
satc18-Jun-15 10:24
satc18-Jun-15 10:24 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
Sascha Lefèvre18-Jun-15 11:18
professionalSascha Lefèvre18-Jun-15 11:18 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
Dave Kreskowiak18-Jun-15 12:22
mveDave Kreskowiak18-Jun-15 12:22 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
satc18-Jun-15 12:45
satc18-Jun-15 12:45 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
Dave Kreskowiak18-Jun-15 13:19
mveDave Kreskowiak18-Jun-15 13:19 
GeneralRe: Mimic a Undo/Redo on a VB.net + Database application Pin
satc18-Jun-15 14:35
satc18-Jun-15 14:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.