dimanche 9 février 2020

How to correctly implement switches in this case? [C#, Visual Studio 2017]

Wrote the following code:

        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;
            }

And it works fine, but I want to implement switches instead of if conditionals

Tried the following code:

            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;
                }

But it doesn't work. What would be the correct way to implement switches in this case?

I want to use switches because I have maaany choices I need to convert to strings (and using switches looks cleaner to me, than using too many ifs), but I wasn't able to figure out how to do it yet (not in this specific case)

Thanks

Aucun commentaire:

Enregistrer un commentaire