[RESOLVED] Store Objects with Key in some sort of Collection/Dictionary
I need to Store Objects with Key like:
Key: <Fontname & FontSize> / Object: <Font Object>
Example content:
Code:
ArialRegular8 | <Corresponding Font Object>
ImpactItalic10 | <Corresponding Font Object>
TimesRomanRegular12 | <Corresponding Font Object>
These font types come from an external library, the real type is DocumentFont, I prefer not to store just values and recreate the font object later, so I need to store the object, and i want to be able to call Contains() later without looping.
I see some possible solutions in the help, like for example Dictionary (of T Key, T Value), is this the best option? Also, to be able to call Contains(), i will have to overload function Contains() using an Interface or this is already built-in in some Collections / Dictionaries when using a String as Key?
Thanks!
Re: Store Objects with Key in some sort of Collection/Dictionary
this is how a dictionary works:
vb Code:
Dim d As New Dictionary(Of String, Integer)
d.Add("one", 1)
d.Add("two", 2)
d.Add("three", 3)
MsgBox(d("one").ToString) 'returns "1"
Re: Store Objects with Key in some sort of Collection/Dictionary
using the dictionary from my previous post, here's how to find if a key exists:
vb Code:
MsgBox(d.Keys.Contains("two"))
Re: Store Objects with Key in some sort of Collection/Dictionary
Yes, the Dictionary should work. I didn't understand why it needed a KeyValuePair as parameter when calling Contains, but now i see should use:
vb.net Code:
If Not d.Keys.Contains("one") Then
'...
End If
I will try this, thanks!
EDIT: Exacly what you said in your last post. And as an alternative: d.ContainsKey("one")
Thanks.