Extending Base Type Functionality with Extension Methods
-
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.
-
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.
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> -
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>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.
-
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 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.
-
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.
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>