none
how to add a new extension method to decimal? RRS feed

  • Question

  • I would like to add a new method for parsing the decimal, that I am calling tryParseV1 for example. I would like that I could use this method in the same way taht I can use the TryParse that exists in the decimal type.

    I am trying this:

    namespace MyProject.ExtensionMethods.Decimal
    {
        public static class Decimal
        {
    		public bool TryParseToDecimalAnyDecimalSeparator(string paramText, out decimal? paramDecimalResult, bool paramEmptyStringCorrect)
            {
    			//my code
    		}
    	}
    }

    Then, in the project in which I want to use the method, I add a new reference to MyProject, that has the extension method, and when I want to use the method I try:

    decimal myDecimal;
    decimal.TryParseV1("2.3", out myDecimal, false);

    The problem is that when I write "decimal.", the intellisense say me that there is a conflicto because there are two decimal objects. So to use the extensión method I have to write the complete route:

    MyPrject.ExtensionMethds.Decimal.TryParseV1()

    I would like to know if there is any way to avoid this, and when I write decimal. that there is only one decimal type and with the two method, the default TryParse and my custom parse method.

    Thank so much.

    Wednesday, March 25, 2015 8:56 AM

Answers

  • >>When I said that I want an extension method, is something like the tryparse of the decimal object. I don't need instance an object, in the same of the TryParse method of the type decimal.

    TryParse is not an extension method, it is a static method of the Decimal class.

    >>There is any way to avoid the needed to instance a object of the type to use the extesion method?

    No.

    >>Can I create a static method that is added to the methods of the typpe to be used without needing to instance a object?

    Yes, but then you need to add the static method to the class and you cannot add a method to the built-in Decimal class.

    You can do add a static method to any of your own types though, e.g:

        public class MyDecimal
        {
            decimal _d;
            public MyDecimal(decimal d)
            {
                _d = d;
            }
    
            public static bool MyTryParse(string s, out decimal result)
            {
                return decimal.TryParse(s, out result);
            }
    
        }   
    

                decimal d;
                MyDecimal.MyTryParse("10.0", out d);

     


    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.

    • Marked as answer by ComptonAlvaro Thursday, March 26, 2015 4:22 PM
    Thursday, March 26, 2015 12:19 PM

All replies

  • I might be wrong, but I think that extension methods work as though they are methods on instances of your chosen type (even though they are actually special static methods). I do not think they can be added as static methods on the a type itself as you are trying to do by adding a static method to the 'decimal' type.

    Look at this code:

    public static class String
    {
         public static bool TryParseToDecimalAnyDecimalSeparator(this string d, string paramText, out decimal? paramDecimalResult, bool paramEmptyStringCorrect)
            {
                // ... your code
            }
    }

    Note the first argument "this string d" of the method. This is adding the extension method to the string type. So now you could write:

    string s = "7.3";
    decimal? output;
    
    s.TryParseToDecimalAnyDecimalSeparator("", out output, true);

    Wednesday, March 25, 2015 9:37 AM
  • Make sure you did not do something like

    Decimal decimal = new Decimal();

    else you are in big trouble.

    chanmm


    chanmm

    Wednesday, March 25, 2015 10:20 AM
  • Thanks so much for your attention.

    However I see that your class is static string, while my case is static decimal, because I want to convert the decimal to string.

    Wednesday, March 25, 2015 10:46 AM
  • Thanks so much for your attention.

    However I see that your class is static string, while my case is static decimal, because I want to convert the decimal to string.

    This is not what it looked like in your original post. In that your example code had the out parameter of type "decimal?" and your test code appeared to be trying to convert the string "2.3" to a decimal.

    Your code example:

    public bool TryParseToDecimalAnyDecimalSeparator(string paramText, out decimal? paramDecimalResult, bool paramEmptyStringCorrect)
    
    ////
    
    decimal myDecimal;
    decimal.TryParseV1("2.3", out myDecimal, false);

    So...which way round are you really trying to do this? Decimal to string or string to decimal??

    Wednesday, March 25, 2015 11:15 AM
  • Well, I think that I am confused about the extensions methods.

    When I said that I want an extension method, is something like the tryparse of the decimal object. I don't need instance an object, in the same of the TryParse method of the type decimal.

    I can do that:

    decimal myResult;
    decimal.TryParse("2.3", out myResult);
    

    In this case, I use the static method of the decimal type, but I don't instance any object.

    With extension mehtods, it seems that I need to instance a object of the decimal type, so I need to do:

    decimal myDecimal = 2.3m;
    myDecimal.MyExtensionMethod();
    

    There is any way to avoid the needed to instance a object of the type to use the extesion method? Can I create a static method that is added to the methods of the typpe to be used without needing to instance a object?

    Thank so much.

    Wednesday, March 25, 2015 11:47 AM
  • There is any way to avoid the needed to instance a object of the type to use the extesion method? Can I create a static method that is added to the methods of the typpe to be used without needing to instance a object?

    Looks like you want to create a static extension method for the type decimal. We can't create static extension method for types in .Net. But I think you could just create an extension method for string, for example:

    public static class MyExtensions
        {
            public static decimal ConvertToDecimal(this string originalString)
            {
                decimal d = 0.0m;
                //convert the string to decimal here...
                return d;
            }
        }
    
    
        //Use the extension method
        string decimalString="2.3";
        decimal result=decimalString.ConvertToDecimal();


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 26, 2015 8:46 AM
    Moderator
  • >>When I said that I want an extension method, is something like the tryparse of the decimal object. I don't need instance an object, in the same of the TryParse method of the type decimal.

    TryParse is not an extension method, it is a static method of the Decimal class.

    >>There is any way to avoid the needed to instance a object of the type to use the extesion method?

    No.

    >>Can I create a static method that is added to the methods of the typpe to be used without needing to instance a object?

    Yes, but then you need to add the static method to the class and you cannot add a method to the built-in Decimal class.

    You can do add a static method to any of your own types though, e.g:

        public class MyDecimal
        {
            decimal _d;
            public MyDecimal(decimal d)
            {
                _d = d;
            }
    
            public static bool MyTryParse(string s, out decimal result)
            {
                return decimal.TryParse(s, out result);
            }
    
        }   
    

                decimal d;
                MyDecimal.MyTryParse("10.0", out d);

     


    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.

    • Marked as answer by ComptonAlvaro Thursday, March 26, 2015 4:22 PM
    Thursday, March 26, 2015 12:19 PM
  • @Caillen, I just wanted to correct you. You said:

    >>Looks like you want to create a static extension method for the type decimal. We can't create static extension method for types in .Net.<<

    But, that's not true. You CAN create extension methods for types, I  have several in my applications.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, March 26, 2015 3:37 PM
    Moderator
  • But, that's not true. You CAN create extension methods for types, I  have several in my applications.


    Can you share an example of that? As far as I know, extensions methods apply to an instance of a type, not on a type itself. 

    Thursday, March 26, 2015 3:58 PM
    Moderator
  • I may have misunderstood what Caillen meant, but *I* meant an extension on the instance of the type (so, in this case, an instance of a decimal variable).

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, March 26, 2015 4:03 PM
    Moderator
  • Pfew.. thanks. I feared I missed something. ;-)
    Thursday, March 26, 2015 4:07 PM
    Moderator
  • @Caillen, I just wanted to correct you. You said:

    >>Looks like you want to create a static extension method for the type decimal. We can't create static extension method for types in .Net.<<

    But, that's not true. You CAN create extension methods for types, I  have several in my applications.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    I mean static extension method. I'm sure we can't.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 31, 2015 2:40 AM
    Moderator
  • Caillen, it depends on what you mean ... notice my reply to Sander. I meant an extension on the instance of the type (so, in this case, an instance of a decimal variable), but that's probably not what you meant and I misunderstood that at the time.

    The OP (ComptonAlvaro) didn't want the kind of extension I meant (on an instance of the type), he wanted the kind of extension you meant (on the type itself), which can't be done. So, you were right, I was wrong ... sorry!


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, April 1, 2015 2:54 PM
    Moderator