Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

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.UnitPrice.get"
"A field initializer cannot reference the non-static field, method, or property 'MyApp.Models.ItemsModel.Total.get"

 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);
    }

Open in new window


How do I return a calculated field in the model.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Florian Lenschow
Florian Lenschow

You could also write an init function that computes extendedPrice and call this function in the objects constructor.
Avatar of CipherIS

ASKER

@Eric - Thanks.