Creating Extension Methods in VB - Jesse Liberty - Silverlight Geek Page view counter

Creating Extension Methods in VB

 

I received a question about how to create extension methods in VB. It turns out that the key difference is that the extension must be defined in a Module and not in a class.

Thus, building on the code from the previous example, we end up with the same Page.xaml and a very similar Page.xaml.vb and a quite different Extension.vb

Here is Extension.vb

Imports Microsoft.VisualBasic
Imports System
Namespace GetLastNFromString
   Module ExtensionModule
      <System.Runtime.CompilerServices.Extension()> _
      Public Function Right(ByVal s As String, _
ByVal howMany As Integer) As String If howMany <= s.Length Then Return s.Substring(s.Length - howMany, howMany) Else Return s End If End Function End Module End Namespace



And here is Page.xaml.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes

Namespace GetLastNFromString
   Partial Public Class Page
       Inherits UserControl
      Public Sub New()
         InitializeComponent()
         AddHandler GoButton.Click, AddressOf GoButton_Click
      End Sub

      Private Sub GoButton_Click( _
            ByVal sender As Object, _
            ByVal e As RoutedEventArgs)
         ResultBox.Text = _
StringToParse.Text.Right(Convert.ToInt32 (NumberToGet.Text)) End Sub End Class End Namespace

For completeness, here is Page.xaml

<UserControl x:Class="GetLastNFromString.Page"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   Width="650"
   Height="200">
  <Grid x:Name="LayoutRoot"
        Background="Bisque">
    <Grid.RowDefinitions>
      <RowDefinition Height="1.5*" />
      <RowDefinition Height="1*" />
      <RowDefinition Height="1*" />
      <RowDefinition Height="1*" />
      <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="1*" />
      <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <TextBlock HorizontalAlignment="Center"
               Margin="0,0,0,0"
               x:Name="Title"
               Grid.ColumnSpan="2"
               FontFamily="Georgia"
               FontSize="36"
               Foreground="#FF0C42EE"
               Text="Last n Traditional (VB)"
               TextWrapping="Wrap" />
    <TextBlock x:Name="String_Prompt"
               Text="String"
               Width="Auto"
               Grid.Row="1"
               Margin="0,0,10,0"
               Foreground="Blue"
               FontFamily="Georgia"
               FontSize="24"
               VerticalAlignment="Bottom"
               HorizontalAlignment="Right" />
    <TextBlock x:Name="NumChars_Prompt"
               Text="Number To  Get"
               Grid.Row="2"
               Margin="0,0,10,0"
               Foreground="Blue"
               FontFamily="Georgia"
               FontSize="24"
               VerticalAlignment="Bottom"
               HorizontalAlignment="Right" />
    <TextBlock x:Name="Result"
               Text="Result"
               Grid.Row="3"
               Margin="0,0,10,0"
               Foreground="Blue"
               FontFamily="Georgia"
               FontSize="24"
               VerticalAlignment="Bottom"
               HorizontalAlignment="Right" />
    <TextBox FontFamily="Georgia"
             FontSize="24"
             HorizontalAlignment="Left"
             Margin="5,0,0,2"
             x:Name="StringToParse"
             VerticalAlignment="Bottom"
             Width="400"
             Grid.Column="1"
             Grid.Row="1"
             Text="" />
    <TextBox  x:Name="NumberToGet"
              FontFamily="Georgia"
              FontSize="24"
              HorizontalAlignment="Left"
              Margin="5,0,0,0"
              VerticalAlignment="Bottom"
              Width="Auto"
              Grid.Column="1"
              Grid.Row="2"
              Text="?" />
    <TextBlock x:Name="ResultBox"
               HorizontalAlignment="Left"
               Margin="5,0,0,0"
               VerticalAlignment="Bottom"
               Grid.Column="1"
               Grid.Row="3"
               FontFamily="Georgia"
               FontSize="24"
               Text="" />
    <Button x:Name="GoButton"
            Content=" Go! "
            Grid.Row="4"
            Grid.Column="0"
            FontFamily="Georgia"
            FontSize="18"
            Foreground="Blue"
            HorizontalAlignment="Right"
            Margin="5"
            Width="Auto" />
  </Grid>
</UserControl>
 

This work is licensed under a Creative Commons Attribution By license.
Published Tuesday, February 10, 2009 10:47 AM by jesseliberty
Filed under:

Comments

# Creating Extension Methods in VB - Jesse Liberty - Silverlight Geek

Pingback from  Creating Extension Methods in VB - Jesse Liberty - Silverlight Geek

# Silverlight Cream for February 10, 2009 -- #513

In this issue: Maurice de Beijer, Jesse Liberty(2), Corey Schuman, Mike Snow, Tim Heuer, and Radenko

Wednesday, February 11, 2009 1:04 AM by Community Blogs

# Programming with Silverlight, WPF &amp; .NET &raquo; Extension Methods, Silverlight &#038; Linq

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Extension Methods, Silverlight &#038; Linq