• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 39
  • Last Modified:

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.
0
CipherIS
Asked:
CipherIS
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
you need to use something like:
public string extendedPrice { get {return  (Convert.ToDouble(UnitPrice) * Convert.ToDouble(Total)).ToString("#,##0.00"); } }

Open in new window

1
 
Florian LenschowCommented:
You could also write an init function that computes extendedPrice and call this function in the objects constructor.
0
 
CipherISAuthor Commented:
@Eric - Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now