开发者

Problem with C# conditional [closed]

开发者 https://www.devze.com 2023-03-10 22:18 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Hi I have the following code:

medianListTester.cfyPE = 0;

if (medianListTester.cfyPE != 0 || testStock.getCEPS() != 0)
{
    medianCYPE_price = medianListTester.cfyPE * testStock.getCEPS();
    counter++;
}

else
   //do something else

but it开发者_运维问答 doesn't seem to "do something else" and still fires the code inside the conditional. Am I doing something wrong here? Why won't it run the code in the if statement?


If testStock.getCEPS() is not 0 you will not hit the else block. Depending on what you need and what you are expecting you might need to use && instead of || ?


medianListTester.cfyPE is set to 0, so the first clause in your if statement evaluates to false. What does TestStock.getCEPS()? If that is non-zero, then that will evaluate to true, and the entire if statement is then true.


I can't really tell what you're asking, but you're calling getCEPS() twice, and perhaps that is returning two different values, and perhaps that is causing your trouble.

Try this instead:

medianListTester.cfyPE = 0;
var ceps = testStock.getCEPS();
if (medianListTester.cfyPE != 0 || ceps != 0) 
{     
    medianCYPE_price = medianListTester.cfyPE * ceps;     
    counter++; 
}  
else    
    //do something else 


It sounds like testStock.getCEPS() is returning something other than 0. That would explain why it never hits do something else. Have you checked what this is returning?


What's the value of the call to testStock.getCEPS()? If that's not 0 it will still enter that code block. Also, I don't know the details of the object referenced by medianListTester, but it is possible that there is a getter/setter that isn't working right.

Your best bet for solving this is to print out or somehow look at the values of both medianListTester.cfyPE and testStock.getCEPS() right after the assignment but just before the if statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消