0

I have a list of flag enums for input validation, I want to return one of the enums as part of my validation. So far this is what I come up with...
These are my enums.

[Flags]
public enum CheckInput
{ 
    Not_Valid, 
    OK, 
    No_First_Name, 
    No_Last_Name, 
    No_Password, 
    Wrong_Password 
};

This is my switch to show a message for when an enum is used

public void ValidInputSwitch()
{
    CheckInput Status = CheckInput.Not_Valid;
    do
    {
        switch (Status)
        {
            case CheckInput.No_First_Name:
                MessageBox.Show("Please enter your first name.");
                break;
            case CheckInput.No_Last_Name:
                MessageBox.Show("Please enter your last name.");
                break;
            case CheckInput.No_Password:
                MessageBox.Show("Please enter your password.");
                break;
            case CheckInput.Wrong_Password:
                MessageBox.Show("Your passwords do not match!");
                break;
            case CheckInput.OK:
                CheckUserName(Uname);
                break;
            default:
                break;
        }
    }
    while (Status != CheckInput.OK);
}

Here is where my problem lies, I want to validate input and RETURN the enum so that it can run with my switch and show the message that is there

public CheckInput InputCheck
{
    {
        if (firstNameTxt.Text == null || firstNameTxt.Text == "") { return CheckInput.No_First_Name; }
    }
}

I actually abandoned this idea in the end and used "Error Reporting". I couldn't quite get this method to work for me. Definitely due to my lack of know how. One day I'll hope to have solved the problem I had.

  • 1
    `Status = InputCheck();`, just add it after the switch and before the while. – Gusman May 09 '16 at 12:50
  • not the same question but good answer with good example http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c – Sebastian Siemens May 09 '16 at 12:52
  • As a separate aside, consider using a mapping structure like a `Dictionary` to map enum values to method calls instead of a switch – D. Ben Knoble May 09 '16 at 12:55
  • There's a handy existing function `String.IsNullOrEmpty(str)` to replace that unwieldy `== null || == ""` thing. – Nyerguds May 09 '16 at 12:59

3 Answers3

0

The following change should solve the problem. But you have some design issues. You need to carefully think how to generate errors and respond.

public void ValidInputSwitch()
{
    CheckInput Status = InputCheck;
    do
    {

or this way:

public void ValidInputSwitch(CheckInput status)
{
    do
    {
        switch (status)
        {
Yahya
  • 3,386
  • 3
  • 22
  • 40
0

A [flags] enum is supposed to have powers-of-two values, so you can OR values together and test them with AND.

I suggest you change your enum as follows:

[Flags]
public enum CheckInput
{
    OK             = 0,
    No_First_Name  = 1,
    No_Last_Name   = 2,
    No_Password    = 4,
    Wrong_Password = 8
}

Then you could write a ValidateInput() something like this:

public static CheckInput ValidateInput(string firstName, string lastName, string password)
{
    CheckInput result = CheckInput.OK;

    if (string.IsNullOrWhiteSpace(firstName))
        result |= CheckInput.No_First_Name;

    if (string.IsNullOrWhiteSpace(lastName))
        result |= CheckInput.No_Last_Name;

    if (string.IsNullOrEmpty(password))
        result |= CheckInput.No_Password;
    else if (!PasswordIsValid(password))
        result |= CheckInput.Wrong_Password;

    return result;
}

And thus you could create an error message from a non-zero status like so:

public static string CreateErrorMessage(CheckInput status)
{
    StringBuilder result = new StringBuilder("Please correct the following issues:\n");

    if ((status & CheckInput.No_First_Name) != 0)
        result.AppendLine("Missing first name.");

    if ((status & CheckInput.No_Last_Name) != 0)
        result.AppendLine("Missing last name.");

    if ((status & CheckInput.No_Password) != 0)
        result.AppendLine("Missing password.");

    if ((status & CheckInput.Wrong_Password) != 0)
        result.AppendLine("Invalid password");

    return result.ToString();
}

Which you call from your validation method to get an error message in the case that status is NOT CheckInput.OK:

...
if (status != CheckInput.OK)
{
    MessageBox.Show(CreateErrorMessage(status));
}
else
{
    ... it's ok.

Or something along those lines.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • This is literally what I wanted to do, I substituted the "(!PasswordIsValid(password))" ect with a "GetValue.password.Text" through a new object so it's actually looking at the user input. (I work pretty slowly, will update my error messaging and update my reply). Thanks – Cameron Howell May 09 '16 at 13:54
0

In the documentation you can find:

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

And bit further:

An explicit cast is necessary to convert from enum type to an integral type. For example, the following statement assigns the enumerator Sun to a variable of the type int by using a cast to convert from enum to int.

So if you want your method to return your enum it has to be cast to an int (or other of the supported types if you don't use the default one). Also, your result has to be then cast from an int back to the enum type to get the enum value. I adjusted your code to apply that so you can see the idea behind. (Since I'm using a console app it's all converted to static and the messages are displayed to the console as well.)

class Program
{
    static void Main(string[] args)
    {
        var res = InputCheck("Piotr", "Wolkowski");
        ValidInputSwitch((CheckInput)res);
        Console.ReadLine();
    }

    public static int InputCheck(string firstName, string lastName)
    {
        if (string.IsNullOrEmpty(firstName))
        {
            return (int)CheckInput.No_First_Name;
        }

        return (int)CheckInput.OK;
    }

    public static void ValidInputSwitch(CheckInput status)
    {
        do
        {
            switch (status)
            {
                case CheckInput.No_First_Name:
                    Console.WriteLine("Please enter your first name.");
                    break;
                case CheckInput.No_Last_Name:
                    Console.WriteLine("Please enter your last name.");
                    break;
                case CheckInput.No_Password:
                    Console.WriteLine("Please enter your password.");
                    break;
                case CheckInput.Wrong_Password:
                    Console.WriteLine("Your passwords do not match!");
                    break;
                case CheckInput.OK:
                    Console.WriteLine("OK");
                    break;
                default:
                    break;
            }
        }
        while (status != CheckInput.OK);
    }
}
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68