为什么不匹配有时会在继续之前发生两次?

时间:2016-03-23 22:10:26

标签: c# linq combobox

我有这段代码:

int RVHHSelectionIndex = 0;
int RVSelectionIndex = 0;
while (RVMismatch())
{
    studentFullname = checkListBoxRV.Text;
    assistantFullname = checkListBoxRVHH.Text;
    listBoxMessages.Items.Add(string.Format(mismatchDiscovered, studentFullname, assistantFullname));
    if (RVHHSelectionIndex < RVHHCount - 2)
    {
        checkListBoxRVHH.SelectedIndex = RVHHSelectionIndex++;
        checkListBoxRVHH.SetItemChecked(checkListBoxRVHH.SelectedIndex, true);
    }
    else
    {
        // If all the HHs have been gone through, start again at the beginning:
        checkListBoxRVHH.SelectedIndex = 0;
        checkListBoxRVHH.SetItemChecked(checkListBoxRVHH.SelectedIndex, true);
        // ...and move to the next Student:
        if (RVSelectionIndex < checkListBoxRV.Items.Count - 2)
        {
            checkListBoxRV.SelectedIndex = RVSelectionIndex++;
            checkListBoxRV.SetItemChecked(checkListBoxRV.SelectedIndex, true);
        }
    }
}

private bool RVMismatch()
{
    if (!System.IO.File.Exists(AYttFMConstsAndUtils.STUDENTS_FILENAME)) return true;
    if (null == AYttFMConstsAndUtils.STUDENTS_FILENAME) return true;
    int RVStudentID = Convert.ToInt32(checkListBoxRV.SelectedValue);
    int RVHHCandidateID = Convert.ToInt32(checkListBoxRVHH.SelectedValue);
    Student RVStudent = AYttFMConstsAndUtils.StudentsList.SingleOrDefault(i => i.StudentID.Equals(RVStudentID));
    Student RVHHCandidate = AYttFMConstsAndUtils.StudentsList.SingleOrDefault(i => i.StudentID.Equals(RVHHCandidateID));
    // If either are null, return true
    if ((null == RVStudent) || (null == RVHHCandidate)) return true;
    // If Student male, candidate assistant female, and not family, return true
    if (RVStudent.IsMale)
    {
        if (!RVHHCandidate.IsMale && RVStudent.FamilyID != RVHHCandidate.FamilyID) return true;
    }
    // If Student female, candidate assistant male, and not family, return true
    if (!RVStudent.IsMale)
    {
        if (RVHHCandidate.IsMale && RVStudent.FamilyID != RVHHCandidate.FamilyID) return true;
    }
    // Otherwise, it's good:
    return false;
}

...当被指派为主要学生的人与该人不匹配时,应该选择另一个家庭成员(RVHH)(有一些规则使他们不匹配)。

有两个组合框,一个是主要学生(RV),另一个是助手(RVHH)。如果第一个HH是不匹配的,它将移动到组合框中的下一个候选HH。如果所有RVHH候选者都不匹配,则RV组合框将递增到下一个,并且该过程将重新开始,直到匹配为止。

它在很大程度上起作用,但奇怪的是我的“日志”条目有时会显示两次相同的不匹配。如果那个总是这个案子,我会发现我的代码中有一个明显且令人震惊的缺陷。但它并不总是发生。例如,请注意此处的“不匹配”消息:

enter image description here

对于底部组合框,第一个HH候选人被视为两次不匹配两次,然后继续下一次,也是一次不匹配但只记录一次,而下一次是同样的情况;它最终选择了下一个不匹配的候选人,因此记录停止了。

为什么第一个经过两次才能继续前进?

1 个答案:

答案 0 :(得分:2)

由于我无法运行您的代码,因此我无法确定;但我猜你的代码只在表单第一次显示时运行良好;这是因为当首次显示表单时,检查列表框的选择索引始终是-1(没有选择集);所以这个集团没有问题:

studentFullname = checkListBoxRV.Text;
assistantFullname = checkListBoxRVHH.Text;
listBoxMessages.Items.Add(string.Format(mismatchDiscovered, studentFullname, assistantFullname));

但是当代码再次运行时,由于这一行,选择索引似乎已更改为0

checkListBoxRVHH.SelectedIndex = 0;

所以listBoxMessages应该读取第一个项目两次,一次是

if (RVHHSelectionIndex < RVHHCount - 2)
{
//...

再次使用正常的代码执行序列。