|
-
Jun 10th, 2012, 05:34 PM
#1
[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!
Last edited by jcis; Jun 10th, 2012 at 05:44 PM.
-
Jun 10th, 2012, 05:44 PM
#2
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"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 10th, 2012, 05:52 PM
#3
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"))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 10th, 2012, 05:56 PM
#4
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.
Last edited by jcis; Jun 10th, 2012 at 07:11 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|