Skip to main content

Encapsulate Method

  • 3 minutes to read

Wraps access to a field’s method in a method exposed in the active class. If the method is a function, the wrapper is also a function of the same type that returns the value returned by the call into the field’s corresponding method.

#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 methods.

#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 EncapsulateMethod
{
    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 EncapsulateMethod
    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 EncapsulateMethod
{
    private int ObjVarSumAB()
    {
        return objVar.SumAB();
    }
    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 EncapsulateMethod
    Private Function ObjVarSumAB() As Integer
        Return objVar.SumAB()
    End Function
    Private objVar As ClassA
End Class

#Animation

rsEncapsulateMethodCSharp