Best Method for exposing an dictionary object through properties

Last post 08-17-2006, 12:23 PM by myoungbl. 2 replies.
Sort Posts: Previous Next
  •  07-17-2006, 12:31 PM Post number 1293

    • myoungbl is not online. Last active: 09-24-2006, 8:06 PM myoungbl
    • Top 500 Contributor
    • Joined on 07-16-2006
    • Gretna, LA
    • Level 1: Deep thought

    Best Method for exposing an dictionary object through properties

    Subject says it all. I am writing a simple contacts application in VB.NET that will include notes about contacts that I am storing in a table. The table will contain the contactID and a date and time field as primary key and a varchar field to store the note in. When I read the notes into my class I am going to store the notes in a dictionary object  as:
     
    Private _notes As Dictionary(Of Date, String)
     
    Having set that up, what is the best way to expose this object though a public property?  I could just make it public since I don't plan to add any business logic yet. but I would like to setup it up as a property anyway incase I do.
     
    Thanks.
     
    Marshall

    Marshall Youngblood
  •  07-25-2006, 6:48 PM Post number 1450 in reply to post number 1293

    • Damon is not online. Last active: 12-02-2008, 6:48 PM Damon
    • Top 10 Contributor
    • Joined on 06-26-2006
    • Dallas, TX
    • Acorn Archimedes

    Re: Best Method for exposing an dictionary object through properties

    It depends on whether you want to expose it as a dictionary or not.  If you want to expose the entire dictionary object then you just make it a normal property.  This is the easiest way to go about things because you only have to write a single property and you get all of the functionality associated with a dictionary object.

    If you want to protect the dicationary from being exposed directly then you can create methods off your business object that operate on the dictionary.  This is a bit harder because you have to consider everything that someone may want to do with the dicationary (add, remove, etc).  But it does give you more control over how the dictionary is used.

    If you were looking for syntax, it should be something like this:

    Private _notes As Dictionary(Of Date, String)

    Public Property Notes() As Dictionary(Of Date, String)
        Get
            If _notes Is Nothing Then _notes = New Dictionary(Of Date, String)()
            Return _notes
        End Get
        Set(ByVal value As Dictionary(Of Date, String))
            _notes = value
        End Set
    End Property


    Damon Armstrong, Technology Consultant
    [Blog] [Articles]
  •  08-17-2006, 12:23 PM Post number 1732 in reply to post number 1450

    • myoungbl is not online. Last active: 09-24-2006, 8:06 PM myoungbl
    • Top 500 Contributor
    • Joined on 07-16-2006
    • Gretna, LA
    • Level 1: Deep thought

    Re: Best Method for exposing an dictionary object through properties

    That was excatly what I was looking for. My app will be starting off as a fairly simple prorgam, so the direct exposure will work nicely.
     
    Thank you.

    Marshall Youngblood
View as RSS news feed in XML