locked
EF cannot make up it mind on string formatting RRS feed

  • Question

  • User379720387 posted

    "Charge" is type money

    "creditcared" is also the same type

    txn.Transaction.creditcard.ToString()  compiler is happy

    txn.Transaction.Charge.ToString() compliler complains about "no overload for method ToString() takes one argument"

    ok so I can change to:

    oldtxn.Transaction.Charge.ToString("C", culture)
    

    Now it complainsabout "no overload for method 'ToString' takes 2 arguments"

    Why are these two decimals treated so differently?

    Friday, January 20, 2017 7:14 PM

Answers

  • User379720387 posted

    The solution is:

    oldtxn.Transaction.Charge.Value.ToString("C", culture)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 13, 2017 3:30 AM

All replies

  • User753101303 posted

    Hi,

    You really have a "no overload for method ToString() takes one argument" message on a ToString() call with no argument? Double check which type(s) are used and use perhaps "Go to definition" on ToString to see what happens.

    Could it be that you are not looking at the correct ToString call??

    Friday, January 20, 2017 10:41 PM
  • User379720387 posted

    The solution is:

    oldtxn.Transaction.Charge.Value.ToString("C", culture)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 13, 2017 3:30 AM
  • User753101303 posted

    So in one case you had a decimal and in the other case a nullable decimal. Are you sure this value won't ever be null here ?

    See https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx ("nullable types") for details.

    Monday, February 13, 2017 9:03 AM
  • User379720387 posted

    All these are (money, null)

    No I don't know that they will never be null.   What approach do you suggest as a fix for that possibility?

    Monday, February 13, 2017 1:34 PM
  • User753101303 posted

    And you are using that in which context? This is to show those values in your UI ? You are assigned yourself the values to those controls rather than using data binding/model binding  ? I would use a DisplayFormat attribute to have ASP.NET doing the right thing for me.

    Monday, February 13, 2017 1:50 PM
  • User379720387 posted

    Creating a pdf with iTextSharp, and Razor C#. There is no data/model binding.

    new Chunk(String.Format("{0:C}", oldtxn.Transaction.Charge.Value), arial12);
    
    Monday, February 13, 2017 2:03 PM