Public or Private members?

learning

Active member
Joined
Aug 31, 2009
Messages
28
Location
Michigan
Programming Experience
5-10
If I have a class defined as shown below. The class itself isn't important. Just that is has private members interfaced through public properties and functions :
VB.NET:
    Public Class DataArray

        Private _dataArr() As Single

        Public Sub New(ByVal initialData As Single())
            _dataArr = initialData
        End Sub

        Public Property Data(ByVal itemIndex As Integer) As Single
            Get
                Return _dataArr(itemIndex)
            End Get
            Set(value As Single)
                _dataArr(itemIndex) = value
            End Set
        End Property

        Public Function Max(ByVal lowIndex As Integer, ByVal hiIndex As Integer) As Single
            Dim sortedData() As Single
            sortedData = CType(Array.CreateInstance(GetType(Single), hiIndex - lowIndex), Single())
            Array.Copy(_dataArr, lowIndex, sortedData, 0, hiIndex - lowIndex)
            Array.Sort(sortedData)
            Return sortedData(sortedData.GetUpperBound(0))
        End Function

   End Class

Then I create another class that uses an instance of the previous one :
VB.NET:
    Public Class Class2

        Public SomeData1 As DataArray
        Public SomeData2 As DataArray
        Public SomeData3 As DataArray

        Private _someOtherData as Integer

        Public Property SomeOtherData As Integer
            Get
                Return _someOtherData
            End Get
            Set(value As Single)
                _someOtherData = value
            End Set
        End Property
        .
        .
        .

    End Class

My question is, should I be declaring SomeData1-3 Public or should it be Private with Public additional properties that expose the members of DataArray? Or, something else.

Thanks
 
You basically never expose read/write data via a public field. Easy. People will do it at times if they are the only consumer of the data but if you just stick to that simple rule then you can never go wrong. Exposing data via properties will never cause a problem but using public fields might, if not now then at some point in the future.
 
Yes, I've been trying to adhere to that. What I was wondering about though, is that in the example I've shown the DataArray class is protecting its members already. So, when I use instances of the class inside of Class2, should I be making them Private there? That may be the question you were answering but I just wanted to be sure.
 
In Class2 it's about the DataArray object. What that object does about its internals is irrelevant. Each class only cares about the objects that it has direct access to and those objects should only be exposed via properties, not fields. It's not about protecting members because if the property has a getter and a setter with no extra code then the object is just as exposed as it would be if you used a field. It's about the fact that properties can be bound and fields can't, the fact that properties allow you to change implementation without changing interface and the fact that properties allow you to add extra code like validation and event raising.
 
So Class2 proper form would be like this?

VB.NET:
    Public Class Class2

        Private _someData1 As DataArray
        Private _someData2 As DataArray
        Private _someData3 As DataArray

        Private _someOtherData as Integer

        Public Property SomeOtherData As Integer
            Get
                Return _someOtherData
            End Get
            Set(value As Single)
                _someOtherData = value
            End Set
        End Property

        Public Property Data(ByVal itemIndex As Integer) As Single
            Get
                Return _someData1.Data(itemIndex)
            End Get
            Set(value As Single)
                _someData1.Data(itemIndex) = value
            End Set
        End Property

        Public Function Max(ByVal lowIndex As Integer, ByVal hiIndex As Integer) As Single
            Return _someData1.Max(lowIndex, hiIndex)
        End Function
        .
        .
        .

    End Class
 
No. Class2 doesn't care what's inside DataArray. All it cares about is the DataArray object. You're trying to complicate it.
Public Class Class2

    Private _someData1 As DataArray

    Public Property SomeData1 As DataArray
        Get
            Return _someData1
        End Get
        Set(value As DataArray)
            _someData1 = value
        End Set
    End Property

End Class
 
Back
Top