C# 声明中缺少部分修饰符……存在这种类型的另一个部分声明”。我是初学者,只是在看这本书
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12866436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing partial modifier on declaration ..another partial declaration of this type exists". I'm a beginner and just following the book
提问by user1742237
I'm a beginner and I'm doing an exercise following a book. Below is my code and i got this
我是初学者,我正在按照一本书做练习。下面是我的代码,我得到了这个
"Missing partial modifier on declaration of type 'Windowsform.Form1'; another partial declaration of this type exists".
"Missing partial modifier on declaration of type 'Windowsform.Form1'; another partial declaration of this type exists".
What should i do?My code is as follows:
我该怎么办?我的代码如下:
using System;
using System.Windows.Forms;
namespace Windowsform
{
public class Form1 : Form
{
private TextBox txtEnter;
private Label lblDisplay;
private Button btnOk;
public Form1()
{
this.txtEnter = new TextBox();
this.lblDisplay = new Label();
this.btnOk = new Button();
this.Text = "My Hellowin app!";
//txtEnter
this.txtEnter.Location = new System.Drawing.Point(16, 32);
this.txtEnter.Size = new System.Drawing.Size(264, 20);
//lblDisplay
this.lblDisplay.Location = new System.Drawing.Point(16, 72);
this.lblDisplay.Size = new System.Drawing.Size(264, 128);
//btnOK
this.btnOk.Location = new System.Drawing.Point(88, 224);
this.btnOk.Text = "OK";
this.btnOk.Click +=
new System.EventHandler(this.btnOK_Click);
//MyForm
this.Controls.AddRange(new Control[] {
this.txtEnter, this.lblDisplay, this.btnOk});
}
static void Main()
{
Application.Run(new Form1());
}
private void btnOK_Click(object sender, System.EventArgs e)
{
lblDisplay.Text = txtEnter.Text + "\n" + lblDisplay.Text;
}
}
}
回答by Adriano Repetti
In another place you declared a class with the same name (Form1) and there it's declared with the partialmodifier.
在另一个地方,您声明了一个具有相同名称 ( Form1)的类,并且在那里用partial修饰符声明了它。
If you're splitting your class into two different files (for example this file for UI layout and another file for logic) then simply add the partialmodifier to the declaration:
如果您将类拆分为两个不同的文件(例如,此文件用于 UI 布局,另一个文件用于逻辑),则只需将partial修饰符添加到声明中:
public partial class Form1
{
}
回答by Igarioshka
When you create a new form, it is actually created in two separate places. one place the class is declared is where all the plumbing is being implemented, and the file that you pasted here, to write all the functionality. for the sake of making this possible, c# has a partial class feature, if in some place of the assembly a class is defined with a partial modifier, then one can define multiple declarations of the class, as long as it has that modifier.
当您创建一个新表单时,它实际上是在两个不同的地方创建的。声明类的地方是实现所有管道的地方,以及您在此处粘贴的文件,以编写所有功能。为了使这成为可能,c# 具有分部类功能,如果在程序集的某个地方用分部修饰符定义了一个类,则可以定义该类的多个声明,只要它具有该修饰符。
回答by Sum None
I may be completely wrong about this, but I was getting this same error (and more) with MVC3 and EF4. After banging my head on the desk for a day or so, I finally decided to upgrade to MVC4 and just suffer through that learning curve if MVC3 wasn't going to cooperate. The funny thing is that all my errors went away.
我可能完全错了,但我在 MVC3 和 EF4 中遇到了同样的错误(以及更多)。在我的头撞在桌子上一天左右之后,我终于决定升级到 MVC4,如果 MVC3 不合作,我就只能忍受学习曲线了。有趣的是,我所有的错误都消失了。
Before upgrading, I must have done 100 searches trying to find my duplicate declarations.
在升级之前,我必须进行 100 次搜索以试图找到我的重复声明。
Seems MVC4 and EF4 play more nice together.
似乎 MVC4 和 EF4 一起玩得更好。
回答by Mike Wallace
Just in case someone stumbles into this like me... I have been switching back and forth between Visual Studio 2013 for Web and Visual Studio 2015 RC to play with my MVC solutions.
以防万一有人像我一样偶然发现……我一直在 Visual Studio 2013 for Web 和 Visual Studio 2015 RC 之间来回切换,以使用我的 MVC 解决方案。
If you attempt to open an MVC4 solution in 2015 RC it does some automagic which can cause this error to come up next time you open that same solution in 2013 for Web.
如果您尝试在 2015 RC 中打开 MVC4 解决方案,它会执行一些自动魔术,这可能会导致下次您在 2013 年为 Web 打开相同的解决方案时出现此错误。
I ended up rolling back to a previous SVN revision and only using 2013 for Web and my code built fine just like it had for years. I know this isn't the proper fix but this is what caused this to 'pop up' out of nowhere in my instance.
我最终回滚到以前的 SVN 修订版,只使用 2013 年的 Web 版本,我的代码构建得很好,就像多年来一样。我知道这不是正确的修复,但这就是导致它在我的实例中突然“弹出”的原因。
回答by sdjuan
TL;DR: caused by vestiges of classes that were renamed by VS2013 but that VS2013 didn't properly rename everywhere, leaving instances of the old name hanging about in undisclosed places.
TL;DR:由 VS2013 重命名的类的残余引起,但 VS2013 没有在所有地方正确重命名,导致旧名称的实例悬在未公开的地方。
DETAIL: I had the exact same error in VS2013, in my case initially I added a new class "Employee" but then realized it should have been AddEmployee so I right-clicked and changed the name and it 'appeared' to change it in all the right places.
Quite a while later I tried to add an Employee class and then got the error. After visiting here and not finding a fix, I searched 'entire solution' for other instances of Employee and that turned up a couple of fails from when I had previously renamed Employee to AddEmployee:
详细信息:我在 VS2013 中遇到了完全相同的错误,在我的情况下,最初我添加了一个新类“Employee”,但后来意识到它应该是 AddEmployee,所以我右键单击并更改了名称,它“出现”以将其全部更改正确的地方。
一段时间后,我尝试添加一个 Employee 类,然后出现错误。访问此处并没有找到修复程序后,我搜索了 Employee 的其他实例的“整个解决方案”,结果出现了几次失败,因为我之前将 Employee 重命名为 AddEmployee:
AddEmployee.aspx.cs: public partial class Employee : System.Web.UI.Page
AddEmployee.aspx.designer.cs: public partial class Employee {
Updated those two to AddEmployee and the error went away and compiled and ran as it should..
将这两个更新为 AddEmployee 并且错误消失并按预期编译和运行..
回答by Jamshaid Kamran
In my case this error occurred when I was using a web form application in asp.netand it was because in my global.asaxmarkup, I had replace CodeBehindAttribute with CodeFile. Changing it back fixed the problem.
在我的情况下,当我在其中使用 Web 表单应用程序时发生此错误asp.net,这是因为在我的global.asax标记中,我已将CodeBehindAttribute替换为CodeFile. 把它改回来解决了这个问题。

