Resolved help with the Enum I've created

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
I've never used Enum befores so forgive me if what i've done is wrong/bad code. Ive created an Enum in my patient class. I've created a helper method which will be activated whenever the user creates a new patient in the system. I'm having two issues.


C#:
public enum Gender
{
    Male,
    Female   
}

public static Gender ConvertToGender(string stringInput)
{
    var gender = stringInput.ToLower();
    switch (gende)
    {
        case "male":
            return Gender.Male;
        case "female":
            return Gender.Female;
      
    }
}

1. whenever I call the helper method, I want it to loop through until the user enters either "male" or "female". I then want that input to be saved within the gender variable within my Add Patient procedure.
2. when I'm creating an instance of a Patient Object how would I present the enum? at the moment I'm inserting the gender string variable, but its causing an error. "Cannot convert from string to Project.Patient.GenderChoice"

can anyone help? im guessing its something pretty simple but I've tried various methods and still have had no luck. Thankyou!
 
Last edited:
The Enum class provides methods for converting numbers and strings to valid enum values. In your case, I'd get rid of that helper method and do the following. Firstly, declare the enum outside of any other type with an appropriate name and casing for the fields:
C#:
public enum Gender
{
    Male,
    Female
}
The word "Choice" has no business there. The value describes the gender of a patient and they didn't choose that. The fact that you're choosing a value in the application is irrelevant.

As for how to receive the input:
C#:
Gender gender;
bool inputIsValid;

do
{
    Console.WriteLine("Patient gender (Male/Female): ");

    var input = Console.ReadLine();

    inputIsValid = Enum.TryParse(input, true, out gender);
} while (!inputIsValid);
That will just keep on prompting until the user enters a valid value, and case is not an issue.
 
The Enum class provides methods for converting numbers and strings to valid enum values. In your case, I'd get rid of that helper method and do the following. Firstly, declare the enum outside of any other type with an appropriate name and casing for the fields:
C#:
public enum Gender
{
    Male,
    Female
}
The word "Choice" has no business there. The value describes the gender of a patient and they didn't choose that. The fact that you're choosing a value in the application is irrelevant.

As for how to receive the input:
C#:
Gender gender;
bool inputIsValid;

do
{
    Console.WriteLine("Patient gender (Male/Female): ");

    var input = Console.ReadLine();

    inputIsValid = Enum.TryParse(input, true, out gender);
} while (!inputIsValid);
That will just keep on prompting until the user enters a valid value, and case is not an issue.
Thankyou!!!
 
Hi, theres one small problem I've encountered with the code, it allows me to enter numbers. if I type any number it takes me to the next console.readline
 
You can change this:
C#:
inputIsValid = Enum.TryParse(input, true, out gender);
to this:
C#:
inputIsValid = Enum.TryParse(input, true, out gender) &&
               Enum.IsDefined(typeof(Gender), gender);
and it will only accept 0 or 1, which are the numeric values for Gender.Male and Gender.Female respectively. If you want to exclude those numeric values too then there are a number of ways to do that, including this:
C#:
inputIsValid = Enum.TryParse(input, true, out gender) &&
               input.Equals(Enum.GetName(typeof(Gender), gender), StringComparison.CurrentCultureIgnoreCase);
That last part will get the label associated with the parsed value, if there is one, and perform a case-insensitive comparison between that and the original input. If they don't match then the input is rejected. This means that, even though 0 and 1 will be successfully parsed to Male and Female respectively, converting those labels to Strings and comparing them to the numeric input will not produce a match.
 
Back
Top Bottom