簡體   English   中英

我的代碼中出現以下錯誤和警告,我不確定為什么

[英]I am getting the following errors and warnings in my code and I am not sure as to why

我正在使用 Microsoft Visual Studio、C# 和 .NET。 這是我的代碼

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.");
        }
    }

當我嘗試運行我的代碼時,我收到一個公共錯誤,顯示“CS0106:修飾符 'public' 對此項目無效。我還收到 OpenDrawing 警告,顯示“CS8321:聲明了本地函數 'OpenDrawing'但從未使用過。

我對使用 C# 很陌生,所以任何反饋都會有所幫助。 謝謝!

該方法被編寫為頂級語句 方法文檔是這樣說的:

在使用頂級語句的應用程序中, Main方法由編譯器生成並包含所有頂級語句。

這意味着OpenDrawing實際上是編譯器生成的Main方法中的一個本地函數 這會導致錯誤,因為本地函數不能具有訪問修飾符

這可以通過將方法放在一個類(最好是命名空間)中來解決:

namespace MyNamespace
{
    public class MyClass
    {
        // your OpenDrawing declaration here
    }
}

然后可以將其稱為MyClass.OpenDrawing 該類應保存在與該類同名的自己的文件中(即示例中的MyClass.cs ),以保持代碼的組織性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM