Author Topic: Error Binding to Target Method  (Read 7203 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
Error Binding to Target Method
« on: May 05, 2007, 05:46:43 PM »
The followiing code produces this error:
Unhandled exception has occurred in a lcomponent in your application.  If you click continue, the application will ignore this error and attempt to continue.

Error binding to target method.

With the following:
************** Exception Text **************
System.ArgumentException: Error binding to target method.
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
   at AcMgCommandClass.InvokeWorker(AcMgCommandClass* , MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at AcMgCommandClass.InvokeWorkerWithExceptionFilter(AcMgCommandClass* , MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at AcMgPerDocumentCommandClass.Invoke(AcMgPerDocumentCommandClass* , gcroot<System::Reflection::MethodInfo ^>* mi, Boolean bLispFunction)
   at AcMgCommandClass.CommandThunk.Invoke(CommandThunk* )

This from VS2005 express.

Any suggestions?

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Colors


Public Class MKWCommands

  ' Define command 'Asdkcmd1'
  <CommandMethod("Create")> _
  Public Function Create()
    Dim Circle As Circle
    Dim btr As BlockTableRecord
    Dim bt As BlockTable
    Dim db As Database = HostApplicationServices.WorkingDatabase
    Dim oColor As New Color

    Dim trans As Transaction
    trans = db.TransactionManager.StartTransaction()

    Circle = New Circle(New Point3d(10, 10, 0), Vector3d.ZAxis, 2.0)


    bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
    Dim btrId As ObjectId = bt.Item(BlockTableRecord.ModelSpace)
    btr = trans.GetObject(btrId, OpenMode.ForWrite)

    btr.AppendEntity(Circle)
    trans.AddNewlyCreatedDBObject(Circle, True)
    trans.Commit()
    trans.Dispose()

  End Function

End Class

LE

  • Guest
Re: Error Binding to Target Method
« Reply #1 on: May 05, 2007, 07:04:50 PM »
I don't do VB.NET but check the name of your Circle variable to a lowercase circle = Dim Circle As Circle <<<-

Here is your function in C# HTH.
Code: [Select]
[CommandMethod("CREATE")]
static public void create()
{
    Database db = HostApplicationServices.WorkingDatabase;
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        Circle circle = new Circle(new Point3d(10, 10, 0), Vector3d.ZAxis, 2.0);
        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        btr.AppendEntity(circle);
        tr.AddNewlyCreatedDBObject(circle, true);
        tr.Commit();             
    }
}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Error Binding to Target Method
« Reply #2 on: May 05, 2007, 08:41:32 PM »

Change the method from a Function to a Sub because you are not returning anything to the calling routine.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Fatty

  • Guest
Re: Error Binding to Target Method
« Reply #3 on: May 06, 2007, 06:21:05 AM »
Hi Mike
You can call your function like this
It will return ObjectID of the circle wich
you could to use somewhere further in
the programm
(tested in A2007 only)
Hth

~'J'~

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Colors


Public Class MKWCommands

    ' Define command 'Asdkcmd1'
    <CommandMethod("Create")> _
    Public Sub TestCreate()
        Dim oID As ObjectId = Create()
        MsgBox("Object ID: " & oID.ToString)
    End Sub
    Public Function Create() As ObjectId
        Dim Circle As Circle
        Dim btr As BlockTableRecord
        Dim bt As BlockTable
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim oColor As New Color

        Dim trans As Transaction
        trans = db.TransactionManager.StartTransaction()
        Circle = New Circle(New Point3d(10, 10, 0), Vector3d.ZAxis, 2.0)
        bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
        Dim btrId As ObjectId = bt.Item(BlockTableRecord.ModelSpace)
        btr = trans.GetObject(btrId, OpenMode.ForWrite)

        btr.AppendEntity(Circle)
        trans.AddNewlyCreatedDBObject(Circle, True)
        Circle.Color = Color.FromColorIndex(ColorMethod.ByAci, 10)
        trans.Commit()
        trans.Dispose()
        Return Circle.ObjectId
    End Function

End Class

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Error Binding to Target Method
« Reply #4 on: May 06, 2007, 06:26:24 AM »
Fatty is correct, that is an option.
But for your own sanity don't use Class names as variable names.
.. even using oCircle or somesuch would make reading the code a little easier.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Fatty

  • Guest
Re: Error Binding to Target Method
« Reply #5 on: May 06, 2007, 06:34:55 AM »
Fatty is correct, that is an option.
But for your own sanity don't use Class names as variable names.
.. even using oCircle or somesuch would make reading the code a little easier.
Thanks Kerry
I agreed with you, it's a good point
Usualy I use it as you said but now I've
a got to hurry up with my answer:)
Sorry Mike

Regards,

Oleg

LE

  • Guest
Re: Error Binding to Target Method
« Reply #6 on: May 06, 2007, 10:59:44 AM »
Is interesting that the compiler does not catch the difference between the class and the variable names (in VB.NET the class name and var name are shown in the same color in C# they are shown with different color - using VS2005 PRO), even in C#, I did not knew about that (I just ran a test to verify, you learn something new every day) but is not a good practice as all have said. :)


Thanks!

TonyT

  • Guest
Re: Error Binding to Target Method
« Reply #7 on: May 06, 2007, 04:08:01 PM »
Fatty showed the problem indirectly, but perhaps he
didn't recognize it.

The error you're seeing is because the runtime is looking
for methods whose signatures have no return type (e.g.,
in C# that would be 'void', and in VB, it would be a Sub
rather than a Function).

So, just changing the method from 'Function Create' to
'Sub Create' should do it.

The followiing code produces this error:
Unhandled exception has occurred in a lcomponent in your application.  If you click continue, the application will ignore this error and attempt to continue.

Error binding to target method.

With the following:
************** Exception Text **************
System.ArgumentException: Error binding to target method.
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
   at AcMgCommandClass.InvokeWorker(AcMgCommandClass* , MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at AcMgCommandClass.InvokeWorkerWithExceptionFilter(AcMgCommandClass* , MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at AcMgPerDocumentCommandClass.Invoke(AcMgPerDocumentCommandClass* , gcroot<System::Reflection::MethodInfo ^>* mi, Boolean bLispFunction)
   at AcMgCommandClass.CommandThunk.Invoke(CommandThunk* )

This from VS2005 express.

Any suggestions?

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Colors


Public Class MKWCommands

  ' Define command 'Asdkcmd1'
  <CommandMethod("Create")> _
  Public Function Create()
    Dim Circle As Circle
    Dim btr As BlockTableRecord
    Dim bt As BlockTable
    Dim db As Database = HostApplicationServices.WorkingDatabase
    Dim oColor As New Color

    Dim trans As Transaction
    trans = db.TransactionManager.StartTransaction()

    Circle = New Circle(New Point3d(10, 10, 0), Vector3d.ZAxis, 2.0)


    bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
    Dim btrId As ObjectId = bt.Item(BlockTableRecord.ModelSpace)
    btr = trans.GetObject(btrId, OpenMode.ForWrite)

    btr.AppendEntity(Circle)
    trans.AddNewlyCreatedDBObject(Circle, True)
    trans.Commit()
    trans.Dispose()

  End Function

End Class

mkweaver

  • Bull Frog
  • Posts: 352
Re: Error Binding to Target Method
« Reply #8 on: May 07, 2007, 08:16:58 AM »
Thanks, all, for the comments.  I agree that using Circle for the variable is asking for trouble, if not from the compiler then at least from anyone having to read the code.

I worked this code up while going through Autodesk's arx lab 3.  I don't have it in front of me now, but I think I followed it fairly closely, including the var Circle and the function instead of a sub.  I wondered why a function since they were only returning a value on one of the logic routes.  I guess I should consider the examples a bit more critically.

Thanks again, for the help.

Mike

TonyT

  • Guest
Re: Error Binding to Target Method
« Reply #9 on: May 07, 2007, 10:27:02 AM »
Actually, using the names of types as parameters and variables
is fairly common in .NET and is done routinely. Unlike LISP, there
is nothing at riisk by doing it, although it can become a little
confusing. A type's identifer cannot be used directly in code as
a variable, or be mistaken for one (e.g., in c# you would always
use typeof( Typename ) to use the type itself).

Thanks, all, for the comments.  I agree that using Circle for the variable is asking for trouble, if not from the compiler then at least from anyone having to read the code.

I worked this code up while going through Autodesk's arx lab 3.  I don't have it in front of me now, but I think I followed it fairly closely, including the var Circle and the function instead of a sub.  I wondered why a function since they were only returning a value on one of the logic routes.  I guess I should consider the examples a bit more critically.

Thanks again, for the help.

Mike

mkweaver

  • Bull Frog
  • Posts: 352
Re: Error Binding to Target Method
« Reply #10 on: May 07, 2007, 01:28:35 PM »
Actually, using the names of types as parameters and variables
is fairly common in .NET and is done routinely. ...

I don't normally use type names for variables, and suspect I was simply following the example shown in the lab.  "...a little confusing."  is too much for me.  I need to avoid confusing as much as I can:-)

TonyT

  • Guest
Re: Error Binding to Target Method
« Reply #11 on: May 09, 2007, 01:48:22 PM »
I agree that it can be confusing.

It's actually public properties that often get
the same name as their type, rather than
variables.

Actually, using the names of types as parameters and variables
is fairly common in .NET and is done routinely. ...

I don't normally use type names for variables, and suspect I was simply following the example shown in the lab.  "...a little confusing."  is too much for me.  I need to avoid confusing as much as I can:-)