Search code examples
c#classmethodsswitch-statementclass-method

switch-case statement as a method in a public class


Looking to implement a switch case statement method inside a class. I have a class that I'm writing to from a SQL select and sql datareader. Having trouble setting up the Status class which I pass in a string to it and it returns the corresponding string result and saves it to appstatus. I can't add the break after each case because I receive unreachable code detected. Now I'm getting this error: Error- member names cannot be the same as their enclosing type

public class SampleData
{
    public SampleData()
    {
    }

    public string name { get; set; }
    public string phoneNbr { get; set; }
    public Status appstatus { get; set; }
}

public class Status
{
    public Status() { }
    public string Status(string l)
    {
        switch (l)
        {
            case "A":
                return "Yes";
            case "B":
                return "No";
            case "C":
                return "Okay";
            case "D":
                return "Maybe";
            case "E":
                return "Need More Info";
            default:
                return ("Unknown");
        }
    }

}

Here is where I access the class and write to it from a SQL select.

   ...using (SqlDataReader read = cmd.ExecuteReader())
                {
                    while (read.Read())
                    {
                        try
                        {
                            SampleData d1 = new SampleData();
                            d1.name = Convert.ToString(read["..."]);
                            d1.phoneNbr = Convert.ToString(read["..."]);                            
                            d1.appstatus = new Status(Convert.ToString(read["..."]).Trim());

                            list.Add(d1);
                        }
        }
         }

Solution

  • The only issues I can see (I think!) are that you need to store the value inside the Status object otherwise the value is just returned and not utilised. Secondly make sure you use .ToUpper() on the l variable, just in case the data from the database is in lowercase. Unless you want any lowercase values to fall through to the default of the switch statement.

    Now if you want to access the value, just use SampleData.Status.Value.

    public class Status
    {
        public string Value { get; set; }
    
        //public Status() { }
    
        public Status(string l)
        {
            switch (l.ToUpper())
            {
                case "A":
                    Value = "Yes";
                    break;
                case "B":
                    Value = "No";
                    break;
                case "C":
                    Value = "Okay";
                    break;
                case "D":
                    Value = "Maybe";
                    break;
                case "E":
                    Value = "Need More Info";
                    break;
                default:
                    Value = "Unknown";
                    break;
            }
        }
    }