
HOWTO: Restrict access to public properties (or at least its data)
Quote:
> I have a project which have classes with properties I want to expose to
> certain projects but not to all projects. Or rather it is the underlying
> data which I want to restrict access to.
> I'd be grateful of any suggestions as to the best way to achieve this?
I don't know your specific situation, but another method to limit access
is to use interfaces.
A class can support many different interfaces. Whatever you include in the
interface is exposed to the outside world. Whatever you don't include
can't be accessed.
For an example, add 3 classes to a new project and paste in the code
below into their respective modules. You'll see the Admin object has full
access, but the Employee object can only read a couple properties.
Access is controlled by what interface they use.
Your projects can then use whatever interface is appropreate.
HTH
LFS
' Form1 code ---
Private Sub Form_Load()
Dim EmpAccess As Class2 ' Employee access
Dim AdmAccess As Class3 ' Admin access
' Admin has full access
Set AdmAccess = New Class1
With AdmAccess
.Name = "Joe"
.ID = "B123"
.Wage = 10.5
End With
' Employee has only read access
Set EmpAccess = AdmAccess
With EmpAccess
Debug.Print .Name
Debug.Print .ID
End With
End Sub
' Class3 code --- (full access)
Public Name As String
Public ID As String
Public Wage As Currency
' Class2 code --- (read access)
Public Function Name() As String
'
End Function
Public Function ID() As String
'
End Function
' Class1 code --- (the data object)
Option Explicit
Implements Class2
Implements Class3
Private mName As String
Private mID As String
Private mWage As Currency
' Class2 code (employee)
Private Function Class2_ID() As String
Class2_ID = mID
End Function
Private Function Class2_Name() As String
Class2_Name = mName
End Function
' Class3 code (admin)
Private Property Let Class3_ID(ByVal RHS As String)
mID = RHS
End Property
Private Property Get Class3_ID() As String
Class3_ID = mID
End Property
Private Property Let Class3_Name(ByVal RHS As String)
mName = RHS
End Property
Private Property Get Class3_Name() As String
Class3_Name = mName
End Property
Private Property Get Class3_Wage() As Currency
Class3_Wage = mWage
End Property
Private Property Let Class3_Wage(ByVal RHS As Currency)
mWage = RHS
End Property