Home > Software engineering >  How can I add a static 'Main' method suitable for an entry point?
How can I add a static 'Main' method suitable for an entry point?

Time:07-16

I am getting an error on my code that says "Error CS5001
Program does not contain a static 'Main' method suitable for an entry point" I am coding in C# using Microsoft Visual Studio and .NET. This is my code.

    using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;






class projectsummer
{
    [CommandMethod("OpenDrawing", CommandFlags.Session)]
   

    public static void OpenDrawing()

    

    {
        string strFileName = "C:\\DRAFT.dwg";
        DocumentCollection acDocMgr = Application.DocumentManager;

        if (File.Exists(strFileName))
        {
            acDocMgr.Open(strFileName, false);
        }
        else
        {
            acDocMgr.MdiActiveDocument.Editor.WriteMessage("File "   strFileName  
                                                            " does not exist.");
        }
    }
}

I am not sure how to go about this error. Thank you!

CodePudding user response:

Looking at this post and your previous question, let's try and break down what's going on.

  1. You created a new Console application in Visual Studio. You did not tick "Do not use top level statements". This gave you a Program.cs file that was essentially empty (there was no "Main" method visible).
  2. You erased the Hello World code given to you, and went to make a static method - the code from your previous question.
  3. Damien_The_Unbeliever commented that based on the error, you put your method inside a "top level statement" file, and to put your method inside a class.
  4. You wrap your method (which is still inside Program.cs) in a class, and now suddenly you get a Can't Find Entry Point error.

User Ryan Pattillo posted a great explanation of the original issue - where your method was "by itself" in the Program.cs file. You should follow their advice, but you should also ensure that this class is in its own file.

You should end up with this:

Program.cs

// this is the entire contents of the file
using ConsoleApp1;

ProjectSummer.OpenDrawing();

ProjectSummer.cs

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;

namespace ConsoleApp1
{
    public class ProjectSummer
    {
        [CommandMethod("OpenDrawing", CommandFlags.Session)]
        public static void OpenDrawing()
        {
            // ...
        }
    }
}

Change ConsoleApp1 to the name of your project.

The entry point of your application, which right now is the only file that has "top level statements", remains Program.cs, thus you fix the Can't Find Entry Point error.


Another adjustment you can make, which seeing you're new to C# might be useful, is to not use top level statements at all. Modify your Program.cs to this:

namespace ConsoleApp1
{
    internal static class Program
    {
        // this is your program's entry point
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            ProjectSummer.OpenDrawing();
        }
    }
}

Change ConsoleApp1 to the name of your project.

CodePudding user response:

You cannot use the AutoCAD .NET API out of process. To be able to use the AutoCAD .NET libraries, you have to build a "class library" project (DLL) and NETLOAD this DLL from a running AutoCAD process. See this topic about in-process vs out-of-process and you can start from this other one to see how to create an AutoCAD .NET project.

  • Related