Encapsulate Event
- 3 minutes to read
In This Article
Wraps access to a field’s event in a new event exposed in the active class.
#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 events.
#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 EncapsulateEvent
{
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 EncapsulateEvent
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 EncapsulateEvent
{
public event ClassA.MyEventHandler ObjVarABSummed
{
add
{
objVar.ABSummed += value;
}
remove
{
objVar.ABSummed -= 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 EncapsulateEvent
Public Custom Event ObjVarABSummed As ClassA.MyEventHandler
AddHandler(ByVal value As ClassA.MyEventHandler)
AddHandler objVar.ABSummed, value
End AddHandler
RemoveHandler(ByVal value As ClassA.MyEventHandler)
RemoveHandler objVar.ABSummed, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal ea As System.EventArgs)
End RaiseEvent
End Event
Private objVar As ClassA
End Class