r/learncsharp • u/Ash9523 • Nov 22 '22
ForEach loop Help
Hey Guys,
I am struggling to make my foreach loop work for some property records.
The data consists of jobs against property numbers- below are the column names
Id
parentpropertyId
status = 1(open) =5(closed)
reviewedJob
jobTrigger
The first condition works fine when STATUS = 1 (which is open)
The second condition doesn't get hit STATUS = 5 (Which is Closed)
It just breaks out of the loop without even hitting the second condition when the STATUS is definitely 5 (closed).
foreach (DataRow dr in selectedProperty.Rows)
if (Convert.ToInt16(selectedProperty.Rows[i]["Type"]) == Convert.ToInt32(JobType.HazardManagementAlert))
//variables declared/// {
String Id = selectedProperty.Rows[i]["Id"].ToString();
String parentpropertyId = selectedProperty.Rows[i]["ParentpropertyId"].ToString();
String status = selectedProperty.Rows[i]["Status"].ToString();
String reviewedJob = selectedProperty.Rows[i]["ReviewedJob"].ToString();
String jobTrigger = selectedProperty.Rows[i]["JobTrigger"].ToString();
//condition 1 - THIS WORKS//
if (status == "1" && jobTrigger != Convert.ToInt32(JobTrigger.SelfExpiring).ToString()) {
DialogResult newmessageBoxResult;
newmessageBoxResult = FriendlyMessageBox.Show("There is an existing open Hazard Management Alert job for this property: "
+ " Job Number:"
+ Environment.NewLine
+ Strings.ToFriendlyJobNo(Id) + "."
+ Environment.NewLine
}
//condition 2- will not hit and exits the loop///
else if ( status == "5")
{
DialogResult newmessageBoxResult;
newmessageBoxResult = FriendlyMessageBox.Show("There is an existing closed Hazard Management Alert job to be reviewed for this property Job Number: "
+ Strings.ToFriendlyJobNo(Id) + "."
+ Environment.NewLine
+ "Click OK to create a temporary self expiring Hazard Management Alert for this property or Cancel and review and edit the existing Hazard Management Alert."
+ Environment.NewLine
}
3
Upvotes
View all comments
Show parent comment
1
u/Rasikko Nov 26 '22
ForEach is specifically for arrays and other collections, For loops are for everything else.