在这种情况下如何正确实现开关? [C#,Visual Studio 2017]

问题描述 投票:0回答:2

编写以下代码:

        class Person
        {
            protected string firstName, lastName, phoneNumber = "";
            protected bool biologicalSex = true;
            protected DateTime birthDate = DateTime.Today;
            protected int idNumber, idType = 0;

            public string pFirstName { get { return firstName; } set { firstName = value; } }

            public string pLastName { get { return lastName; } set { lastName = value; } }

            public bool pBiologicalSex { get { return biologicalSex; } set { biologicalSex = value; } }

            public DateTime pBirthDate { get { return birthDate; } set { birthDate = value; } }

            public string pPhoneNumber { get { return phoneNumber; } set { phoneNumber = value; } }

            public int pIdNumber { get { return idNumber; } set { idNumber = value; } }

            public int pIdType { get { return idType; } set { idType = value; } }


            public string ChoicesToStrings(string argument)
            {
                if (argument == "pIdType")
                {
                    if (pIdType == 1)
                        return "Social Security Card";
                    else
                    {
                        if (pIdType == 2)
                            return "Driver's License";
                        else
                            if (pIdType == 3)
                            return "Passport";
                        else
                            return "Department of Defense ID Card";
                    }
                } 
                else
                {
                    if (pBiologicalSex == true)
                        return "Male";
                    else
                        return "Female";
                }                    
            }

            public string toStringPerson()
            {
                return "First Name: " + pFirstName + "\nLast Name: " + pLastName +
                     "\nBirth Date: " + pBirthDate.ToShortDateString() + "\nBiologic Sex: " + ChoicesToStrings("pBiologicalSex") +
                    "\nPhone Number: " + pPhoneNumber + "\nID Type: " + ChoicesToStrings("pIdType") + "\nID Number: " + pIdNumber;
            }

而且效果很好,但是我想实现开关而不是如果有条件的话

尝试以下代码:

            class Person
            {
                protected string firstName, lastName, phoneNumber = "";
                protected bool biologicalSex = true;
                protected DateTime birthDate = DateTime.Today;
                protected int idNumber, idType = 0;

                public string pFirstName { get { return firstName; } set { firstName = value; } }

                public string pLastName { get { return lastName; } set { lastName = value; } }

                public bool pBiologicalSex { get { return biologicalSex; } set { biologicalSex = value; } }

                public DateTime pBirthDate { get { return birthDate; } set { birthDate = value; } }

                public string pPhoneNumber { get { return phoneNumber; } set { phoneNumber = value; } }

                public int pIdNumber { get { return idNumber; } set { idNumber = value; } }

                public int pIdType { get { return idType; } set { idType = value; } }


                private void ChoicesToStrings(string[] args)
                {                    
                    switch (string argument)
                    {
                        case 'pIdType':
                            if (pIdType == 1)
                                return "Social Security Card";
                            else
                            {
                                if (pIdType == 2)
                                    return "Driver's License";
                                else
                                    if (pIdType == 3)
                                    return "Passport";
                                else
                                    return "Department of Defense ID Card";
                            };
                            break;
                        case 'pBiologicalSex':
                            if (pBiologicalSex == true)
                                return "Male";
                            else
                                return "Female";
                            break;
                    }
                }

                public string toStringPerson()
                {
                    return "First Name: " + pFirstName + "\nLast Name: " + pLastName +
                         "\nBirth Date: " + pBirthDate.ToShortDateString() + "\nBiologic Sex: " + ChoicesToStrings("pBiologicalSex") +
                        "\nPhone Number: " + pPhoneNumber + "\nID Type: " + ChoicesToStrings("pIdType") + "\nID Number: " + pIdNumber;
                }

但是它不起作用。在这种情况下,实现开关的正确方法是什么?

我想使用开关,因为我有很多选择,我需要转换为字符串(使用开关对我来说比使用太多ifs更干净),但是我还无法弄清楚该怎么做(不是在这种情况下)

谢谢

c# if-statement switch-statement conditional-statements crud
2个回答
1
投票

我在这里承担。您可以开始或移动的东西。

private string ChoicesToStrings(string argument)
    {
        switch (argument)
        {
            case "pIdType":
                {
                    switch (pIdType)
                    {
                        case 1:
                            return "Social Security Card";
                        case 2:
                            return "Driver's License";
                        case 3:
                            return "Passport";
                        default:
                            return "Department of Defense ID Card";
                    }
                }
            case "pBiologicalSex":          
                return pBiologicalSex == true ? "Male" : "Female";              
            default:
                return string.Empty;
        }
    }

1
投票

您可以尝试类似的操作,在switch值上使用pIdType,并使用三元运算符获取性别名称

public string ChoicesToStrings(string argument)
{
    if (argument == "pIdType")
    {
        switch (pIdType)
        {
            case 1:
                return "Social Security Card";
            case 2:
                return "Driver's License";
            case 3:
                return "Passport";
            default:
                return "Department of Defense ID Card";
        }
    }

    return pBiologicalSex ? "Male" : "Female";
}

如果需要在switch类中将ANY属性用于Person,则可以使用Func获取值(而不是为每个属性名称切换),并简化ChoicesToStrings方法,例如那

private Func<Person, string> idTypeSelector = person =>
{
    switch (person.pIdType)
    {
        case 1:
            return "Social Security Card";
        case 2:
            return "Driver's License";
        case 3:
            return "Passport";
        default:
            return "Department of Defense ID Card";
    }
};

private Func<Person, string> genderSelector = person => person.pBiologicalSex ? "Male" : "Female";
...
public string ChoicesToStrings(string argument)
{
    switch (argument)
    {
        case nameof(pIdType):
            return idTypeSelector(this);
        case nameof(pBiologicalSex):
            return genderSelector(this);
        default:
            return string.Empty;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.