Results 1 to 4 of 4

Thread: [RESOLVED] Store Objects with Key in some sort of Collection/Dictionary

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Resolved [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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Store Objects with Key in some sort of Collection/Dictionary

    this is how a dictionary works:

    vb Code:
    1. Dim d As New Dictionary(Of String, Integer)
    2. d.Add("one", 1)
    3. d.Add("two", 2)
    4. d.Add("three", 3)
    5.  
    6. MsgBox(d("one").ToString) 'returns "1"

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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:
    1. MsgBox(d.Keys.Contains("two"))

  4. #4

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. If Not d.Keys.Contains("one") Then
    2.   '...
    3. 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
  •  



Click Here to Expand Forum to Full Width