I have a doubt about a type declaration. In the code below, If I don't convert my already Decimal object to Decimal, I'm receiving this error: Error: No overload for method 'ToString' takes '1' arguments.
Decimal? valorPatrocinio = null;
if (IsPost) {
if (!String.IsNullOrEmpty(Request.Form["valorPatrocinio"]) ){
if (Request.Form["valorPatrocinio"].IsDecimal()){
valorPatrocinio = Request.Form["valorPatrocinio"].AsDecimal();
}else{
ModelState.AddError("valorPatrocinio", "O valor informado não é válido.");
}
}
}else if (detalhe!=null){
valorPatrocinio = detalhe.ValorPatrocinio;
}
//This works: (Decimal)valorPatrocinio).ToString("C2")
//This doesnt works: valorPatrocinio.ToString("C2")
<input type="text" id="valorPatrocinio" name="valorPatrocinio" title="Informe o valor do patrocínio"
value="@(valorPatrocinio!=null ? ((Decimal)valorPatrocinio).ToString("C2") : "")" />
The decimal class supports various implementations of ToString, including one that accepts a format string as you are trying to use. Vars of type decimal? are actually instances of Nullable<T> which only implements ToString with no variables. That is why
you have to cast to decimal in order to use the desired ToString
ronaldorezen...
Member
28 Points
61 Posts
types and variables declaration
Jun 16, 2012 05:21 PM|LINK
I have a doubt about a type declaration. In the code below, If I don't convert my already Decimal object to Decimal, I'm receiving this error: Error: No overload for method 'ToString' takes '1' arguments.
Decimal? valorPatrocinio = null; if (IsPost) { if (!String.IsNullOrEmpty(Request.Form["valorPatrocinio"]) ){ if (Request.Form["valorPatrocinio"].IsDecimal()){ valorPatrocinio = Request.Form["valorPatrocinio"].AsDecimal(); }else{ ModelState.AddError("valorPatrocinio", "O valor informado não é válido."); } } }else if (detalhe!=null){ valorPatrocinio = detalhe.ValorPatrocinio; } //This works: (Decimal)valorPatrocinio).ToString("C2") //This doesnt works: valorPatrocinio.ToString("C2") <input type="text" id="valorPatrocinio" name="valorPatrocinio" title="Informe o valor do patrocínio" value="@(valorPatrocinio!=null ? ((Decimal)valorPatrocinio).ToString("C2") : "")" />AidyF
Star
9204 Points
1570 Posts
Re: types and variables declaration
Jun 16, 2012 06:11 PM|LINK
The decimal class supports various implementations of ToString, including one that accepts a format string as you are trying to use. Vars of type decimal? are actually instances of Nullable<T> which only implements ToString with no variables. That is why you have to cast to decimal in order to use the desired ToString