Comparisons

lional

Well-known member
Joined
Nov 29, 2018
Messages
55
Programming Experience
Beginner
Hi
I am trying to do a comparison that is confusing me
I start by extracting the portion of the ID number that pertains to gender. 0001 - 4999 is female, and 5000 - 9999 is male.
I then see what the gender is that is being selected and I compare it
In my tests I use the gender portion to be 5001 and I choose female as per the code below. This should run the code in the if conditional but it deems it evaluates to true but it deems it to be false. Am I doing something incorrectly
C#:
string genderSegment = nationalIdstring.Substring(6, 4);
int genderSegmentInt = Convert.ToInt32(genderSegment);
string genderString = addStudentGenderCodeComboBox.SelectedValue.ToString();
string maleGender = "M";
 string femaleGender = "F";
if ((genderSegmentInt > 4999) && (genderString == femaleGender))
   {
        isValidated = false;
        validationErrorMessage.Append("The gender does not match portion in ID");
        validationErrorMessage.Append(Environment.NewLine);
  }
 
I'd be willing to bet that genderString is not "F". Have you set a breakpoint on the if statement and actually looked at each value being used on that line? If not, that's what you need to do and what you should have done before you posted here. If genderSegmentInt really is greater than 4999 and genderString really is equal to "F" then your system is broken.
 
Thanks. I have done message boxes just as quick test and it all seems to be OK, which is why I am confused but I will do breakpoints now
 
I have set the breakpoint and genderString is "F" and genderSegmentInt is 5001 but it skips teh entire block. I just wanted to know if my syntax was correct. I will troubleshoot some more
 
The syntax looks right and, based on those values, the block should be executed, so I can only assume that there's some information that you're not providing. I would suggest using the Watch or Immediate window to evaluate both parts of that Boolean expression and see which one is not giving the result expected I still suspect that that string contains an unexpected character or the like so you should probably examine it more closely.
 
Personally, I would do something like this:
C#:
enum Gender
{
    Female,
    Male
}

Gender GetGenderFromRange(int value)
{
    if (value < 1 || value > 9999)
        throw new ArgumentOutOfRangeException(nameof(value));
    return value < 5000 ? Gender.Female : Gender.Male;
}

Gender GetGenderFromChar(char ch)
{
    ch = char.ToUpper(ch);
    if (ch == 'F')
        return Gender.Female;
    else if (ch == 'M')
        return Gender.Male;
    throw new ArgumentOutOfRangeException(nameof(ch));
}

Gender GetGenderFromString(string s) => GetGenderFromChar(s[0]);

:

isValid = GetGenderFromRange(genderSegmentInt) == GetGenderFromString(genderString);
if (!isValid)
        validationErrorMessage.AppendLine("The gender does not match portion in ID");

This makes the intent of the code clearer. If the gender from the number is the same as the gender from then string, then the current form is valid. If they don't match, the form must be invalid. And you get the side benefit that exceptions are thrown if there is invalid data being passed in which would suggest that some other issue is at hand with regards to the inputs.
 
Back
Top Bottom