Concept: Extensions

Reference: MSDN Extension Methods (Visual Basic)

Intro:While translating a C# library, i ran into a segment of code that i didnt recognize. After doing some research found out that the same design can be implemented in VB.Net (VB 2k8). One catch to the whole implementation is that the Extension development is only possible in a module. As well, there are some limitations to the design of the extension.


C# style
Syntax:
1:  Scope ReturnType MethodName('this' ParentType parameter, ParamType para1, ...)  
2:  {  
3:  ....  
4:  }  

When designing this in C# the structure is: - Scope : Public, Shared, Private, Friend, etc - ReturnType : Object or Type - MethodName : any name that is consistent with the naming convention of the compiler, - 'this' : the internal pointer to the object that will have the extension, - ParentType : the object/type that will be utilizing this extension, - parameter : the internal reference to the 'this' object for operation within the extension method. - ParamType : the object/type that will actually be passed to the method - para1 : the parameter variable name to be used within the method All that being said, here is a live example from a Silverlight Library that i have been de-engineering for educational purposes only (Silverlight Menu).

Declaration:
1:  public static Point TransformFromRootVisual(this UIElement element)  
2:  {  
3:   try  
4:   {  
5:    MatrixTransform globalTransform = (MatrixTransform)element.TransformToVisual(null);  
6:    return globalTransform.Matrix.Transform(_zero);  
7:   }  
8:   catch { }  
9:   return _zero;  
10:  }  

Usage:
1:  Point p = this.TransformFromRootVisual();  

VB 2k8 styleVB 2k8 has a similar declaration statement, but varies in how each part is incorporated. I will keep the same declaration format names for consistency and comparison.

Syntax:
1:  <Extension()> _  
2:  Scope Method MethodName(parameter As ParentType, para1 As ParamType, ...) As ReturnType  
3:  End Method  

Explanation:
- <Extension()> : Attribute to precede the Method declaration, much like all other Attributes, except this Attribute is Method (Sub/Function) specific and is not available for use with any other declaration type.
- Scope : Private, Public, Friend, etc.
- Method : Sub or Function
- MethodName : Any method name that is consistent with the nameing convention of the compiler
- parameter : any parameter name that is consistent with the naming convention of the compiler.
- ParentType : The object/type that this Extension method will be attached to.
- param1 : Any parameter name that is consistent with the naming convention of the compiler, this parameter will actually be a passed value.  This is an optional part of the Extension declaration.
- ParamType : The object/type that the parameter will be defined.
- ReturnType : the object/type that will be returned once all operations are completed inside the method.

As you can see there isn't a whole lot of difference between the C# and VB 2k8 format.  yes there are some structural and design implementation difference but in either case they result in the same outcome.

Here is an example of the VB 2k8 equivalent of the C# example:
Extension Declaration:
1:  Public Function TransformFromRootVisual(ByVal element As UIElement) As Point  
2:   Dim p As New Point  
3:   Try  
4:    Dim globalTransform As MatrixTransform  
5:    globalTransform = CType(element.TransformToVisual(Nothing), MatrixTransform)  
6:    p = globalTransform.Matrix.Transform(_zero)  
7:   Catch ex As Exception  
8:    p = _zero  
9:   End Try  
10:   Return p  
11:  End Function  

Usage:
1:  Dim p As Point = Me.TransformFromRootVisual()  

Summary:
  Although in C# you dont have the availability of putting certain code in a module to make it "globally" available, to the whole namespace in which it is declared, you need to place it in a class that is static and not instantiatable.

  On the flip side, in VB 2k8 you need to explicitely define a module within the namespace in which you want it used.  The caveat is that Extension(s) for VB 2k8 are restricted to only Module level methods and not Class level.  Which for some mind-sets restricts the ability to co-locate in consolidated locations.

  The key notes:
1) Include System.Runtime.ComilerServices
2) make sure the first parameter is the Object/Type you want the method to be associated with.  If you want it to operate on Object types, then make the first parameter of type Object, but be aware that if you do so, the Extension method will be extended out to all objects that inherit type Object within the namespace.

Comments

Popular posts from this blog

SysInternals - BgInfo for ALL Users

JSON/AJAX Helpers

Acquiring List of controls - Classic JS