I searched SO and found some posts, but could not get them to work.
Question: How would I loop to the next item in my List Collection (custLoginHist[1] etc)?
List<eCommCustomer.oCustomer> custLoginHist = new List<eComm.oCustomer>();
eCommCustomerDAL.GetCustomerPrevLogin(custLoginHist, oCust);
if (custLoginHist.Count > 0)
{
eCommSecurityFactory oSecFactory = new eCommSecurityFactory();
if (oCust.CustHash == oSecFactory.CreateHash(custLoginHist[0].CustSalt, custLoginHist[0].CustHash))
{
//Password has been used before;
return false;
}
else
{
// Valid password;
return true;
}
}
return true;
}
foreach(eCommCustomer.oCustomer cust in custLoginHist)
{
//Do something with cust here.
}
OR:
for(int i = 0; i != custLoginHist.Count; ++i)
{
eCommCustomer.oCustomer cust = custLoginHist[i];
//Do something with cust here.
}
In this case, we want to return false for any single match, and true otherwise, so:
foreach(eCommCustomer.oCustomer cust in custLoginHist)
if(oCust.CustHash == oSecFactory.CreateHash(custLoginHist[0].CustSalt, custLoginHist[0].CustHash)
return false;
return true;//if we reached here, no matches.
This is a bad idea though, because you've made breaking into the system easier. If I try to set my password to something, and you refuse, I now know that one of your users uses that password. You are much better off letting this case happen, though you should perhaps be blocking some of the more likely offenders ("password", "password1", etc) with a quality check.
List<eCommCustomer.oCustomer> custLoginHist = new List<eComm.oCustomer>();
eCommCustomerDAL.GetCustomerPrevLogin(custLoginHist, oCust);
foreach (var custLogin in custLoginHist)
{
eCommSecurityFactory oSecFactory = new eCommSecurityFactory();
if (oCust.CustHash == oSecFactory.CreateHash(custLogin.CustSalt, custLogin.CustHash))
{
//Password has been used before;
return false;
}
}
return true;
Try something like this, maybe you have to customize your return statements but it should give you an insight how it works.
foreach(var item in yourList)
{
//Iterate;
}
If you want break , you can use : break;
If you want finish you can use : continue;
List<T> implements IEnumerable<T>, so you can just use foreach or if you to be able to edit T in the loop, you can use for.
foreach(var item in custLoginHist)
{
}
Or
for (int i = 0; i < custLoginHist.Count; i++)
{
}
Then if you need to exit out of the loop before it is completed (such as if you have a condition that is true, you can just use break; to exit the loop, or you can return from a loop too if you want to return a value.
You can you loop for this. For example foreach or for:
foreach (var custLogin in custLoginHist)
{
eCommSecurityFactory oSecFactory = new eCommSecurityFactory();
if (oCust.CustHash == oSecFactory.CreateHash(custLogin.CustSalt, custLogin.CustHash))
{
//Password has been used before;
return false;
}
else
{
// Valid password;
return true;
}
}
List<eCommCustomer.oCustomer> custLoginHist = new List<eComm.oCustomer>();
eCommCustomerDAL.GetCustomerPrevLogin(custLoginHist, oCust);
return custLoginHist.Select(c=>oSecFactory.CreateHash(c.CustSalt,c.CustHash))
.Any(x=>x==oCust.CustHash)
Related
Is there a way to check if all the buttons in an array are active or not, all at once? I am using for loop to determine it but the break; does not really signify anything that I want to achieve.
public Button[] bts;
public void StateButton()
{
for (int i = 0; i < bts.Length; ++i)
{
if (!bts[i].interactable)
{
Debug.Log("all buttons are off");
break;
}
else
{
Debug.Log("atleast one is on");
}
}
}
It is possible using LINQ:
var allInteractable = bts.All(e => e.interactable)
Depending on the length of the array and the expected state of the buttons (i.e. you know that almost all the time at least one button is not interactable) it might be more performant to inverse the query using:
var atLeastOneNonInteractable = bts.Any(e => !e.interactable)
Your debug logs don't really match with the title of your question.
Written out it should either be
public bool StateButton()
{
var allOn = true;
foreach (var btn in bts)
{
if (!btn.interactable)
{
allOn = false;
break;
}
}
Debug.Log(allOn ? "All buttons on" : "At least one button off");
return allOn;
}
or
public bool StateButton()
{
var anyOn = false;
foreach (var btn in bts)
{
if (btn.interactable)
{
anyOn = true;
break;
}
}
Debug.Log(anyOn ? "At least one button on" : "All buttons off");
return anyOn;
}
depending on your needs.
And then you already have Michael Mairegger's answer using Linq to shorten this into a single line ;)
I need to loop through a list. Each element should be compared to 2 conditions.
The problem is that for one element result can be True, for another False. I have to keep all these results and make the operation OR, in order to get the final result.
foreach (var elem in mappingList)
{
if (elem._mappingStatus == enum_MappingStatus.E_MAPPING_OK
|| elem._mappingStatus == enum_MappingStatus.E_MAPPING_OK_END)
{
statusConfiguration = true;
}
else
{
statusConfiguration = false;
}
}
The problem that I don't know how to keep statusConfiguration for all elements and then compare them. If at leat one of the results is false then the final result should be also false.
Thanks in advance.
Use Any() to determine if not any item with these conditions exists
If at leat one of the results is false then the final result should
be also false
statusConfiguration = mappingList.Any(x =>
x._mappingStatus != enum_MappingStatus.E_MAPPING_OK &&
x._mappingStatus != enum_MappingStatus.E_MAPPING_OK_END);
You should break off the foreach after statusConfiguration has been 'falsed'.
bool statusConfiguration = true;
foreach (var elem in mappingList)
{
if (elem._mappingStatus == enum_MappingStatus.E_MAPPING_OK
& elem._mappingStatus == enum_MappingStatus.E_MAPPING_OK_END)
{
statusConfiguration = true;
}
else
{
statusConfiguration = false;
break;
}
}
if (!statusConfiguration) {finalResult = false;}
I have problem with List string. I put 3 values into myCollection
List<string> myCollection = new List<string>();
myCollection.Add(Encoding.Default.GetString(data));
myCollection.Add(Encoding.Default.GetString(data2));
myCollection.Add(Encoding.Default.GetString(data3));
and now i have 3 values : A,B,C
but now i want to block buttons with contains this values :
for (var i = 0; i < myCollection.Count; i++)
{
if (myCollection.Contains(A))
{
this.A.Enabled = false;
}
else if (myCollection.Contains(B))
{
this.B.Enabled = false;
}
else if (myCollection.Contains(C))
{
this.C.Enabled = false;
}
}
After this loop just first button=false. Now loop done 3 times this same try block button A and my question is:
How block other buttons?
Now i get in first loop run:
this.A.Enabled = false;
2nd this.A.Enabled = false;
3rd this.A.Enabled = false;
but i want :
1st : this.A.Enabled = false;
2nd : this.B.Enabled = false;
3rd : this.C.Enabled = false;
You don't need a loop for this. Just use simple if statements without else.
if (myCollection.Contains("A"))
this.A.Enabled = false;
if (myCollection.Contains("B"))
this.B.Enabled = false;
if (myCollection.Contains("C"))
this.C.Enabled = false;
Mainly the else was causing problems for you. If the condition for A was true, then the code for B and C did not run. This is how else works.
Not sure what exactly are you trying to achieve, but your problem here is that you use if..else. If any of the conditions is true, the rest won't be resolved.
To solve your problem just remove the else keywords from your conditions.
Also, the loop is unnecesary when you use Contains.
If you'd insist on the loop, you would have to change the condition a bit, and then the else would be properly used:
for (int i = 0; i < myCollection.Count; i++)
{
if (myCollection[i] == A)
{
this.A.Enabled = false;
}
else if (myCollection[i] == B)
{
this.B.Enabled = false;
}
else if (myCollection[i] == C)
{
this.C.Enabled = false;
}
}
This is fairly simple method. I use entity framework to get some data and then check some values in a if statement. However right now the method is marked with red.
This is my method:
private bool IsSoleInProduction(long? shoeLastID)
{
if (shoeLastID == null)
{
MessageBox.Show(Resources.ERROR_SAVE,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
ISoleService soleService =
UnityDependencyResolver.Instance.GetService<ISoleService>();
List<Sole> entity =
soleService.All().Where(s => s.ShoeLastID == shoeLastID).ToList();
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
What am I missing?
You need to take advantage of LINQ with Any, replace your code:
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
with simpler code:
return entity.Any(item => item.Status == 20);
Or even better performance:
return soleService.All()
.Any(s => s.ShoeLastID == shoeLastID
&& s.Status == 20);
Edit: With you comment, below code is what you need:
List<Sole> entity = soleService.All()
.FirstOrDefault(s => s.ShoeLastID == shoeLastID);
return entity == null ? false : entity.Status == 20;
If there's no item in your entity collection, then neither of the containing if/else branches will be executed. In this case there's no return statement anymore, because the else part won't be executed, and outside your foreach you have no return statement.
The compiler does not "see" that if
entity.Count() != 0
then your loop
foreach (var items in entity)
will run at least once. Therefore it sees a possibility of running the forech zero times, and not running the else block.
Suppose first time the entity is enumerated, it yields some (finite number of) items. Then the Count will be non-zero. Then suppose next time the same entity is enumerated then it yields no items! That would cause your code to "fall through" without returning.
It is very probably that you can guarantee that the source yields the same number of items each time it is re-enumerated. But the compiler cannot.
Solution: Just skip if (entity.Count() != 0) and do foreach right away.
You haven't retrun anything from this code block
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
// return someting
}
You might consider doing the following. This will adhere to the "single exit-point" principle (which can sometimes help improve code clarity), and ensure you have a default value in any case:
private bool IsSoleInProduction(long? shoeLastID)
{
// The main change: A default value, assuming "no":
var isSoleInProduction = false;
if (shoeLastID == null)
{
MessageBox.Show(Resources.ERROR_SAVE,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
isSoleInProduction = false;
}
ISoleService soleService =
UnityDependencyResolver.Instance.GetService<ISoleService>();
List<Sole> entity =
soleService.All().Where(s => s.ShoeLastID == shoeLastID).ToList();
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
isSoleInProduction = true;
}
else
{
isSoleInProduction = false;
}
}
}
else
{
isSoleInProduction = false;
}
return isSoleInProduction;
}
What will be your entity.Count() is not 0 and your entity doesn't have any items?
That means your if block will work but foreach part will not work. Since your if part doesn't have any return statement, that's why you get an error.
You should put return statement in your if part.
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
//return true or false
}
If your entity collection has no elements you will not reach a return statement - you need to add a return false forexample as last statement
As the error states there can be cases in which none of your return clause is evaluated (e.g. if there are no elements in your list).
To quickly solve it you can put a default return statement, for example by moving the last return clause outside the else statement. But it really depends on the behavior you'd expect.
private bool IsSoleInProduction(long? shoeLastID)
{
if (shoeLastID == null)
{
MessageBox.Show(Resources.ERROR_SAVE, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
ISoleService soleService = UnityDependencyResolver.Instance.GetService<ISoleService>();
List<Sole> entity = soleService.All().Where(s => s.ShoeLastID == shoeLastID).ToList();
if (entity.Count() != 0)
{
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
}
return false;
}
The compiler can't guarantee that the first call to Count means that the foreach will loop at least once (with good reason, because you could, if you wished, create a collection where this wasn't true). You can do this instead (with no need for the outer if):
foreach (var items in entity)
{
if (items.Status == 20)
{
return true;
}
else
{
return false;
}
}
return false;
I have a string array.
I need to display buttons based on if the selected item is in the array.
I need to know how to tell the program if "(array.NOT Contains("string"))".
Please can anybody help me??
Thanks in advance
My code:
List<string> activationids = new List<string>();
foreach (ModuleActivation moduleactivation in activationid)
activationids.Add(moduleactivation.ActivationID);
string gvselectActID = GridView1.SelectedRow.Cells[1].Text;
if (activationids.Contains(gvselectActID))
{
activateInsert.Visible = true;
activateUpdate.Visible = false;
deactivate.Visible = true;
}
else if (activationids."NOT" Contains(gvselectActID))
{
activateInsert.Visible = false;
activateUpdate.Visible = true;
deactivate.Visible = false;
}
else
{
activateInsert.Visible = false;
activateUpdate.Visible = false;
deactivate.Visible = false;
}
}
Change:
else if (activationids."NOT" Contains(gvselectActID))
to
else if (!activationids.Contains(gvselectActID))
Or even simpler
bool containsItem=activationids.Contains(gvselectActID);
activateInsert.Visible = containsItem;
activateUpdate.Visible = !containsItem;
deactivate.Visible = containsItem;
The ! means "NOT". So you have to place it in front of the expression you need to negate;
!activationids.Contains("blahblah");
However, it's quite clear that if activationids.Contains("blahblah") is false, you are gonna go into the second case. Also, currently, your third block (... else { ...) will never be hit.
There are two very straightforward ways to do this:
Not the result of the bool function call:
if(!activationids.Contains(gvselectActID))
Check the result and compare it to false
if(activationids.Contains(gvselectActID) == false)
However, you are checking if it contains it in the first if() clause, which means that the first else clause will be fired if it isn't contained. There is no need to check, and there is no way that the third else will ever be fired.
Contains returns true or false, sou you cannot have three branches, you can do just
if (activationids.Contains(gvselectActID)) // it does contain
...
else // it does not contain
...
there are no other possibilities
[joke]
well it could work in this case
http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx
[/joke]
This will be enough:
if (activationids.Contains(gvselectActID))
{
// Goes here if condition is true
activateInsert.Visible = true;
activateUpdate.Visible = false;
deactivate.Visible = true;
}
else
{
// Goes here if condition is false
activateInsert.Visible = false;
activateUpdate.Visible = true;
deactivate.Visible = false;
}
There are no other possible options - there can't be a third branch.
This makes no sense:
if(booleanCondition)
{}
else if (!booleanCondition)
{}
else
{}
As by definition, if the booleanCondition is false, the else branch will be taken - there is no need to test for it being false.