I have written a property, where I want to have the GET be available to
anyone (i.e. Public); however, I want the SET to be available ONLY to the
class or program itself (i.e. Friend).
In VB6 this was easy, since the Gets and Sets were seperate. How can one do
this in VB.NET?
Tom
If you are determined to use Property as opposed to individual
accessors (which is far easier), you can go along the lines of the
following:
--begin example--
Public MustInherit Class TestBase
Protected mValue As String
Friend Property Value() As String
Get
Return mValue
End Get
Set(ByVal Value As String)
mValue = Value
End Set
End Property
End Class
Public Class Test
Inherits TestBase
Public Shadows ReadOnly Property Value() As String
Get
Return mValue
End Get
End Property
End Class
--end example--
To set the value from within the same project, you can use:
Dim oTest As Test
...
CType(oTest,TestBase).Value = "Testing"
This can be neatened up by using a separated interface pattern. ITest
is implemented (and therefore exposed), and there is no need to expose
"TestBase" at all.
But I do agree with the other chaps: The following is by far the
easiest:
--begin example--
Public Class Test
Private mValue As String
Public ReadOnly Property Value() As String
Get
Return mValue
End Get
End Property
Friend Sub SetValue(Value As String)
mValue = Value
End Sub
End Class
--end example--
Rgds,