Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 22 days ago.
Improve this question
I used the following code to get the active network card of the system.
My problem is that the active network card of my system is Wi-Fi, but the system detects Local Area Connection!
While this network card does not exist!
I tried this :
var activeAdapter = networks.First(x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback
&& x.NetworkInterfaceType != NetworkInterfaceType.Tunnel
&& x.OperationalStatus == OperationalStatus.Up
&& x.Name.StartsWith("Ethernet") || x.Name.StartsWith("Wi-Fi") || x.Name.StartsWith("Local") == true );
label2.Text = activeAdapter.Name;
precedence of logical operators
&& x.Name.StartsWith("Ethernet") || x.Name.StartsWith("Wi-Fi") || x.Name.StartsWith("Local") == true );
shoudl be
&& (x.Name.StartsWith("Ethernet") || x.Name.StartsWith("Wi-Fi") || x.Name.StartsWith("Local") == true ));
Related
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 3 years ago.
Improve this question
I have call for an written exam for Software Engineer. They give a queastion but i think i can not answer it properly. The queastion was
if(type == "string" || type == "int" || type ="char" || type == "double")
return true;
else
return false;
Remove the or operator but it still to give the same output.
Using linq,
string[] primitiveTypes = new string[4] {"int", "double", "char", "string"};
string type = "double";
Contains()
return primitiveTypes.Contains(type);
Any()
return primitiveTypes.Any(x => x == type);
Array.Exists()
return Array.Exists(primitiveTypes, x => x == type);
Array.IndexOf()
return Array.IndexOf(primitiveTypes, type) > -1;
I am not sure if I got your question rigth. If yes, then this is the correct answer:
if(!(type != "string" && type != "int" && type != "char" && type != "double"))
There is possibly a typo in there. Or maybe the question is designed to be a trick question. I see ...type == "int" || type ="char"... and because the second part has a single equals sign you are going to get Operator '||' cannot be applied to types of bool and string.
I would use a switch statement:
switch (type.ToLower())
{
case "string":
case "int":
case "char":
case "double":
return true;
default:
return true;
}
But you could use && like this:
if (!(type != "string" && type != "int" && type != "char" && type != "double"))
return true;
else
return false;
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 4 years ago.
Improve this question
I have below if-else statement, and I want to optimize it more.
Is there any way to optimize it more
if(entity[attributeName] != null)
{
if (entity.FormattedValues.Contains(attributeName) && entity[attributeName].GetType() != typeof(EntityReference))
{
return entity.FormattedValues[attributeName];
}
else
{
return GetDisplayObjectFromRawValue(entity[attributeName]);
}
}
else
{
if (entity.FormattedValues.Contains(attributeName))
{
return entity.FormattedValues[attributeName];
}
else
{
return GetDisplayObjectFromRawValue(entity[attributeName]);
}
}
If it's just for length of code, you could rewrite it like so:
if (entity.FormattedValues.Contains(attributeName)
&& (entity[attributeName] == null || entity[attributeName].GetType() != typeof(EntityReference)))
{
return entity.FormattedValues[attributeName];
}
else
{
return GetDisplayObjectFromRawValue(entity[attributeName]);
}
Or expressed as a ternary statement:
return entity.FormattedValues.Contains(attributeName)
&& (entity[attributeName] == null || entity[attributeName].GetType() != typeof(EntityReference))
? entity.FormattedValues[attributeName]
: GetDisplayObjectFromRawValue(entity[attributeName]);
I've simplified your original code down to boolean values in an example here:
v1 (entity[attributeName] != null),
v2 (entity.FormattedValues.Contains(attributeName))
v3 (entity[attributeName].GetType() != typeof(EntityReference))
Note that you should generally write your code in a way that promotes legibility over brevity.
If I understand you mean correctly, your code can be rewritten like this:
if(entity[attributeName] != null)
{
return entity.FormattedValues.Contains(attributeName) && entity[attributeName].GetType() != typeof(EntityReference)
? entity.FormattedValues[attributeName]
: GetDisplayObjectFromRawValue(entity[attributeName]);
}
return entity.FormattedValues.Contains(attributeName)
? entity.FormattedValues[attributeName]
: GetDisplayObjectFromRawValue(entity[attributeName]);
P/S: This way has nothing to do with: it made the code run faster or it saves memory while running or something like that. Just for readability.
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);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i'm developing a C# app.
I have a text input field with a submit button.
When I click on the button I want to check if the text is excactly: "CODxxxxx" (x=number).
Actually I have this:
if (inputText1.ToLower().Contains("COD") && inputText1.Length.ToString() == "8")
{
//DO THINGS
}
but if the string is xxxCODxx the statement returns true.
I think regular expressions can help me, but I never used with C#...
Try regular expression:
bool correct = Regex.IsMatch(inputText1, "^COD[0-9]{5}$");
If you want to ignore case (i.e. Cod, cOD etc. are supposed to be correct), just add option:
bool correct = Regex.IsMatch(inputText1, "^COD[0-9]{5}$", RegexOptions.IgnoreCase);
You can also solve it without RegEx
string input = "COD12345";
bool result = input.StartsWith("COD") && input.Skip(3).All(x => char.IsDigit(x)) && input.Length == 8;
Try:
inputText1.ToLower().StartsWith("cod") && inputText1.Length == 8
Or if you realy want it with regex,
this will be the right pattern:
Regex.IsMatch( inputText1, "COD\w{5}");
You can either use regular expressions:
Regex.IsMatch( text, "^COD\\d{5}$");
Or your own Solution:
string txt = inputText1.ToLower();
if (txt.StartsWith("cod") && txt.Length == "8")
{
int a = 0;
int.TryParse(txt.Substring(3), out a);
if(a!=0){
//DO THINGS
}
}
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 8 years ago.
Improve this question
I'm having some trouble turning the following code into a for loop. The purpose of the code is to check if the string has both a letter and a number in it.
else if (!string.Any(Char.IsLetter) || !string.Any(Char.IsDigit))
{
return false;
}
Any ideas?
Do you mean something like this?
bool anyLetter = false;
bool anyDigit = false;
foreach(var ch in str)
{
if(char.IsLetter(ch)) anyLetter = true;
if(char.IsDigit(ch)) anyDigit = true;
if(anyLetter && anyDigit) break;
}
return anyLetter || anyDigit;
Note that if this string should contain at least one digit and one letter, you need to use && instead of ||
Since Selman22 seems to have already answered the question, another solution I found is I guess you could also use RegEx:
letterCount = Regex.Matches(yourstring,#"[a-zA-Z]").Count;
numberCount = Regex.Matches(yourstring,#"\d").Count;
return letterCount != 0 && numberCount != 0;
Did you mean a loop for a set of strings?
var src = new List<string>{"A4C", "D3F", "G7I"};
var allHaveCharAndDigit = src.TrueForAll(s => s.Any(c => char.IsLetter(c)) && s.Any(c => char.IsDigit(c)));