Let's presume that the System.Runtime.CompilerServices namespace has been imported.
Public Module Exts <Extension> Public Sub SetBGcolor(c As Control, bg As Drawing.Color) c.BackgroundColor = bg End Sub <Extension> Public Sub SetFGcolor(c As Control, fg As Drawing.Color) c.ForegroundColor = fg End Sub End Module
MyLabel.SetBG( Color.Blue ) MyLabel.SetFG( Color.Pink )
Public Module Exts <Extension> Public Function SetBGcolor(c As Control, bg As Drawing.Color) As Control c.BackgroundColor = bg Return c End Function <Extension> Public Function SetFGcolor(c As Control, fg As Drawing.Color) As Control c.ForegroundColor = fg Return c End Function End Module
Why prefer to use a function?
If you return something. For example the thing you've been working on in the method. Which allows us to chain methods together, also know as Fluent style.
Also VB.net lets you call a Function without having to store the returned value in a variable.
Just use the function (for it's side-effects) and discard the result.
myLabel.SetBGcolor( Color.Blue ).SetFGcolor( Color.Pink )
You may have seen this before via the LINQ extension methods.
This post has been edited by AdamSpeight2008: 05 March 2014 - 01:17 PM