Why Some Methods in C# fail with an Error "cannot explicitly call operator or accessor"?

By Barbara Han

When using C# in .NET., one developer got an error “"cannot explicitly call operator or accessor" when he tried to build his project that uses certain functions. Both utodesk.AutoCAD.DatabaseServices.Entity.get_LayerId and Autodesk.AutoCAD.DatabaseServices.Entity.get_Layer fail with this error.

The reason is that you should not use any get_ methods. To get an entity’s layer name, use the Entity.Layer property instead, as demonstrated in the following sample:

    // Modal Command with localized name

    [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]

    public void MyCommand() // This method can have any name

    {

      // Put your command code here

      Document curDoc = Application.DocumentManager.MdiActiveDocument;

      Database db = curDoc.Database;

      Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;

 

      using (Transaction myT = tm.StartTransaction())

      {

        BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);

        BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead,false);

        foreach (ObjectId acObjId in btr)

        {

          Entity ent = (Entity)myT.GetObject(acObjId, OpenMode.ForRead);

          string myLayName;

          myLayName = ent.Layer;

        }

 

        myT.Commit();

 &#16
0;    }   

    }

This is same for other methods, for example get_LinetypeScale().

          double myScale;

          //myScale = ent.get_LinetypeScale();

          //Use .LinetypeScale instead

          myScale = ent.LinetypeScale;


Comments

One response to “Why Some Methods in C# fail with an Error "cannot explicitly call operator or accessor"?”

  1. There is a big question in me, can I try another method on Java?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading