1

I get an error saying "Missing partial modifier on declaration of type 'projectName.Main'; another partial declaration of this type exists. From what i can read about this error, its because i have classes with same name. It does work if i modify the main class to public partial class Main : Form but i want to know why it gives me this error. I need to initializeComponent within Main(), tried creating a method start() and then calling main.Start() in a load event, but then the form loads blank.

namespace projectName
{
    public class Main : Form
    {
        public Main() // Method: Starts the main form
        {
            InitializeComponent();
        }

        public void Main_Load(object sender, EventArgs e)
        // On load of main class, handle events and arguments
        {
            Main main = new Main();
            main.getCurrentDomain();
        }

        public void getCurrentDomain() // Method: Get current domain
        {
            Domain domain = Domain.GetCurrentDomain();
        }
    } // Ends the main class
}
crashmstr
  • 28,043
  • 9
  • 61
  • 79
Johnny
  • 108
  • 1
  • 9
  • 2
    There can only exist a single class with a given name in a given namespace. You can't have two classes named `projectName.Main` in your assembly. You can, however, declare different chunks of the same `projectName.Main` class in different places, which is what `partial` is for. – Asad Saeeduddin Aug 05 '15 at 12:15
  • possible duplicate of [Why use partial classes?](http://stackoverflow.com/questions/3601901/why-use-partial-classes) – Binkan Salaryman Aug 05 '15 at 12:18

2 Answers2

5

Assuming this is a Windows Forms app, the problem is that the Visual Studio WinForms designer has created another file (e.g. Main.designer.cs) with:

public partial class Main : Form

to contain designer-generated code.

Your partial class source is effectively merged with that - but it can only happen when both source files declare that the class is partial. Otherwise, you're just trying to declare two classes with the same name in the same namespace, which is prohibited by C#.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

There is another file with the name .designer.cs. This file contains the partial definition of class and that is where your InitializeComponent function is defined. You need to add partial modifier to your class.

Sandeep
  • 399
  • 5
  • 15