Results 1 to 5 of 5

Thread: Protected fields

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Protected fields

    With reference to the following link to MSDN titled 'Do not declare visible instance fields'...

    http://msdn.microsoft.com/en-us/libr...41(VS.80).aspx

    Externally visible refers to public, protected, and protected internal (Public, Protected, and Protected Friend in Visual Basic) accessibility levels.
    Protected fields are also considered visible.
    ...I am a bit confused as to why fields defined as 'protected' in a class are considered to be externally 'visible'.

    As I understand it, if you have an abstract class and a protected field defined within that class, that field can only be accessed by the containing class, and any classes derived from it. I agree that it is visible from classes other than the one it is defined in, but only derived classes. Isn't that the point of 'protected'?

    The article suggests defining the field as 'private', and accessing it via a protected property.

    This seems like an unnecessary step to me, but perhaps I am missing something. (Highly likely.)


    Thoughts?
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: Protected fields

    Nope, not missing a thing. For the same reason you don't declare fields public, you don't declare them protected. While yes, they are only visible to derived objects, they are visible to anything deriving from it (single layer public).

    4.0 has a new implementation to make the redundancy a little less... redundant:
    Code:
    Public Property MyProperty As string
    This assumes a private string field "MyProperty" for you (during JIT).

    Now the reason for all this? Fields are limiting and very much so. You can't bind to a field, fields are a pain in the arse to validate consistently, and they are not overrideable/overloadable (shadow works, but the value only exists when working with the derived class).

    Binding - The following works:
    VB Code:
    1. Public Class myExample
    2.         Private m_Value As string
    3.         Public property Value as string
    4.             Get
    5.                 Return m_value
    6.             End Get
    7.             Set(ByVal value As String)
    8.                 m_value = value
    9.             End Set
    10.         End Property
    11.     End Class
    12.  
    13.     Private Sub Form1_Load( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles MyBase.Load
    14.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="1"})
    15.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="2"})
    16.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="3"})
    17.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="4"})
    18.  
    19.         Me.ComboBox1.DisplayMember = "Value"
    20.     End Sub
    This, however, does not:
    VB Code:
    1. Public Class myExample
    2.         Public Value As string
    3.     End Class
    4.  
    5.     Private Sub Form1_Load( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="1"})
    7.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="2"})
    8.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="3"})
    9.         Me.ComboBox1.Items.Add(New myExample() with {.Value ="4"})
    10.  
    11.         Me.ComboBox1.DisplayMember = "Value"
    12.     End Sub

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Protected fields

    Hmmm, ok, I definitely agree with the point about not making fields public, and on reflection I think I see the point is valid for protected also.

    Interesting to see the new implementation in 4.0. I assume this does the same thing for protected properties also? (i.e. Defines a private field for you 'behind the scenes'.)

    I'm using 2.0, so will not affect me, but interesting nonetheless.


    Thanks for the reply.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  4. #4
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: Protected fields

    Interesting to see the new implementation in 4.0. I assume this does the same thing for protected properties also? (i.e. Defines a private field for you 'behind the scenes'.)
    Yes, for the single line property - scope doesn't change the implementation.
    From the "New Features in VB10" doc:
    Auto-implemented Properties
    Developers often need to create simple entity classes or containers for data, in which the properties defined follow a very simple structure:
    Private _FirstName As String

    Property FirstName() As String
    Get
    Return _FirstName
    End Get
    Set(ByVal value As String)
    _FirstName = value
    End Set
    End Property

    Auto-implemented properties provide a simple one-line way of expressing this concept:

    Property ID() As Integer
    Property FirstName() As String
    Property LastName() As String

    In this case the compiler will generate a backing field with the same name as the property, but with a preceding underscore. It will also fill in the property’s getter and setter.
    Initializers can be used to give an auto-implemented property a default value (which gets set in the class’ constructor):
    Property ID() As Integer = -1
    Property SupplierList() As New List(Of Supplier)
    Property OrderList() As New List(Of Order) With {.Capacity = 100}

    <DefaultValue("-")>
    Property Name() As String Implements ICustomer.Name

    Auto-implemented properties cannot have parameters, nor can they be declared ReadOnly or WriteOnly.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Protected fields

    Implicit public properties/autoimplemented properties already exist in C# 2.0, it's just being made available to VB.NET in VB.NET 4.0.

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