Using Extension Methods in .NET

1. Create a module (VB.NET in this example) or static class (C#.NET)
***IMPORTANT: Make sure it is a PUBLIC module***

2. Create a Function or Sub.  Add Imports declaration.  Mark each function or sub with <Extension()>

3. The first parameter in the method signature will be the object type desired (str as STRING). You can add more parameter after that one but it will not be used as an input parameter.  So for example the call would be...

Dim foo as string = "ABC"

foo.StripNull("DEF")

Result----> foo = "ABCDEF"

 

Imports System.Runtime.CompilerServices

Public Module StringExt

    <Extension()>
    Public Function StripNull(str As String, otherParam as string) As String
         Try
              
Return str.ToString + otherParam.ToString
         
Catch ex As Exception
              
Return ""
         
End Try
    End Function

End Module


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences