System.InvalidCastException when using PublisherControls.ARLayer

930
3
02-27-2013 08:03 AM
KennMurphy
New Contributor
I'm writing an add-in for ArcMap 10.0 using VB.Net and I'm running into an Invalid Cast Exception.

The error message is:
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.PublisherControls.ARLayer'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{33DF9650-35B7-42F8-A7C9-9F8A1B53AA80}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).


The line that I believe is generating the error is:
Dim SearchLayer as ESRI.ArcGIS.PublisherControls.ARLayer = My.ArcMap.Document.FocusMap.Layer(LayerNum)


I'm fairly certain that LayerNum is accurate and this code works in a stand-alone application that performs the same function, so I'm not sure what I'm doing incorrectly. The next thing I was going to try was something like:
Dim SearchLayer As ESRI.ArcGIS.PublisherControls.ARLayer
SearchLayer = CType(My.ArcMap.Document.FocusMap.Layer(LayerNum), ESRI.ArcGIS.PublisherControls.ARLayer)


But I wanted to see if I could get some opinions and ideas before I tried it.

Thanks!
0 Kudos
3 Replies
RichardWatson
Frequent Contributor
FocusMap.Layer returns ILayer.  I'd get the result as that type and then log the Name and other details before trying to cast it to the type of layer that you believe it should be.  That should help you debug the issue.

My assumption here is that all of this is occurring on the main thread of execution.  Please state if this assumption is incorrect.
0 Kudos
KennMurphy
New Contributor
[Edit]: I am making the changes you suggested, but we only have one mapping person and I will need to wait until he is available to test.

You are correct; I'm not doing any threading with this add-in (that's not a bad idea, but I'll have to leave it for a later date).

What we're trying to do is create a search function that will search multiple layers and fields in those layers for user-entered text. I'm not as familiar with ArcObjects as I'd like to be (so I'm likely not doing this in the easiest of ways). I was using the ARLayer.QueryARFeatures method to search for text in the layer fields. I was doing it this way because I found some similar code on the forums and adapted it for our immediate needs. If there's an easier or better way to do this, please let me know.

Thanks!
0 Kudos
KennMurphy
New Contributor
I'm sorry it took so long to respond, but that didn't help. I've decided to try a different tack and will be trying to use the FindFeatures CoClass. Thanks for the help.
0 Kudos