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. Web Development
  3. ASP.NET
  4. Extending Base Type Functionality with Extension Methods

Extending Base Type Functionality with Extension Methods

Scheduled Pinned Locked Moved ASP.NET
tutorial
5 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.
  • I Offline
    I Offline
    Imran Khan Pathan
    wrote on last edited by
    #1

    For Example. string myString = "This my Code : 589745"; string myCode = myString.GetNumber(); myString.GetNumber method returns number from string variable. I can write method to get number from string but I just need to know how to implement class that extend base type functionality. thanks Imrankhan

    please don't forget to vote on the post that helped you.

    A 1 Reply Last reply
    0
    • I Imran Khan Pathan

      For Example. string myString = "This my Code : 589745"; string myCode = myString.GetNumber(); myString.GetNumber method returns number from string variable. I can write method to get number from string but I just need to know how to implement class that extend base type functionality. thanks Imrankhan

      please don't forget to vote on the post that helped you.

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Why do you need to write a new class when there is already a concept of Extension Function.. use like this

      public int GetNumber(this string str)
      {
      // Apply your regular expression here to get the number.
      }

      Now to call just use myString.GetNumber. :)

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      My Latest Articles-->** Microsoft Bing MAP using Javascript
      CLR objects in SQL Server 2005
      Uncommon C# Keywords
      /xml>

      I 2 Replies Last reply
      0
      • A Abhishek Sur

        Why do you need to write a new class when there is already a concept of Extension Function.. use like this

        public int GetNumber(this string str)
        {
        // Apply your regular expression here to get the number.
        }

        Now to call just use myString.GetNumber. :)

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        My Latest Articles-->** Microsoft Bing MAP using Javascript
        CLR objects in SQL Server 2005
        Uncommon C# Keywords
        /xml>

        I Offline
        I Offline
        Imran Khan Pathan
        wrote on last edited by
        #3

        Abhishek Sur wrote:

        Now to call just use myString.GetNumber.

        I am not able to get it in intellisense when I am trying to get myString.GetNumber. Thanks Imrankhan

        please don't forget to vote on the post that helped you.

        1 Reply Last reply
        0
        • A Abhishek Sur

          Why do you need to write a new class when there is already a concept of Extension Function.. use like this

          public int GetNumber(this string str)
          {
          // Apply your regular expression here to get the number.
          }

          Now to call just use myString.GetNumber. :)

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Microsoft Bing MAP using Javascript
          CLR objects in SQL Server 2005
          Uncommon C# Keywords
          /xml>

          I Offline
          I Offline
          Imran Khan Pathan
          wrote on last edited by
          #4

          I got the solution.I got error at compile time that says 'Extension methods must be static' so I create one class named StringHelpers as static. public static class StringHelpers { public static int GetNumber(this string str) { return 1; } } Thanks Imrankhan

          please don't forget to vote on the post that helped you.

          A 1 Reply Last reply
          0
          • I Imran Khan Pathan

            I got the solution.I got error at compile time that says 'Extension methods must be static' so I create one class named StringHelpers as static. public static class StringHelpers { public static int GetNumber(this string str) { return 1; } } Thanks Imrankhan

            please don't forget to vote on the post that helped you.

            A Offline
            A Offline
            Abhishek Sur
            wrote on last edited by
            #5

            Yes .. sorry.. I just overlook on that. Really extension functions should be declared as static. I think now you have made it working .. ;) cheers... :thumbsup:

            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


            My Latest Articles-->** Microsoft Bing MAP using Javascript
            CLR objects in SQL Server 2005
            Uncommon C# Keywords
            /xml>

            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