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

Leo*_*Leo 16 .net c# using-directives

我尝试编译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();
    }
Run Code Online (Sandbox Code Playgroud)

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

    }
}
Run Code Online (Sandbox Code Playgroud)

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

Par*_*Joe 32

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

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

UserVoice是.Net 4.0,我的项目属性设置为".Net 4.0 Client Profile".在项目上更改为.Net 4.0可以清除错误.我希望这可以帮助别人.

  • 发现.这表明注释指出"你是否缺少using指令或汇编引用?" 不一定相关.如果你实际上没有完成其中任何一个,你应该在源_before_编译时看到错误.如果你有不兼容的程序集,你可能会疯狂地想知道为什么在编译中找不到像正确声明的程序集那样的_exactly_. (2认同)

Geo*_*ker 18

您没有Login类所在的命名空间作为参考.

将以下内容添加到使用Login该类的表单:

using FootballLeagueSystem;
Run Code Online (Sandbox Code Playgroud)

如果要在另一个命名空间中使用类,则必须告诉编译器在哪里找到它.在这种情况下,LoginFootballLeagueSystem命名空间内部,或者:FootballLeagueSystem.Login完全限定的命名空间.

正如评论者指出的那样,您在FootballLeagueSystem命名空间内声明了Login类,但是您在FootballLeague命名空间中使用它.

  • 哪个命名空间与表单所在的命名空间不同,因此要么添加using指令,要么完全限定名称.错误消息是正确的. (5认同)