Pk, so here's the code I'm running for a checkboxlist object

public void regSubjects(object sender, EventArgs e)
{
    int courseCount = 0;
    for (int i = 0; i < coursesFound.Items.Count; i++)
    {
        if (coursesFound.Item[i].Selected)
            coursesCount++;
    }

    if ((courses < 4) && (courses > 6))
        clabel.Text = string.Format("Choose between 4 - 6 subjects");
}

Wierd thing is, if have only one condition in my final if, it works fine, if i have both, it doesn't pick it up, even if I nest the two conditions in their own if, it doesn't pick up on the error, just ignores it. Viusal Studio 2010, on Windows 7. The same code will run fine on a friends laptop.

Have you tried running just the compiled code without the VS2010 running? I've heard some people have trouble running VS2010 on Win7/8. I think some have had better luck using the XP Compatiblity Mode.

if ((courses < 4) && (courses > 6)) is wrong. You want to verify if the courses variable is between 4 and 6, inclusive, so it should be if ((courses < 4) || (courses > 6)). IE, use the OR, not the AND boolean operator.

Comments
Good observation

In addition to rubberman's comment:

you are summing in "coursesCount:, but comparing "courses".

Is this the logic you intended?

@rubberman ok, that solved it, thanks a lot. How come then, that this same code works as intended on another friend's machine, running the same software?
@TnTinMN Sorry about that meant it to be courseCount; was typing fast and didn't check that the variables are the same, that's not a copy/paste so the discrepancy.

this same code works as intended on another friend's machine, running the same software

Strange. Did you ever encounter a courses variable that is both smaller than 4 AND greater than 6?
Smaller than 4 OR greater than 6 is possible.

Edited 3 Years Ago by ddanbe: typo

@ ddanbe

Strange. Did you ever encounter a courses variable that is both smaller than 4 AND greater than 6?
Smaller than 4 OR greater than 6 is possible.

Ah, now I see it(what was I thinking). Apologies, community; my logic is usually not this messed up.

My most useful class in college for my software engineering career was my required philosophy class, for which I took Formal Logic! Go figure! :-)

This question has already been answered. Start a new discussion instead.