找不到类型或命名空间名称''(您是否缺少using指令或程序集引用?) [英] The type or namespace name ' ' could not be found (are you missing a using directive or an assembly reference?)

查看:527
本文介绍了找不到类型或命名空间名称''(您是否缺少using指令或程序集引用?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用列表来存储和显示数据,从而在控制台应用程序中构建联系人管理器程序。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个联系人和一个联系人对象。我还有一种更新联系人的方法,但我想让用户能够选择联系人的名字,姓氏,电子邮件地址,电话号码,类型,并能够使用用户输入的信息进行更新。但是在这一行c1.GetContactType =(ContactTypes)Enum.Parse(typeof(ContactTypes),Console.ReadLine(),true);我一直收到错误:



无法找到类型或命名空间名称'ContactTypes'(您是否缺少using指令或程序集引用?)





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 contact and a contact object. I also have a method to update a contact but I want to have the user to be able to pick a contact first name, last name, email address, phone number, type and be able to update with the information the user entered. However on this line c1.GetContactType = (ContactTypes)Enum.Parse(typeof(ContactTypes), Console.ReadLine(), true); I keep getting the error:

The type or namespace name 'ContactTypes' could not be found (are you missing a using directive or an assembly reference?)


 public static void createContact()
{
    Contact c1 = new Contact();
    try {
    Console.WriteLine("\nGetFirstName");
    c1.GetFirstName =  Console.ReadLine();
        }
    catch (System.NullReferenceException)
    {
        Console.WriteLine("Contact create failed");
    }
    Console.WriteLine("\nGetLastName");
    c1.GetLastName = Console.ReadLine();
    Console.WriteLine("\nGetEmailAddress");
    c1.GetEmailAddress = Console.ReadLine();
    Console.WriteLine("\nGetPhoneNumber");
    c1.GetPhoneNumber = Console.ReadLine();
    Console.WriteLine("\nContactTypes");
     //ERROR LINE//
    c1.GetContactType = (ContactTypes)Enum.Parse(typeof(ContactTypes), Console.ReadLine(), true);

    //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();

}



如果需要,这是我的联系课程




Here is my Contact class if needed

class Contact
    {

        //private member variables
        private String _firstName;
        private String _lastName;
        public ContactTypes _contactTypes;
        private String _phoneNumber;
        private String _emailAddress;




        //Public constructor that takes five arguments
        public Contact()
        {
            //Call the appropriate setter (e.g. FirstName) to set the member variable value
            /*GetFirstName = firstName;
            GetLastName = lastName;
            ContactTypes = contactTypes;
            GetPhoneNumber = phoneNumber;
            GetEmailAddress = emailAddress;*/

        }


        /*********************************************************************
         * Public accessors used to get and set private member variable values
         *********************************************************************/
        //Public  ContactTypes accessor
            public ContactTypes GetContactType
        {
            get
            {
                //Return member variable value
                return _contactTypes;
            }
            set
            {
                //Validate value and throw exception if necessary
                  if (value == " ")
           {
                    //throw new Exception("ContactType must have a value");
           }
                   else
           {
                    //Otherwise set member variable value
                    _contactTypes = value;
           }
            }
        } 
        public enum ContactTypes { Family, Friend, Professional }
        //Public FirstName accessor: Pascal casing
        public String GetFirstName
        {
            get
            {
                //Return member variable value
                return _firstName;
            }
            set
            {
                //Validate value and throw exception if necessary
                if (value == "")
                {
                    throw new Exception("First name must have a value");
                }
                else
                {
                    //Otherwise set member variable value
                    _firstName = value;
                }
            }
        }

        //Public LastName accessor: Pascal casing
        public String GetLastName
        {
            get
            {
                //Return member variable value
                return _lastName;
            }
            set
            {
                //Validate value and throw exception if necessary
                if (value == "")
                    throw new Exception("Last name must have a value");
                else
                    //Otherwise set member variable value
                    _lastName = value;
            }
        }



        //Public PhoneNumber accessor
        public String GetPhoneNumber
        {
            get
            {
                //Return member variable value
                return _phoneNumber;
            }
            set
            {
                /*bool isValid = Regex.IsMatch(value, @"/d{3}-/d{3}-/d{4}");
                if (!isValid)
                {
                    throw new Exception("PhoneNumber must have a value");
                }
                else
                {
                    _phoneNumber = value;
                }*/
                //Validate value and throw exception if necessary
                if (value == "")
                {
                    throw new Exception("PhoneNumber must have a value");
                }
                else
                {
                    //Otherwise set member variable value
                    _phoneNumber = value;
                }
            }
        }



        //Public Email accessor
        public String GetEmailAddress
        {
            get
            {
                //Return member variable value
                return _emailAddress;
            }
            set
            {
                //Validate value and throw exception if necessary
                if (value == "")
                {
                    throw new Exception("EmailAddress must have a value");
                }
                else
                {
                    //Otherwise set member variable value
                    _emailAddress = value;
                }
            }
        }

    }

推荐答案

你定义了ContactTypes里面的ContactTypes所以它是一个只与Contact有关的Type

得到ContactTypes在Contact之外的定义或使用
you defined ContactTypes inside Contact so it is a Type that is related to Contact only
get the definition of ContactTypes outside Contact or use
Contact.ContactTypes

代替

ContactTypes

当您想要调用此枚举时。

when you want to call this Enum.


您的代码未显示ContactTypes枚举的定义 - 所以我们无法准确地告诉你该怎么做。 是的,它!我错过了它... [/ edit]

但是,Visual Studio可以。



将光标放在行中的单词ContactTypes中错误 - 在单词的开头会出现一个小的蓝色标记。

将鼠标悬停在蓝色标记上,然后会出现一个下拉菜单。

打开下拉列表,VS会为您提供修复它的选项,可能是通过添加适当的使用语句,或明确引用类的全名。
Your code doesn't show the definition of the ContactTypes enum - so we can't be exact in telling you what to do. [edit] Yes it does! I missed it...[/edit]
However, Visual Studio can.

Put the cursor in the word ContactTypes in the line with the error - a small blue marker will appear at the beginning of the word.
Hover the mouse over the blue marker, and a drop down will appear.
Open the drop down, and VS will give you options to fix it, probably by adding the appropriate using statement, or explicitly referencing the class full name.


这篇关于找不到类型或命名空间名称''(您是否缺少using指令或程序集引用?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆