Solved! Go to Solution.
Solved by cadull_rb. Go to Solution.
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<Simonsson> wrote in messageI'm
href="news:6044220@discussion.autodesk.com">news:6044220@discussion.autodesk.com...
working with an Inventor addin (C#), but I have problems with the events. I
use the Inventor 2009 API Reference, but the syntax is in VB or something. For
instance:
Public Event OnFileNew( _
ByVal DocumentType As
DocumentTypeEnum, _
ByRef TemplateFileName As String, _
ByVal Context As
NameValueMap, _
ByRef HandlingCode As HandlingCodeEnum _
)
In C#
the "ByVal" seems to be default so I don't have to write anything there. But
"ByRef" can be both "ref" and "out" it seems. My problem is to translate this
syntax to C#. I managed to translate this OnFileNew event, but generally - how
do I know that "ByRef TemplateFileName As String" should be "ref string
TemplateFileName" and that "ByRef HandlingCode As HandlingCodeEnum" should be
"out HandlingCodeEnum HandlingCode"?
It seems just to be a matter of
syntax, so is there any way to autogenerate this code? I use Visual Studio
2005. First I do this:
fileUIEvents.OnFileNewDialog += new
FileUIEventsSink_OnFileNewDialogEventHandler(fileUIEvents_OnFileNewDialog);
And
here I get problems:
void fileUIEvents_OnFileInsertNewDialog(
[PROBLEMS] );
Error 1 No overload for 'fileUIEvents_OnFileNewDialog'
matches delegate
'Inventor.FileUIEventsSink_OnFileNewDialogEventHandler'
Brians links seems to be broken, and I can´t find any information on delegates in the help file or VBA objectbrowser.
It might be due to some esoteric self-explanatory convention within C# / COM, but for newcomers as myself a straight forward explanation on the delegate naming would be great and welcome.
Going through several samples and module 10 from the API virtual training videos, I asume it works like this:
Object.Event : XXEvents.OnXX(arguments)
Delegate : XXEventsSink_OnXXEventHandler(handler)
Handler : handler(arguments){ code }
Is this understood correctly and if yes, is it consistent throughout the API ?
In my experience the naming follows the convention you describe. In practice, you may not need the delegate name.
// C#
// keep reference to event manager
XXEvents events = ...
// subscribe
events.OnXX += handler
// unsubscribe
events.OnXX -= handler
From the documentation it seems we need to maintain a reference to the event manager XXEvents so that garbage collection does not remove the subscribed handlers.
The other catch to be aware of is it is not safe to unsubscribe during the handler execution. Doing so can cause the execution of other subscribed handlers to be skipped. I haven't tested whether this impacts all handlers of an event (e.g. Inventor and other add-ins) or if it is just limited to subscriptions from the same add-in.
// multiple subscriptions
events.OnXX += handler1
events.OnXX += handler2
// in hander1
events.OnXX -= handler1
// handler2 is not executed
Regards,
cadull
Thanks for the reply..
Your right :In practice I don´t need the names as VS pops them up. ( I was studying before executing )
& thanks for the additional information.. It will most likely save me days of confusion later on!
The GC had made me Headaches. Holding an explicite Reference to the Document.DocumentEvents Object fixed it!
Never had figured this out by myself 🙂
Can't find what you're looking for? Ask the community or share your knowledge.