How can I handle the situation opposite to my condition? [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a foreach loop where the code doesn't need to run in one special case. I've done it like this:
if (!IsZoom && entry.StartDate.Year != Year && entry.EndDate.Year != Year)
{
}
else
{
// my code...
}
Somehow I am not happy with that. Is it maybe better to write this as follows?
bool foo = !IsZoom && entry.StartDate.Year != Year && entry.EndDate.Year != Year;
if (!foo)
{
// my code...
}

Reverse your logic. Invert all your boolean expressions, and replace AND with OR:
if (IsZoom || entry.StartDate.Year == Year || entry.EndDate.Year == Year) {
// Do something...
}

Related

Does this public get set for a private variable make sense? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am working on a codebase a student with questionable knowledge made, here I am just confused if there is any reasonable intend there or if its just unnecessary... I personally would just use the ShowNewButton field and trash the _showNewBtn, please provide an opinion.
private _showNewBtn;
public bool ShowNewButton
{
get => _showNewBtn;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
_showNewBtn = value;
}
}
I see no reason (in the sample you provided) for the _showNewBtn.
You now have 2 markers which contain the visibility state of the button. At some point, this will cause problems if you aren't carefull.
Either remove the _showNewBtn completely:
public bool ShowNewButton
{
get => bbNew.Visibility == BarItemVisibility.Always;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
}
}
Or justify the existance of _showNewBtn which makes my remark null and void.

C# nicer way of writing if statement with two values [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I was wondering if anyone could please tell me a nicer way of writing this. These are the only conditions that are used.
private static void BondedNonBoundedIndicator(InvoiceLine invoiceLine, Product packageProduct)
{
var value = packageProduct.BondedQuantity;
var value2 = packageProduct.NonBondedQuantity;
if (value == 0 && value2 == 1)
{
invoiceLine.BondedORBbondedIndicator = "N";
}
if (value == 1 && value2 == 0)
{
invoiceLine.BondedORBbondedIndicator = "Y";
}
}
Using C# 8, we can use tuples and the switch expression to simplify this:
invoiceLine.BondedORBbondedIndicator = (value, value2) switch {
(0, 1) => "N",
(1, 0) => "Y",
_ => invoiceLine.BondedORBbondedIndicator
};
You could shorten the last case even further if you know the invoiceLine.BondedORBbondedIndicator before the code is run is always, say ""(, then the last case would be _ => "").
You can use xor logic
If(value1^value2 == 1)
value1 == 0 ? invoiceLine.BondedORBbondedIndicator = "N" : invoiceLine.BondedORBbondedIndicator = "Y";

C# Convert string to name of bool variable [closed]

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 7 years ago.
Improve this question
I trying to make something like that:
// int counter; - this is changing in ther other parts of program
bool abc1; bool abc2; bool abc3; bool abc4;
if("abc" + counter == true)
{
//some code
}
Anyway, I need to convert string and int to bool name. How can I do this?
Use an array instead:
bool[] abc;
// ...
if (abc[counter] == true) {
{
// some code.
}

How do I turn .Any into a for loop? [closed]

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)));

Cannot apply indexing with [] to an expression of type? [closed]

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 have got this error
Error 3 Cannot apply indexing with [] to an expression of type
my problem here
if (client["banhours"] == 0)
{
client["banhours"] = -1;
client["banreason"] = "Infinite time.";
client["banstamp"] = DateTime.Now.AddYears(100);
}
if (Account.State == Database.AccountTable.AccountState.Banned)
{
if (client["banhours"] != -1)
{
DateTime banStamp = client["banstamp"];
if (DateTime.Now > banStamp.AddDays(((int)client["banhours"]) / 24).AddHours(((int)client["banhours"]) % 24))
Account.State = Database.AccountTable.AccountState.Player;
}
}
client is >>>
Client.GameClient client;
Have you tried client.banhours or client.banreason?
If Client.GameClient is a class and those are properties or fields, they must not be accessed like an array or dictionary.

Categories

Resources