Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    Property Setter and Getter Accessors

    I see Scott talking about Using for VB in VS 2005 and thought I'd throw in another new thing (that I absolutely love and I think also works in C#, but I haven't had time to try it yet).

    Private _Test As String = ""
    Public Property Test() As
    String
        
    Get
             
    Return
    _Test
         End
    Get
        
    Friend Set(ByVal Value As String
    )
              _Test = Value
         End
    Set
    End Property

    So in this case, inside of my Class Library (the Assembly where my code sits), I can Read and Write to the Property (in case I happen to have any other logic in my setter) and then anytime someone tries to write to the Property outside of the Assembly, they won't be able to (it will appear to be a ReadOnly Property.  This is perfect and much needed for we Class Library junkies and I am super pumped about it!  :)

    Sorry if this has already been talked about.  I haven't been able to keep up on reading blogs much lately!  :(

    Comments

    TrackBack said:

    Two more VB 2005 features...
    # June 15, 2004 6:36 PM

    secretGeek said:

    hey that's cool. i often want to do exactly that.
    # June 16, 2004 12:36 AM

    Ever hopefull said:

    Can anyone confirm if this works for C# too?
    # June 16, 2004 12:04 PM

    Fan said:

    @Ever hopefull
    Yes, It dose!
    # June 16, 2004 1:03 PM

    HumanCompiler said:

    Thanks for verifying that for us...good to know! :)
    # June 16, 2004 2:32 PM

    TrackBack said:

    # June 16, 2004 5:10 PM

    Mike Swaim said:

    Why not make the variable internal. That way, other objects inside your class library can mess with it, but it's invisible to code outside your library. (Plus, you can do it now.)
    # June 16, 2004 5:21 PM

    HumanCompiler said:

    Because what if I have logic in my setter than does some other things? That's a no go for me at least...what I end up doing is like this (today)...

    Private _Test As String = ""

    Public ReadOnly Property Test() As String
    Get
    Return _Test
    End Get
    End Property

    Friend Sub SetTest(ByVal Value As String)
    _Test = Value
    'other code
    End Sub

    That's why the new stuff mentioned in this post will be so handy...don't have to make a separate Method everytime :P
    # June 16, 2004 5:34 PM

    TrackBack said:

    # June 16, 2004 6:38 PM

    Diego Vega said:

    Nice that you mention this. I have been wondering if VB.NET 2005 would support this since it was included in C# 2.0 spec (http://msdn.microsoft.com/vcsharp/team/language/default.aspx). Let me say this one of the very few things I missed from VB6 ;)
    # June 19, 2004 4:48 AM

    Shane said:

    Are there any negative aspects of using setters and getters in this manner?

    # August 11, 2007 6:02 PM

    HumanCompiler said:

    Not that I know of.

    # August 13, 2007 2:43 PM