Adding a method to all forms existing forms without changing their code
Hi was just wondering if it is possible to add a method to all forms in my project without having to do it on one form and Inherit all my other forms from that one
Thanks
Kris
Re: Adding a method to all forms existing forms without changing their code
In order for a type to have a method it must either declare that method itself or it must inherit it form its base class. There are no other ways.
Re: Adding a method to all forms existing forms without changing their code
The following seems to work:
Code:
Friend Module foo
<System.Runtime.CompilerServices.Extension> _
Friend Sub test(ByVal myForm As System.Windows.Forms.Form)
MessageBox.Show("seems to work")
End Sub
End Module
Call it without specifying the parameter - e.g., Me.test()
You'll need to reference System.Core.dll to make this work.
Re: Adding a method to all forms existing forms without changing their code
Quote:
Originally Posted by
David Anton
The following seems to work:
Code:
Friend Module foo
<System.Runtime.CompilerServices.Extension> _
Friend Sub test(ByVal myForm As System.Windows.Forms.Form)
MessageBox.Show("seems to work")
End Sub
End Module
Call it without specifying the parameter - e.g., Me.test()
I was going to suggest an extension method myself but decided not to because it has severe limitations. As you have, you would need to extend the Form class in order for it to be callable on all forms in the project. As such you would only have access the public members of the Form class within the method. You wouldn't have access to any Protected members of the Form class and you wouldn't have access to any members of each specific form. As such it's unlikely to be especially useful.