'员工'是一个名称空间'但是像''类型'

时间:2015-11-09 00:54:55

标签: c#

'员工'是一个名称空间'但是像“'类型”一样使用。我似乎无法纠正4个错误。帮助任何人?第12和第27行。我对2条错误行进行了说明。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Employee
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee firstEmployee = new Employee();   **(2 errors on line for Employee)**
            ApplicationUtilities.DisplayApplicationInformation(); //display the                     header for the application
            ApplicationUtilities.DisplayDivider("Start Program"); //Show heading that the program has started
            ApplicationUtilities.DisplayDivider("Prompt for employee information and create first employee"); //Heading that shows we are ready to input first employee information
            firstEmployee.firstName = InputUtilities.getStringInputValue("First Name"); //Get first name input from the user
            firstEmployee.lastName = InputUtilities.getStringInputValue("Last Name");//Get last name input from the user
            firstEmployee.gender = InputUtilities.getCharInputValue("Gender");//Get gender input from the user
            firstEmployee.dependents = InputUtilities.getIntegerInputValue("# Dependents");//Get dependent input from the user
            firstEmployee.annualSalary = InputUtilities.getDoubleInputValue("Annual Salary");//Get annual salary input from the user

            Console.WriteLine("");
            Console.Write(firstEmployee.ToString());

            ApplicationUtilities.PauseExecution();
            Employee secondEmployee = new Employee("First Name", "Last Name", 'F', 3, 52000); ////declare an instance of second employee object with overloaded constructor called     **(2 errors on this line for - Employee)**
            Console.Write(secondEmployee.ToString());
            ApplicationUtilities.TerminateApplication();
        }
    }
}

2 个答案:

答案 0 :(得分:6)

正如错误所示,命名空间和类共享相同的名称。编译器对你想要实例化的内容感到困惑。

快速修复:在创建Employee的新实例时指定命名空间,因此编译器知道您要在同名的命名空间中创建类的新实例

Employee.Employee firstEmployee = new Employee.Employee();

正确修复:重命名命名空间,以便您可以更轻松地实例化该类。

您可能还想从对编译器非常了解的人那里阅读这篇文章:Do not name a class the same as its namespace

FWIW,这个大小的小应用程序在技术上并不需要命名空间。但是(正如Neolisk指出的那样)如果你确实使用了一个,那么use a good one。随着程序的增长,您可以在名为Employee的名称空间中对表示人员的类(例如Entity)进行分组。

答案 1 :(得分:0)

重命名您的Employee命名空间或重命名您的Employee类。

相关问题