Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Using Extension Methods in VS2005

Using Extension Methods in VS2005

Scheduled Pinned Locked Moved Visual Basic
csharpdotnetdebugginghelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dlarkin77
    wrote on last edited by
    #1

    Hi, I know that you can use extension methods in C# while targeting version 2.0 of the .NET framework by creating your own System.Runtime.CompilerServices.ExtensionAttribute I've done the same in a VB.NET application but for the life of me I can't get the thing to work. Here is the code I'm using:

    Namespace System.Runtime.CompilerServices
    <attributeusage(attributetargets.method> _
    Public NotInheritable Class ExtensionAttribute
    Inherits Attribute
    End Class
    End Namespace

    In a module

    Imports System.Runtime.CompilerServices
    <extension()> _
    Module ExtensionMethods
    <extension()> _
    Public Function Test(ByVal s As String) As String
    Return s & " hello"
    End Function
    End Module

    Sub Main()
    Dim s As String = "Hello"
    Debug.Print(s.Test())
    End Sub

    I get a compiler error saying that "Test is not a member of String" Am I missing something or can you simply not use extension methods in VB.NET for version 2.0? Thanks very much, dlarkin77

    W D 2 Replies Last reply
    0
    • D dlarkin77

      Hi, I know that you can use extension methods in C# while targeting version 2.0 of the .NET framework by creating your own System.Runtime.CompilerServices.ExtensionAttribute I've done the same in a VB.NET application but for the life of me I can't get the thing to work. Here is the code I'm using:

      Namespace System.Runtime.CompilerServices
      <attributeusage(attributetargets.method> _
      Public NotInheritable Class ExtensionAttribute
      Inherits Attribute
      End Class
      End Namespace

      In a module

      Imports System.Runtime.CompilerServices
      <extension()> _
      Module ExtensionMethods
      <extension()> _
      Public Function Test(ByVal s As String) As String
      Return s & " hello"
      End Function
      End Module

      Sub Main()
      Dim s As String = "Hello"
      Debug.Print(s.Test())
      End Sub

      I get a compiler error saying that "Test is not a member of String" Am I missing something or can you simply not use extension methods in VB.NET for version 2.0? Thanks very much, dlarkin77

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      If I remember correctly, extension methods cannot be targeted for version 2 (for example extension attribute was introduced in 3.5). Have a look at this article: Extension Methods (Visual Basic)[^].

      The need to optimize rises from a bad design. My articles[^]

      1 Reply Last reply
      0
      • D dlarkin77

        Hi, I know that you can use extension methods in C# while targeting version 2.0 of the .NET framework by creating your own System.Runtime.CompilerServices.ExtensionAttribute I've done the same in a VB.NET application but for the life of me I can't get the thing to work. Here is the code I'm using:

        Namespace System.Runtime.CompilerServices
        <attributeusage(attributetargets.method> _
        Public NotInheritable Class ExtensionAttribute
        Inherits Attribute
        End Class
        End Namespace

        In a module

        Imports System.Runtime.CompilerServices
        <extension()> _
        Module ExtensionMethods
        <extension()> _
        Public Function Test(ByVal s As String) As String
        Return s & " hello"
        End Function
        End Module

        Sub Main()
        Dim s As String = "Hello"
        Debug.Print(s.Test())
        End Sub

        I get a compiler error saying that "Test is not a member of String" Am I missing something or can you simply not use extension methods in VB.NET for version 2.0? Thanks very much, dlarkin77

        D Offline
        D Offline
        dlarkin77
        wrote on last edited by
        #3

        It seems I was being particularly dense yesterday afternoon. The reason I couldn't get this to work was that I was actually using VS 2005 instead of VS 2008. The above code snippets work correctly when targeting .NET 2.0 in a VS 2008 project Thanks, dlarkin77

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups