Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a contact managers program in a console application using a list to store and display the data. I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. I have a method to create a list with data and a contact object. contact object is all stings except for ContactTypes which is a Type data type. I keep getting the error Cannot implicitly convert type string to System.Type in createContact() method. I am not sure how to fix this.

any guidance would be appreciated

C#
static void Main(string[] args)
        {         
            //Declare the list

            List<Contact> contactList = new List<Contact>();
                       
            //Main Driver
            char menuItem;
             Console.WriteLine("Contact List\n");
            menuItem = GetMenuItem();
            while (menuItem != 'Q')
            {

                ProcessMenuItem(menuItem, contactList);
                menuItem = GetMenuItem();

            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
            while (menuItem != 'C'
                && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(IOConsole.GetChar("\nEnter option or M for menu:"));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
           Console.WriteLine("C-> Create Contacts");
           Console.WriteLine("R-> Remove Contacts");
           Console.WriteLine("U-> Update Contacts");
           Console.WriteLine("D -> Load data from file");
           Console.WriteLine("S-> Save data to file");
           Console.WriteLine("L-> View sorted by last name");
           Console.WriteLine("F-> View sorted by first name");
           Console.WriteLine("P-> View by partial name search");
           Console.WriteLine("T-> View by contact type");
           Console.WriteLine("Q-> Quit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, List<Contact> contactList)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    removeContact(contactList);
                    break;
                case 'U':
                    updateContact(contactList);
                    break;
                case 'D':
                    LoadToFile();
                    break;
                case 'S':
                    saveToFile();
                    break;
                    
                case 'L':
                    sortByLastName(contactList);
                    break;
                case 'F':
                    sortByFirstName(contactList);
                       break;
                case 'P':
                       DisplayList(contactList);
                       break;
                case 'T':
                       sortByContactType();
                       break;
                case 'Q':
                       
                       break;

            }                   
        }

         public static void createContact()
        {
            Contact c1 = new Contact();
            Console.WriteLine("\nGetFirstName");
            c1.GetFirstName = Console.ReadLine();
            Console.WriteLine("\nGetLastName");
            c1.GetLastName = Console.ReadLine();
            Console.WriteLine("\nGetEmailAddress");
            c1.GetEmailAddress = Console.ReadLine();
            Console.WriteLine("\nGetPhoneNumber");
            c1.GetPhoneNumber = Console.ReadLine();
            Console.WriteLine("\nContactTypes");
            c1.ContactTypes = Console.ReadLine();

            //Create more contacts...

            //Add all contacts here
            ContactCollection contactList = new ContactCollection();
            contactList.Add(c1);

            //Loop through list
            foreach (Contact c in contactList)
            {
                Console.WriteLine(c.GetFirstName);
                Console.WriteLine(c.GetLastName);
                Console.WriteLine(c.GetEmailAddress);
                Console.WriteLine(c.GetPhoneNumber);
                Console.WriteLine(c.ContactTypes);
               
            }

            Console.ReadLine();

        }
Posted
Updated 27-Jul-15 20:43pm
v3
Comments
Maciej Los 28-Jul-15 2:36am    
Where is the definition for contact class?

C#
using System.ComponentModel;

TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);
 
Share this answer
 
v2
Comments
Maciej Los 28-Jul-15 2:39am    
First of all: it won't help, because this is not a problem (in my opinion).
Secondly, please, add few words of comment.
Note: i'm not a down-voter.
If ContactType is an enum, you need to convert the string value you read from console to corresponding enum value.

For example, if ContactType is defined like:

C#
public enum ContactType
{
Home,
Office
}


And user enters "Home", then you need co convert "Home" (string) to ContactType.Home (Type/enum)

You can do this the following way:

ContactType contactType = (ContactType) Enum.Parse(typeof(ContactType), contactTypeString);   
 
Share this answer
 
Comments
Maciej Los 28-Jul-15 2:45am    
Wrong approach. Please, read the question carefully and see my comment to the question.
himanshu agarwal 28-Jul-15 2:54am    
Hmmm, I went by what Josh said, that, only ContactType is not a string and I believe that is the place where the implicit typecast would fail.

If that is the case, I need not ask him to provide the Contact class or the custom collection he has defined.
Member 11878183 6-Jun-19 3:47am    
In my case, it works for me. As it is, what I am looking for. I try to convert int values into a system.Type; here Type is my enums [flags] that I am loading using reflection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900