Why logical evaluation is not stopped on fist "false" result? [closed] - c#

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
testExceptions is Dictionary.
var testConfigurations = cells.SelectMany(x => x.TestConfigurations).Where(
y => y.ID == testId &&
!testExceptions[index].ContainsKey(y.Cell) &&
!testExceptions[index][y.Cell].Contains(y.ID)
).ToList();
!testExceptions[index][y.Cell].Contains(y.ID) is giving me an exception when testExceptions doesn't contain key y.Cell. But since I was checking ContainsKey I expected it will not evaluate last condition. Is there any way to avoid exception without using long if/else statements?

You're checking if it doesn't contain the key. You need to check if it does contain the key:
testExceptions[index].ContainsKey(y.Cell) && ... use the key ...

Related

"else cannot start a statement" issue [closed]

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
I tried making a program where every few seconds a random letter dropped. I noticed that the "correct" one dropped to rarely. I made a int correctLetterDrop = Random.Range(0,100) and wanted to artificially increase the chance. The rest of the code handles everything, i just need to assign the correct sprite to check against.
This gives me an error at the .correctSprite; saying "else cannot start a statement". I expect it to just check if the Random.Range returned 7 or less (for a 7% chance) and if it did to set the correct one, and otherwise just set a random one.
if(correctLetterDrop <= 7){
Clone.GetComponent<SpriteRenderer>().sprite = correctLetterScript.correctSprite;
else{
Clone.GetComponent<SpriteRenderer>().sprite = Letters[Letter];
}
}
Check for the first if condition closure }:
if(correctLetterDrop <= 7) {
Clone.GetComponent<SpriteRenderer>().sprite = correctLetterScript.correctSprite;
}else{
Clone.GetComponent<SpriteRenderer>().sprite = Letters[Letter];
}

C# Length method [closed]

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 2 years ago.
Improve this question
I'm looking how to use correctly the Lenght method, I'm not c# developer, first time looking into code so basically try to get throw using documentation, so I build an validation code that uses string.IsNullOrEmpty to check if the fieldName.name is either empty or null, this is working
var namepropName = PathFinder<InfoField>.Get(i => i.fieldName.name);
RegValidation(namepropName, (x, y) => {
ValueRequired(, () => string.IsNullOrEmpty(x.fieldName.name));
});
Now I trying to check if the fieldName.name length is bigger than 20 characters.
The below code is throwing errors, how can I use string.Length is bigger than 20 correctly?
ValueRequired(, () => string.Length > 20 (x.fieldName.name));
ValueRequired(, () => x.fieldName.name.Length > 20);

Operator '==' cannot be applied to operands of type 'method group' and 'string' styleid in C# [closed]

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 3 years ago.
Improve this question
I wanted to compare the int[] StyleIds to string S_StyleId.
but I dont know how to do it..
var cs = optionalEquipments.FirstOrDefault(
d => d.StyleIds.Any(s=>s.ToString == c.S_StyleId);
I wanted to get and compare the styleId
ToString is a method,
You need to change
Any(s=>s.ToString == c.S_StyleId);
into
Any(s=>s.ToString() == c.S_StyleId);

blank is a 'variable' but is used like a 'method' - why am I getting this? [closed]

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
I have a list of cleaners (linq class) I am trying to remove 'cleaners if they are unconfirmed. Here is my code.
List<ConfirmCleaner> Unconfirmed = context.ConfirmCleaners.Where(d => d.confirmed == false).ToList();
List<Cleaner> Cleaners = context.Cleaners.ToList();
List<Cleaner> TempCleaners = new List<Cleaner>();
foreach (var c in Unconfirmed)
{
foreach(var r in Cleaners){
if(c.cleanerId == r.id){
TempCleaners.Add(r);
}
}
}
List<Cleaner> result = Cleaners().Except(TempCleaners()).ToList();
return result;
Why am I getting an error :
'Cleaners' is a 'variable' but is used like a 'method'
The problem is this line:
List<Cleaner> result = Cleaners().Except(TempCleaners()).ToList();
You need to change it to:
List<Cleaner> result = Cleaners.Except(TempCleaners).ToList();
Hope that the error message is clear enough to identify the issue, anyway you have declared Cleaners as a List of Cleaner, and When you were using that variable in the final query you are using the same like Cleaners(). When you use like this, the compiler expects a method, and that is what the compiler specified in the error message as well. So the code should be :
return Cleaners.Except(TempCleaners()).ToList();

How can I count number of items in a list based on multiple criteria [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have a list List<TestObj> testList
class TestObj
{
public int type;
public string phase;
}
1 Phase1
1 Phase1
2 Phase1
2 Phase2
I want to get count of specific type and phase. So I tried this
var count = testList.Count(x=> x.type == 1 && x.phase.Equals("Phase1"));
I got zero.
Use == for comparisons in C#. The = operator is for assignments only.
var count = testList.Count(x=> x.type == 1 && x.phase == "Phase1");
== Operator (C# Reference)
I got zero.
Your code would not even comile.
Maybe you are using == instead and you have an item with phase="phase1". C# is case-sensitive, you can use String.Equals with StringComparison.CurrentCultureIgnoreCase :
var count = testList.Count(x=> x.type == 1 && "Phase1".Equals(x.phase, StringComparison.CurrentCultureIgnoreCase));

Categories