Sagar Upadhyay
Sagar Upadhyay

Reputation: 819

Statement return invalid output

See the below code

   totLen = (r.Field<int>("Quantity") <= 0 ? 1 : r.Field<int>("Quantity")) *
   ((r.Field<decimal>("Breath") <= 0 ? 1 : r.Field<decimal>("Breath")) * 
   (r.Field<decimal>("Length") <= 0 ? 1 : r.Field<decimal>("Length")) * 
   (r.Field<decimal>("Height") <= 0 ? r.Field<decimal>("Height") : r.Field<decimal>("Height")))

In this code I had think that if any value of the r.Field(COLUMN NAME) come zero than it should be replaced by 1 only for multiplication by using ? and : conditional operator but its gives me wrong output and return a ZERO(0)...
Can Any one suggest me why this happening?

Upvotes: 1

Views: 66

Answers (2)

Kiran1016
Kiran1016

Reputation: 914

 totLen = (r.Field<int>("Quantity") <= 0 ? 1 : r.Field<int>("Quantity")) *
 ((r.Field<decimal>("Breath") <= 0 ? 1 : r.Field<decimal>("Breath")) * 
 (r.Field<decimal>("Length") <= 0 ? 1 : r.Field<decimal>("Length")) * 
 (r.Field<decimal>("Height") <= 0 ? 1 : r.Field<decimal>("Height")))

Try this

Upvotes: 1

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41767

If the "Height" field is 0 you're still using it in your multiplication.

Change

r.Field<decimal>("Height") <= 0 ? r.Field<decimal>("Height") : r.Field<decimal>("Height")

to

r.Field<decimal>("Height") <= 0 ? 1 : r.Field<decimal>("Height")

Upvotes: 1

Related Questions