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
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)).
Solved! Go to Solution.
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.