Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to write a conditional statement that will check while clicking the button, to check if at least one checkbox is checked.
So an example would be:
if (
checkbox_delete.Checked = false &&
checkbox_export = false &&
checkbox_name = false &&
checkbox_PST = false
)
{
string messageboxtext = "Please check at least one of the checkboxes.";
MessageBox.Show(messageboxtext);
}
I get error message saying:
Operator '&&' cannot be applied to operands of type 'bool' and 'System.Windows.Forms.CheckBox'
Can anyone help to figure out what am I doing wrong?
Ps. I also tried doing
if (
(checkbox_delete.Checked = false) &&
(checkbox_export = false) &&
(checkbox_name = false) &&
(checkbox_PST = false)
)
{
string messageboxtext = "Please check at least one of the checkboxes.";
MessageBox.Show(messageboxtext);
}
But I then get:
Cannot implicitly convert type 'bool' to 'System.Windows.Forms.CheckBox'
There are a couple of issues with your current code:
As you've spotted, you need to reference the property Checked. See https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checked(v=vs.110).aspx for related documentation.
Also, instead of using = you should use ==. The first is used for assignment; the second for equality comparison. See https://stackoverflow.com/a/4704401/361842 for more information on this point. Try running the below code a console app or in LinqPad to see some of the odd results you can get when this is misunderstood:
Example of Issue
bool something = true;
Debug.WriteLine(something); //returns true
if (something = false) {
Debug.WriteLine("this won't show");
} else {
Debug.WriteLine("this will show"); //this is output, which you'd maybe expect, but for the wrong reasons...
}
Debug.WriteLine(something); //returns false, as we've reassigned it accidentally above
if (something = false) {
Debug.WriteLine("this won't run");
} else {
Debug.WriteLine("surprise!"); //even though we saw above that something is now false, because we assign the value false to something and then evaluate the value of something (rather than comparing `something` with `false`, finding them equanlt and thus returning `true`; so the above statement effectively reads `if (false)`
}
Outputs:
True
this will show
False
surprise!
... beyond that, your current code would also uncheck all of your checkboxes, as you're setting the checked property to false.
Amended code:
if (
checkbox_delete.Checked == false &&
checkbox_export.Checked == false &&
checkbox_name.Checked == false &&
checkbox_PST.Checked == false
)
{
string messageboxtext = "Please check at least one of the checkboxes.";
MessageBox.Show(messageboxtext);
}
An alternate method to saying "if all are false" would be to say "if none are true". You may find that easier to read; but this is down to personal preference. See below for an example:
if (!(
checkbox_delete.Checked ||
checkbox_export.Checked ||
checkbox_name.Checked ||
checkbox_PST.Checked
))
{
string messageboxtext = "Please check at least one of the checkboxes.";
MessageBox.Show(messageboxtext);
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The while loop
public static bool Authentication()
{
bool isAuthenticated = false;
int count = 4;
while (isAuthenticated = false && count > 0)
{
Console.WriteLine("Please insert your 4 digit PIN");
string pin = Console.ReadLine();
if (pin == "1704")
{
isAuthenticated = true;
return isAuthenticated;
}
else
{
count--;
Console.WriteLine($"Wrong PIN: {count} Input remained ");
}
}
return isAuthenticated;
}
I haven't been able to understand, when the debugger goes to the while loop, it does not execute it. It may be the condition of the while that does not fit the logic.
I believe you are using the = as an assigning operator in the condition of the loop. You could try !isAuthenticated instead of isAuthenticated == false and see if that works
while (!isAuthenticate && count > 0)
I believe you were assigning false to isAuthenticate, so the condition could never be true.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So this should be super simple , perhaps indexOf is not the way to go
var incoming = "MDd"; // incoming data
List<string> cities = new List<string>();
cities.Add("MD");
cities.Add("Mumbai");
cities.Add("Berlin");
cities.Add("Istanbul");
if(cities.IndexOf(incoming) != 1 )
{
Console.WriteLine("found");
}
else
{
Console.WriteLine("not found");
}
I am seeing "found" with linqpad output , whether it is correct "MD" or "MDd" why? and what do I change to fix this?
From the documentation of IndexOf:
Return Value The zero-based index position of value if that string is
found, or -1 if it is not. If value is String.Empty, the return value
is 0.
When value is not found in the string, it returns -1, otherwise it returns index position, which is greater than or equal to 0. So either check if index is not -1 or check if it >= 0. I personally prefer the latter one:
// if (cities.IndexOf(incoming) != -1)
if (cities.IndexOf(incoming) >= 0)
{
Console.WriteLine("found");
}
This is a typo error. You want to check for -1 rather than 1 in your if statement:
if(cities.IndexOf(incoming) != -1)
{
Console.WriteLine("found");
}
else
{
Console.WriteLine("not found");
}
I have this piece of code which i simplified from my app. It does what I want but I know its not put in the most efficient way because I'm still having some trouble understanding the && and & operators.
if (AgeInput.Text.Equals(""))
{
Textblock1.Text = "✘";
}
else if (AgeInput.Text.All(Char.IsNumber)){
Textblock1.Text = "✔";
//DO-THIS-A
}
else
{
Textblock1.Text = "✘";
}
I need it to make sure there is no white spaces in the string and to also check so its not empty and finally check if its a number, If it ticks all those requirements it will //DO-THIS-A
Whats the most efficient way to do this?
EDIT:
If somebody knows how to make a XAML textbox numerical only (so no whitespaces) that would be even better (only a single property or don't worry otherwise)
if(!String.IsNullOrEmpty(AgeInput.Text) && AgeInput.Text.All(char.IsNumber))
Textblock1.Text = "✔";
else
Textblock1.Text = "✘";
String.IsNullOrEmpty returns true if the input is as stated: Null or Empty.
We Invert that with the "!", so that it returns true if it isn't empty.
Then we add the && Operator to expand the if condition and ask if the text only contains numbers.
Also look here: For a description of the difference between &, && and |, ||
Not really sure I understand your question, because && and & are for totally different uses.
if (string.IsNullOrWhiteSpace(AgeInput.Text))
{
Textblock1.Text = "✘";
}
else if(Char.IsNumber(AgeInput.Text.All))
{
Textblock1.Text = "✔";
}
The & is a binary operator and && is a logical operator.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
MyProject.MyForms.m_FormBeingCreated.Add(typeof(T), null);
try
{
try
{
result = Activator.CreateInstance<T>();
return result;
}
object arg_96_0;
TargetInvocationException expr_9B = arg_96_0 as TargetInvocationException;
int arg_B8_0;
if (expr_9B == null)
{
arg_B8_0 = 0;
}
else
{
TargetInvocationException ex = expr_9B;
ProjectData.SetProjectError(expr_9B);
arg_B8_0 = (((ex.InnerException != null) > false) ? 1 : 0);
}
endfilter(arg_B8_0);
}
finally
{
MyProject.MyForms.m_FormBeingCreated.Remove(typeof(T));
}
}
result = Instance;
return result;
}
/// What have i done wrong?
Keep getting errors about: Expected catch or finally #object arg_96_0;
Operator '>' cannot be applied to operands of type 'bool' and 'bool' #ex.InnerException !=null)
The name 'endfilter' does not exist in the current context. #endfilter(arg_B8_0);
Your first problem is with this line of code
arg_B8_0 = (((ex.InnerException != null) > false) ? 1 : 0);
In C# a boolean cannot be greater than or less than, so try changing that to
arg_B8_0 = (((ex.InnerException != null) != false) ? 1 : 0);
Also, as pointed out by Jeroen and others in comments, this piece of code is not very clean and is doing some evaluations it doesn't need to do.
arg_BB_0 = ex.InnerException != null ? 1 : 0;
This is a much better way to write the expression and achieve the same goal.
Your next issue is that all try statements must be accompanied by a catch or finally statement.
Try reading this article on MSDN about proper use of try-catch blocks.
As far as getting an error that says endfilter doesn't exist in the current context, this means that you need to check the scope of where endfilter is declared. endfilter is probably not declared where it accessible in the current scope. MSDN can again be helpful here, This is a good place to get started understanding scope.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I don't know before this asked or not, but I am confuse in below of code block.
CODE 1
if (String.IsNullOrEmpty(control.Text.Trim()))
{
// Code to execute
}
CODE 2
if (control.Text.Trim() == "")
{
// Code to execute
}
CODE 3
if (control.Text.Trim() == null)
{
// Code to execute
}
CODE 4
if (control.Text.Trim() == string.Empty)
{
// Code to execute
}
According to me all are working to me.
I just feeling wonder that what is the different in between in this 4 code block.
Let's start from primitives:
The first block checks if the string control.Text.Trim() is null or String.Empty.
The second block checks if the string control.Text.Trim() is "".
The third block checks if the string control.Text.Trim() is null.
The fourth block checks if the string control.Text.Trim() is String.Empty; this is exactly the same as the second block: "" equals String.Empty.
Fine, that's easy to understand. However, note that String.Trim() will never return null. Thus, the first block is equivalent to control.Text.Trim() == String.Empty. This is same as the second block and the fourth block, again because "" equals String.Empty. The third block will never be hit, ever.
Thus, the first, second and fourth blocks are equivalent to checking if control.Trim the empty string and the third block is useless and impossible to satisfy. Be careful, if control is null or control.Text is null you will hit an exception. Thus, you should strongly consider using `String.IsNullOrWhiteSpace and replacing everything with:
if(control != null && String.IsNullOrWhiteSpace(control.Text)) {
// code to execute
}
(unless you have some sort of guarantee that control is not null, in which case leave off the first part of the if).
More proper would be:
if (String.IsNullOrWhiteSpace(control.Text))
{
// Code to execute
}
that way you avoid null reference exception.
All your examples have the same bug, they will throw exception if variable is null.
You should also see diference between string.empty ("") and null, those are not the same things. Code 4 and code 2 are the same but both would throw if Text is null.
control.Text.Trim() is never going to be null
control might be
control.Text might be
in which case all three versions will blow chunks...
private static bool validControl(Control argControl)
{
return (argControl != null) && (argControl.Text != null) && (argControl.Text.Trim() != ""));
}
if (validControl(control))
{
// code to execute
}
maybe