Skip to main content

Encapsulate Property

  • 3 minutes to read

Wraps access to a field’s property in a new property (of the same type as the field’s property) exposed in the active class. The getter returns the value of the field’s property and the setter assigns the value to the field’s property.

#Availability

Available from the context menus or via shortcuts:

  • when the edit cursor or caret is on a field declaration, provided that the field type contains properties.

#Example

public class ClassA
{
    // delegates...
    public delegate void MyEventHandler(object sender, EventArgs ea);

    // constructor...
    public ClassA(int a, int b)
    {
        A = a;
        B = b;
    }

    // public properties...
    public int A { get; set; }
    public int B { get; set; }

    // public methods...
    public int SumAB()
    {
        if (ABSummed != null)
            ABSummed(this, EventArgs.Empty);
        return A + B;
    }

    // public events...
    public event MyEventHandler ABSummed;
}

class EncapsulateProperty
{
    ClassA objVar;
}
Public Class ClassA
    ' delegates
    Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal ea As EventArgs)

    ' private fields...
    Private _A As Integer
    Private _B As Integer

    ' constructor...
    Public Sub New(ByVal A As Integer, ByVal B As Integer)
        _A = A
        _B = B
    End Sub

    ' public properties...
    Public Property A() As Integer
        Get
            Return _A
        End Get
        Set(ByVal value As Integer)
            _A = value
        End Set
    End Property
    Public Property B() As Integer
        Get
            Return _B
        End Get
        Set(ByVal value As Integer)
            _B = value
        End Set
    End Property

    ' public methods...
    Public Function SumAB() As Integer
        RaiseEvent ABSummed(Me, EventArgs.Empty)

        Return A + B
    End Function

    ' public events...
    Public Event ABSummed As MyEventHandler
End Class

Class EncapsulateProperty
    Private objVar As ClassA
End Class

Result:

public class ClassA
{
    // delegates...
    public delegate void MyEventHandler(object sender, EventArgs ea);

    // constructor...
    public ClassA(int a, int b)
    {
        A = a;
        B = b;
    }

    // public properties...
    public int A { get; set; }
    public int B { get; set; }

    // public methods...
    public int SumAB()
    {
        if (ABSummed != null)
            ABSummed(this, EventArgs.Empty);
        return A + B;
    }

    // public events...
    public event MyEventHandler ABSummed;
}

class EncapsulateProperty
{
    public int ObjVarA
    {
        get
        {
            return objVar.A;
        }
        set
        {
            objVar.A = value;
        }
    }
    ClassA objVar;
}
Public Class ClassA
    ' delegates
    Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal ea As EventArgs)

    ' private fields...
    Private _A As Integer
    Private _B As Integer

    ' constructor...
    Public Sub New(ByVal A As Integer, ByVal B As Integer)
        _A = A
        _B = B
    End Sub

    ' public properties...
    Public Property A() As Integer
        Get
            Return _A
        End Get
        Set(ByVal value As Integer)
            _A = value
        End Set
    End Property
    Public Property B() As Integer
        Get
            Return _B
        End Get
        Set(ByVal value As Integer)
            _B = value
        End Set
    End Property

    ' public methods...
    Public Function SumAB() As Integer
        RaiseEvent ABSummed(Me, EventArgs.Empty)

        Return A + B
    End Function

    ' public events...
    Public Event ABSummed As MyEventHandler
End Class

Class EncapsulateProperty
    Public Property ObjVarA() As Integer
        Get
            Return objVar.A
        End Get
        Set(ByVal value As Integer)
            objVar.A = value
        End Set
    End Property
    Private objVar As ClassA
End Class

#Animation

rsEncapsulatePropertyCSharp