Issue Basemap & IEnumLayer

360
2
Jump to solution
12-05-2013 04:59 AM
AlexandraFairbarns
New Contributor III
Afternoon everyone -
The following works great...

  Dim uid As ESRI.ArcGIS.esriSystem.IUID = New ESRI.ArcGIS.esriSystem.UIDClass   uid.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}" 'IDataLayer    Try     Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = map.Layers((CType(uid, ESRI.ArcGIS.esriSystem.UID)), True) ' Explicit Cast     enumLayer.Reset()     Dim layer As ESRI.ArcGIS.Carto.ILayer = enumLayer.Next     Do While Not (layer Is Nothing)         layer = enumLayer.Next()     Loop   Catch ex As System.Exception     'System.Windows.Forms.MessageBox.Show("No layers of type: " + uid.Value.ToString);   End Try 


Until you add a basemap (e.g. light grey canvas map)
Even though its "Reference" layer is not a data layer IEnumLayer is continually picking up the layer - so when you do layer = enumLayer.Next() you get this wonderful error:

System.InvalidCastException was caught
  Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Carto.ILayer2'.
This operation failed because the QueryInterface call on the COM component for the interface with
IID '{599DEE2E-2182-4E54-8A86-725145C9D989}' failed due to the following error: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).


Has anyone else had similar difficulties?

Thank You
Alex
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
Are you certain that the error is occurring on that line of code?  The error says it is unable to cast the object to ILayer2.  Nowhere in the code you posted are you attempting a cast to ILayer2 (only ILayer) so I don't see any way for that exception to be thrown.

View solution in original post

0 Kudos
2 Replies
NeilClemmons
Regular Contributor III
Are you certain that the error is occurring on that line of code?  The error says it is unable to cast the object to ILayer2.  Nowhere in the code you posted are you attempting a cast to ILayer2 (only ILayer) so I don't see any way for that exception to be thrown.

View solution in original post

0 Kudos
AlexandraFairbarns
New Contributor III
Are you certain that the error is occurring on that line of code?  The error says it is unable to cast the object to ILayer2.  Nowhere in the code you posted are you attempting a cast to ILayer2 (only ILayer) so I don't see any way for that exception to be thrown.
Thanks Neil you inadvertently solved the issue. The code posted was from ArcGIS Online as I didn't have the code directly in front of me. The only difference in my code is that I implement ILayer2 since ESRI advises:
Provides access to members that work with all layers. Note: the ILayer interface has been superseded byILayer2. Please consider using the more recent version.
I changed code back to using ILayer - it works perfectly. Obviously ILayer2 has not been implemented for this particular layer.
0 Kudos