What does result.IsVisible equal?
if(a==b)
result.IsVisible = obj1.status.abc_REPORT == 'Y'
&& obj1.AnotherValue.ToBoolean() == false;
That depends on the values of obj1.status.abc_Report and obj1.AnotherValue.ToBoolean() (and it all depends on whether a==b or not).
I'm not quite sure of what the real question is here - which bit is confusing you?
One bit which may be confusing you is the shortcircuiting && operator (and possibly the lack of bracing!)
The && operator will only evaluate its right hand side if the left hand side evaluates to true: and the overall result of the expression is true if and only if both sides evaluates to true. (I'm assuming no strange user-defined conversions here.)
So another way of writing it would be:
if (a == b)
{
bool visibility = false;
if (obj1.status.abc_REPORT == 'Y')
{
if (obj1.AnotherValue.ToBoolean() == false)
{
visibility = true;
}
}
result.IsVisible = visibility;
}
Note that a condition comparing Booleans, like this:
obj1.AnotherValue.ToBoolean() == false
would usually be written like this:
!obj1.AnotherValue.ToBoolean()
(Note the exclamation mark at the start - the logical "not" operator.)
The same as this, in many less lines:
if (a==b) {
if (obj1.status.abc_REPORT == 'Y') {
if (obj1.AnotherValue.ToBoolean() == false) {
result.IsVisible = true;
}
else {
result.IsVisible = false;
}
}
else {
result.IsVisible = false;
}
}
In simple words:
If a is equal to b:
result will be visible only if:
object1's status's abc_report is Yes(Y = Yes most probably) AND object1's other value cannot be converted to a Boolean
I'm guess result.IsVisible is a boolean
It will be true if the following conditions are true:
obj1.status.abc_REPORT == 'Y'
and
obj1.AnotherValue.ToBoolean() == false
Also, a == b must be true to enter the initial if
lets go line by line:
if(a==b)
obvious if value of a equals value of b the execute following line
result.IsVisible = obj1.status.abc_REPORT == 'Y'
&& obj1.AnotherValue.ToBoolean() == false;
result is some object (maybe winforms controls etc) which has a property IsVisible set it to true if obj1.status.abc_REPORT is equal to 'Y' and also obj1.AnotherValue.ToBoolean() is equal to false;
Related
My question is why this condition in a for loop makes it cancel out iteration, however it seems to me that condition is fulfilled?! If I try with one of two given variables without using AND operator looping works and continues infinitely.
bool a = false;
bool b = false;
for (; a && b == false; )
{
Console.WriteLine("");
}
This condition
a && b == false
means
a && (b == false)
Since && does short circuit evaluation, the first false will result in false for whole expression, and thus there is no need to evaluate second expression.
Also to add, even with a single & (which doesn't perform short circuit evaluation, your complete condition will result in false.
If you want to compare both a and b to false you can do:
a == false && b == false
or
!a && !b
You should also consider using a while loop, if there is no iteration variable involved.
https://msdn.microsoft.com/en-us/library/aa691323%28v=vs.71%29.aspx
Because the == is evaluated before &&, the condition is a && (b == false)
so false.
For example i want to write:
txt1.Text=="A" || txt2.Text="A" ? return true : return false
I know how to work with one condition but with two i don't know.
The txt2.Text="A" instead of txt2.Text=="A" it's not what i meant.
My question was how do i add a condition to this specific if.
Yes, i know how to use in if.
the regular way to use in the other if is:
txt1.Text=="A"? return true: return false
and i want to improve that.
The code that i have written doesn't work.
Thank you
Are you looking for
return txt1.Text == "A" || txt2.Text == "A";
?
I think this is what you are looking for.
if(txt1.Text == "A" || txt2.Text == "A") //checks if txt1 is A OR txt2 is A
{
return true;
}
else
{
return false;
}
I have an xml filelike so
<Config>
<Allowed></Allowed>
</Config>
The Allowed tag is read like this:
string isAllowed = (string)xml.Root
.Element("Config")
.Elements("Allowed")
.SingleOrDefault();
isAllowed is supposed to take a default value of true when
The tag is not present
Is present but is empty
Has any other value other than true, false, yes, or no.
Here is the code which does this:
if (isAllowed == null)
{
DoSomething();
return true;
}
if (isAllowed.Length == 0)
{
DoSomething();
return true;
}
if (isAllowed.Length != 0)
{
if (isAllowed.ToUpper() != "FALSE" && isAllowed.ToUpper() != "NO")
{
DoSomething();
return true;
}
}
There's got to be a better way to do this?
if (isAllowed == null)
{
DoSomething();
return true;
}
if (isAllowed.Length == 0)
{
DoSomething();
return true;
}
Can be replaced with:
if (string.IsNullOrEmpty(isAllowed)
{
DoSomething();
Return true;
}
But actually, given your criteria I think string.IsNullOrWhiteSpace(isAllowed) would be more appropriate as it will return true if the tag's contents are "empty".
Also, you don't need the following condition a second time, because if if the condition was met the first time around the function will return (short circuit evaluation). This means that the statements you currently have in the second If block would never be executed anyway.
if (isAllowed.Length != 0)
My first instinct to make this cleaner was to take the same approach as Jon did in his answer, there's no advantage in repeating it. However, I did consider this as another good design as should you introduce more conditions it will be much cleaner:
private static bool Validate(string isAllowed)
{
var defaultTrueConditions = new[] {"true", "false", "yes", "no"};
if (string.IsNullOrWhiteSpace(isAllowed) ||
defaultTrueConditions.Contains(isAllowed, StringComparer.OrdinalIgnoreCase))
{
DoSomething();
return true;
}
return false;
}
It sounds like you might be better off like this:
// This deals with the nullity aspect. (The "Yes" is just for clarity - it could
// be any value other than "No" or "False" in some form.)
isAllowed = isAllowed ?? "Yes";
bool isFalse = isAllowed.Equals("No", StringComparison.OrdinalIgnoreCase) ||
isAllowed.Equals("False", StringComparison.OrdinalIgnoreCase);
return !isFalse;
Basically the fact that you're defaulting to true means that the return value should only be false if you find an element and it's got a value of No or False, in a case-insensitive way. Note that I've used an ordinal match here - you may want to change that, e.g. to CurrentCultureIgnoreCase or InvariantCultureIgnoreCase.
It's not clear where the DoSomething method call comes in, but I would separate that out anyway. Write one method which just determines the appropriate value, as shown above - and then have:
bool allowed = CheckAllowed(doc);
if (allowed)
{
DoSomething();
}
// And use allowed here too
That's a much cleaner separation to my mind.
Trying to write a piece of code to compare two strings. If either are equal to the textbox then it opens a new winform. I know how to do the winform part.
string CreditCard1 = "Some numbers";
string CreditCard2 = "Some Numbers";
string RFIDCard1 = "E00700000330E44C";
string RFIDCard2 = "E00700000338E539";
if(CardScan_txtBx = CreditCard1 || RFIDCard1)`
I get an error from MSVS 2010 saying:
Operator '||' cannot be applied to operands of type 'string' and 'string'
Is there a way to do what I want?
This line is the culprit:
if(CardScan_txtBx = CreditCard1 || RFIDCard1)`
Try:
if(CardScan_txtBx.Text == CreditCard1 || CardScan_txtBx.Text == RFIDCard1)
On a side note, it scares me that you're apparently working with credit card information, but don't know how to compare values in a text box. I really, really hope, for the sake of your customers, that you plan on investigating how to securely manage that information.
There are three problems here:
You cannot compare against multiple values using an OR (||). This is a surprisingly common misconception, but makes no sense to the compiler.
Comparison in C# is done with ==. = is for assignment.
A TextBox is not a string; you need to use its Text property to get or set the text it contains.
So in the end, your if statement should look like this:
if(CardScan_txtBx.Text == CreditCard1 || CardScan_txtBx.Text == RFIDCard1) {
// ...
}
Ok you have 2 issues here, firstly single equals is assignment not comparison and secondly each argument separated by an or needs to be a bool, ie should be
if(CardScan_txtBx == CreditCard1 ||CardScan_txtBx == RFIDCard1)
Could you use else if?
if(CardScan_txtBx == CreditCard1)
{
//Do something
} else if(CardScan_txtBx == RFIDCard1)
{
//Do something
}
The other answers have the correct code, here is an explanation of the why. When you use the || operator, it is expecting an expression on either side to be something that evaluates to a bool (true or false). When you wrote CardScan_txtBx.Text == CreditCard1 || RFIDCard1 you have an statement that evaluates to a bool on the left, CardScan_txtBx.Text == CreditCard1 and you have a statement that evaluates to string on right RFIDCard1 Because a string is not a bool, you get the compile time error. that is why you must repeat the == operator on the right hand side so that you say CardScan_txtBx.Text == RFIDCard1
Try out with following code.....
if (CardScan_txtBx.Equals(CreditCard1) || CardScan_txtBx.Equals(RFIDCard1))
{
//Code
}
Try this:
if (CardScan_txtBx.Text == CreditCard1 || CardScan_txtBx.Text == RFIDCard1)
See here: http://msdn.microsoft.com/en-us/library/53k8ybth%28v=vs.71%29.aspx
Not: =
Right: expr1 == expr2
You can also use a List.
List<string> listOfValidStrings = new List<string>();
//Initialise all valid strings.
if(listOfValidStrings.contains(txtbox.text())
{ do something.}
This is a really simple question I am sure, but I cannot figure out why this assertion fails...
basically if IsApple is false or IsBannana is false assertion should fail, however if one of the two is true assertion should pass, could anyone explain why this assertain fails?
[Test]
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (!IsApple || !IsBannana)
Assert.Fail();
Assert.Pass();
}
What you're saying makes no sense.
Here's how I (and the compiler) understand what you're saying:
basically if IsApple is false or IsBannana is false assertion should fail
If IsApple is false the assertion should fail
If IsBanana is false, the assertion should fail
In other words, if one of them is false, you don't care whether or not the other is also false.
however if one of the two is true assertion should pass
If one of them is true, you don't care whether the other one is also true.
Those requirements contradict each others.
Perhaps you meant "if IsApple is false AND IsBanana is false"; that is, if they are both false.
But what you wrote was "if IsApple is false OR IsBanana is false", that is, if one of them are false.
!IsBannana is true so the if evaluates to true.
I bet you wanted:
if(!IsApple && !IsBananna)
Assert.Fail();
The assert will fail if EITHER is false, it will only pass if BOTH are true.
Edit: I would re-write this as:
if (IsApple || IsBanana)
{
Assert.Pass();
}
else
{
Assert.Fail();
}
Interestingly, the question as posed cannot be answered (since if one is true and the other is false according to the question the expected result is amiguous).
Do this instead:
if(!IsApple && !IsBannana)
if (IsApple != IsBannana)
Assert.Fail();
I think this trick != is a C# FAQ as poor man's XOR
You dont want the case where neither a nor b is false, or rather at least one should be true, so
[Test]
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (!(IsApple || IsBannana))
Assert.Fail();
Assert.Pass();
}
Your pass condition is IsApple || IsBannana, so your fail condition can be written as:
if (!(IsApple || IsBannana))
Or alternatively you can say both must be false to fail:
if ((IsApple==false) && (IsBannana==false))
Which usually is written as:
if (!IsApple && !IsBannana))
Personally I think the first of these three alternatives is the best. But that's just a stylistic choice. functionally they are equivalent.
Or even better, you could switch your then and else part of the if:
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (IsApple || IsBannana)
Assert.Pass();
else
Assert.Fail();
}
Or even throw out the if statement entirely:
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
Assert.IsTrue(IsApple || IsBannana);
}
Your pass condition contradicts your fail condition, because if only one is true, the other one is false and thus fulfills the fail condition.
if you want it to make sense, change it to this:
if (!IsApple && !IsBanana)
Assert.Fail():
or, if you also don't want it to be an apple and banana at the same time
if (IsApple == IsBanana)
Assert.Fail():
(!A|| !A) = !!(!A || !B) = !(A && B) = !(true && false) --> false