找不到类型或命名空间(是否缺少 using 指令或程序集引用?

The type or namespace cannot be found (are you missing a using directive or an assembly reference?)

提问人:Leo 提问时间:4/27/2010 最后编辑:Tomas AschanLeo 更新时间:6/28/2017 访问量:190772

问:

尝试编译 C# 程序时出现以下错误:

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FootballLeague
{
    public partial class MainMenu : Form
    {
    FootballLeagueDatabase footballLeagueDatabase;
    Game game;
    Team team;
    Login login; //Error here

    public MainMenu()
    {
        InitializeComponent();
        changePanel(1);
    }

    public MainMenu(FootballLeagueDatabase footballLeagueDatabaseIn)
    {
        InitializeComponent();
        footballLeagueDatabase = footballLeagueDatabaseIn;
    }

    private void Form_Loaded(object sender, EventArgs e)
    {
    }

    private void gameButton_Click(object sender, EventArgs e)
    {
        int option = 0;
        changePanel(option);
    }
    private void scoreboardButton_Click(object sender, EventArgs e)
    {
        int option = 1;
        changePanel(option);
    }
    private void changePanel(int optionIn)
    {
        gamePanel.Hide();
        scoreboardPanel.Hide();

        string title = "Football League System";

        switch (optionIn)
        {
            case 0:
                gamePanel.Show();
                this.Text = title + " - Game Menu";
                break;
            case 1:
                scoreboardPanel.Show();
                this.Text = title + " - Display Menu";
                break;
        }
    }

    private void logoutButton_Click(object sender, EventArgs e)
    {
        login = new Login();
        login.Show();
        this.Hide();
    }

Login.cs类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FootballLeagueSystem
{
    public partial class Login : Form
    {
    MainMenu menu;
    public Login()
    {
        InitializeComponent();
    }

    private void administratorLoginButton_Click(object sender, EventArgs e)
    {
        string username1 = "08247739";
        string password1 = "08247739";

        if ((userNameTxt.Text.Length) == 0)
            MessageBox.Show("Please enter your username!");
        else if ((passwordTxt.Text.Length) == 0)
            MessageBox.Show("Please enter your password!");
        else if (userNameTxt.Text.Equals("") || passwordTxt.Text.Equals(""))
            MessageBox.Show("Invalid Username or Password!");
        else
        {
            if (this.userNameTxt.Text == username1 && this.passwordTxt.Text == password1)
                MessageBox.Show("Welcome Administrator!", "Administrator Login");
            menu = new MainMenu();
            menu.Show();
            this.Hide();
        }
    }

    private void managerLoginButton_Click(object sender, EventArgs e)
    {
        {
            string username2 = "1111";
            string password2 = "1111";

            if ((userNameTxt.Text.Length) == 0)
                MessageBox.Show("Please enter your username!");
            else if ((passwordTxt.Text.Length) == 0)
                MessageBox.Show("Please enter your password!");
            else if (userNameTxt.Text.Equals("") && passwordTxt.Text.Equals(""))
                MessageBox.Show("Invalid Username or Password!");
            else
            {
                if (this.userNameTxt.Text == username2 && this.passwordTxt.Text == password2)
                    MessageBox.Show("Welcome Manager!", "Manager Login");
                menu = new MainMenu();
                menu.Show();
                this.Hide();
            }
        }
    }

    private void cancelButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    }
}

错误在哪里?我做错了什么?

C# .NET 使用指令

评论

26赞 jamesmortensen 3/1/2013
这些评论有点没有必要。显然,请求者缺少程序集引用。问题是如何解决该问题,因为错误消息没有澄清这一点。
1赞 Vishwa G 11/28/2016
请查看链接,它可能有用 stackoverflow.com/a/40838955/3763015
1赞 Vishwa G 11/28/2016
请参阅链接,这可能会有所帮助 stackoverflow.com/a/40838955/3763015
1赞 Paul McCarthy 3/21/2019
让我烦恼的是 Visual Studio 无法修复它。我缺少在我的解决方案中其他地方使用的东西。建议的解决方案应具有从解决方案中的另一个项目复制缺少的引用的选项。

答:

4赞 ChrisF 4/27/2010 #1

您需要添加以下行:

using FootballLeagueSystem;

添加到.cs.cs所有使用 .Login

目前,编译器找不到该类。Login

评论

0赞 Leo 4/27/2010
我在我的 MainMenu 表单中使用了这一行,但我在使用 System 的 programme.cs 类中遇到了同样的错误;使用 System.Collections.Generic;使用 System.Linq;使用 System.Windows.Forms;namespace FoolballLeague { static class Program { /// <summary> /// 应用程序的主要入口点 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(新的 **Login());} } **登录());错误显示在这里 }
2赞 Lasse V. Karlsen 4/27/2010
因此,您将相同的 using 指令添加到 program.cs 中。这里有什么问题?阅读错误消息...
0赞 Joel Etherton 7/14/2010
或者进入相关项目的属性,并将其添加到全局导入的命名空间列表中(如果存在)。
25赞 George Stocker 4/27/2010 #2

您没有 Login 类所在的命名空间作为引用。

将以下内容添加到使用该类的窗体中:Login

using FootballLeagueSystem;

当您想在另一个命名空间中使用某个类时,您必须告诉编译器在哪里可以找到它。在本例中,is 在命名空间内,或者 : 是完全限定的命名空间LoginFootballLeagueSystemFootballLeagueSystem.Login

正如评论者所指出的,您在命名空间中声明了 Login 类,但您在命名空间中使用它。FootballLeagueSystemFootballLeague

评论

0赞 Leo 4/27/2010
登录在FootballLeagueSystem中。它不应是完全限定的命名空间。
5赞 Lasse V. Karlsen 4/27/2010
这是与表单所在的命名空间不同的命名空间,因此您可以添加 using 指令,或者完全限定名称。错误消息是正确的。
0赞 Tommy 7/28/2012 #3

如果您在项目中的单独文件夹中拥有登录名,请确保在使用它时执行以下操作:

using FootballLeagueSystem.[Whatever folder you are using]

评论

1赞 dmportella 10/11/2012
虽然这是真的,但线索在他的源代码上......即登录类的命名空间与主窗体不同。
47赞 Parrhesia Joe 12/21/2013 #4

当我的项目 .net 框架版本与我链接到的 DLL 的框架版本不匹配时,我会收到此错误。就我而言,我得到了:

“找不到类型或命名空间名称'UserVoice'(是否缺少 using 指令或程序集引用?

UserVoice 是 .Net 4.0,我的项目属性设置为“.Net 4.0 Client Profile”。在项目上更改为 .Net 4.0 清除了错误。我希望这对某人有所帮助。

评论

0赞 DaveN59 3/13/2014
谢谢!当我读到这篇文章时,我记得以前遇到过它,但一开始我被难住了......
4赞 Auspex 7/27/2016
点对点。这表明指出“你是否缺少 using 指令或程序集引用?”的注释不一定相关。如果您实际上没有执行其中任何一个操作,则应该在编译时之前看到源代码中的错误。如果你有不兼容的程序集,你可能会发疯,想知道为什么在编译中找不到看起来完全像正确声明的程序集的东西。
0赞 Craig0409 9/16/2022
我在引用 4.6.1 目标项目的 .net framework 4.8 项目中遇到了类似的问题。将 4.6.1 项目切换到目标 4.8 为我修复了它。
0赞 Anup Prakash Tiwari 6/28/2017 #5

出现此错误是因为 compile 不知道在哪里可以找到该类。因此,它主要发生在复制或导入项目时。来解决这个问题.. 1.将 formName.cs 和 formName.Designer.cs 中的命名空间更改为项目的名称。