I defined one class (MyClass) with a constructor with an integer parameter (value). This is the constructor:
If value > 1000 Then
MessageBox("not Valid")
RaiseEvent MyEvent
Else
MessageBox("valid")
End If
MyEvent has been defined in the Event Definition of MyClass (no parameters, no Return)
In Window1 I want to handle MyEvent in the Opening event of the Window:
Var MyObject As MyClass = New MyClass(1500)
AddHandler MyObject.MyEvent, WeakAddressof MyRemark
MyRemark is a method in Windows1 with one MessageBox.
AddHandler gives two error messages:
- Expected delegate Delegate(MyClass), but got delegate Delegate()
- This method requires fewer parameters than were passed.
I don’t understand what is wrong with this piece of code!
Part of the problem might be that you are instantiating MyClass (which will raise MyEvent) before you call AddHandler.
2 Likes
I think you wrote
If value > 1000 Then
MessageBox("not Valid")
RaiseEvent MyEvent
Else
MessageBox("valid")
End If
// Calling the overridden superclass constructor.
Super.Constructor
It is preferable wrote:
// Calling the overridden superclass constructor.
Super.Constructor
If value > 1000 Then
MessageBox("not Valid")
RaiseEvent MyEvent
Else
MessageBox("valid")
End If
1 Like
Event handler methods receive an additional parameter that refers back to the object that’s raising the event:
i.e. it should look like this:
Sub MyRemark(Sender As MyClass)
rather than like this:
Sub MyRemark()
1 Like
@Andrew_Lambert True, no error messages anymore. But the message in MyRemark (MessageBox(“Value not OK”)) isn’t displayed yet.
@Clifford_Antrim That’s a good advice. I will change the program not sending a value to the constructor.
@J_Luc_Pellerin I think Super.Constructor is not important in this case.
@Andrew_Lambert @Clifford_Antrim A combination of both replies solved the problem. Thank you so much!
Sub MyRemark(sender As MyClass)
MessageBox("Value not ok")
In the Opening event:
Var MyObject As MyClass = New MyClass
AddHandler MyObject.MyEvent, WeakAddressof MyRemark
MyObject.MyInput(1500)