CipherIS
asked on
C# Return Calculated Field in Model
I want ExtendedPrice to be calculated from values of two fields below. I am getting below errors:
"A field initializer cannot reference the non-static field, method, or property 'MyApp.Models.ItemsModel.U nitPrice.g et"
"A field initializer cannot reference the non-static field, method, or property 'MyApp.Models.ItemsModel.T otal.get"
How do I return a calculated field in the model.
"A field initializer cannot reference the non-static field, method, or property 'MyApp.Models.ItemsModel.U
"A field initializer cannot reference the non-static field, method, or property 'MyApp.Models.ItemsModel.T
public class ItemsModel
{
public string CustomerName { get; set; }
public string PO { get; set; }
public string DateOrder { get; set; }
public string Total { get; set; }
public string UnitPrice { get; set; }
public string extendedPrice = Convert.ToDouble(UnitPrice) * Convert.ToDouble(Total);
}
How do I return a calculated field in the model.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could also write an init function that computes extendedPrice and call this function in the objects constructor.
ASKER
@Eric - Thanks.