ESRI.ArcGIS.Desktop.AddIns.Tool Casting Issue

Discussion created by jhoy-esristaff on Jan 26, 2011
Hey Everyone,

I am working with the following code:

if (_queryExtentCommand == null)
var uid = new UID {Value = ThisAddIn.IDs.QueryExtentCommand};

_queryExtentCommand = ArcMap.Application.Document.CommandBars.Find(uid, false, false);

if (_queryExtentCommand != null)
QueryExtentCommand qeCommand;

qeCommand = (QueryExtentCommand)_queryExtentCommand.Command;
catch (Exception)
qeCommand = null;

_queryEventChangedEventHandler = new QueryExtentCommand.QueryExtentChangedEventHandler(QueryExtentChanged);

if (qeCommand != null)
qeCommand.Changed += _queryEventChangedEventHandler;



The issue that I am getting is in the line that is in red. An exception is thrown at that line:

"Unable to cast COM object of type 'System.__ComObject' to class type 'ESRI.GMS.Lite.Commands.QueryExtentCommand'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."

However, the exception is catched and it allows me to execute the command so I can create an extent on the map. However, when it goes to return that extent through the "Changed" method it is still set to null therefore does nothing. I was hoping maybe someone has come across this before or might know how to get around this.

Any help would be greatly appreciated!!!!