Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing partial modifier on declaration ..another partial declaration of this type exists". I'm a beginner and just following the book

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".

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;
       }
    }
}
like image 249
user1742237 Avatar asked Oct 12 '12 20:10

user1742237


2 Answers

In another place you declared a class with the same name (Form1) and there it's declared with the partial modifier.

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 partial modifier to the declaration:

public partial class Form1
{
}
like image 106
Adriano Repetti Avatar answered Oct 15 '22 14:10

Adriano Repetti


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.

more on that in msdn

like image 5
Igarioshka Avatar answered Oct 15 '22 16:10

Igarioshka