Reputation: 1
I am using Microsoft Visual Studios, C#, and .NET. This is my code
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
[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.");
}
}
When I try to run my code I get an error for public that says "CS0106: The modifier 'public' is not valid for this item. I also get a warning for OpenDrawing that says "CS8321: The local function 'OpenDrawing' is declared but never used.
I am pretty new to using C#, so any feedback would be helpful. Thank you!
Upvotes: 0
Views: 167
Reputation: 370
The method is written as a top-level statement. The methods documentation says this:
In an application that uses top-level statements, the
Main
method is generated by the compiler and contains all top-level statements.
This means that OpenDrawing
is effectively a local function within the compiler-generated Main
method. This causes an error because local functions can't have access modifiers.
This can be fixed by placing the method in a class (and preferably a namespace):
namespace MyNamespace
{
public class MyClass
{
// your OpenDrawing declaration here
}
}
It can then be called as MyClass.OpenDrawing
. This class should be kept in its own file with the same name as the class (i.e. MyClass.cs
in the example) to keep the code organized.
Upvotes: 1