A delegate refers to a method that is not a class member.
Move method description under the class and change a link to the method taking the class into account.
Delegate Delegate3(index: integer);
Sub Main;
Var
cl1: Class1;
Begin
cl1 := New Class1.Create;
cl1.DelegateEvent := cl1.DelegateFunc1;
cl1.DelegateEvent(1);
cl1.DelegateEvent := DelegateFunc2;
cl1.DelegateEvent(2);
End Sub Main;
Public Class Class1: Object
Friend Event DelegateEvent: Delegate3;
Public Sub DelegateFunc1(index: integer);
Begin
Debug.WriteLine("DelegateFunc1 from Class1 " + index.ToString)
End Sub DelegateFunc1;
End Class Class1;
Public Sub DelegateFunc2(index: integer);
Begin
Debug.WriteLine("DelegateFunc2 " + index.ToString)
End Sub DelegateFunc2;
When the specified code is compiled, the string cl1.DelegateEvent := DelegateFunc2; displays the following error: Delegate refers to method that does not belong to class. To resolve the error, move description of the DelegateFunc2 method under the Class1 class and replace the string cl1.DelegateEvent := DelegateFunc2; with cl1.DelegateEvent := cl1.DelegateFunc2;.
See also: