-4

I'm building a program that has to check if any of multiple variables are negative but it's telling me that Operator || cannot be applied to operands of type 'double' and 'double'.

if (ASUses || eleven52Uses || UreaUses || PotashUses || FillerUses<0)
{
   MessageBox.Show("Error: one or more of the desired outputs is negative");

}

The problem only exists in the first two variables:

(ASUses || eleven52Uses)
Termininja
  • 6,620
  • 12
  • 48
  • 49
  • possible duplicate of [More advanced OR statement? C#](http://stackoverflow.com/questions/11812494/more-advanced-or-statement-c-sharp) – Jeroen Vannevel May 26 '14 at 16:50
  • @JeroenVannevel: Maybe this requirement is so popular that it should be considered for C# 6 or for a later version ? :) – qqbenq May 26 '14 at 16:57
  • @qqbenq: it would give the design team a nightmare to parse such expressions correctly – Jeroen Vannevel May 26 '14 at 17:01
  • @JeroenVannevel I was just kidding around of course. But still, with some syntax extensions it would be doable. And still a nightmare - which would introduce all new kind of bugs :) – qqbenq May 26 '14 at 17:03

2 Answers2

5

Try this:

if (ASUses<0 || eleven52Uses<0 || UreaUses<0 || PotashUses<0 || FillerUses<0)

(Assuming that all variables are double numbers and not bools of course...)

The logical or operator (||) can only be applied to boolean values. While human logic can easily find out the meaning behind your syntax, the compiler can only read it if it adheres to the language specifications, unfortunately.

Also: I would consider writing a function which checks for validity of the variables and not use so many variables in the if statement, as it will be quite hard to maintain code like that.

qqbenq
  • 10,220
  • 4
  • 40
  • 45
4

That's not how it works, you need to separate each statement. or use an array:

if(new []{ ASUses,eleven52Uses,UreaUses,PotashUses,FillerUses }.Any(x => x < 0))

Also you need < 0 if you want to check for negative numbers

Selman Genç
  • 100,147
  • 13
  • 119
  • 184