Yes, there a lot of things kind of strange with this forum... People posting questions and then posting an answer and marking their own post as the answer post. However, the points are browny points anyway and the value comes from the users not from the points.As far as getting the correct layer, yes the code you posted should work as long as you have the option strict off (which I don't recommend.) Option strict forces you to explicit cast (type conversion) to the specific type before you pass in an argument to a method. In your case, your method signature calls for an IFeatureLayer. Your method passes in an ILayer. That works fine as long as the ILayer happens to be a FeatureLayer. All FeatureLayers implement ILayer but lots of other types of layers implement ILayer such as grouplayer, rasterlayer, etc. and don't implement IFeatureLayer. Explicitly casting your ILayer to IFeatureLayer is a better practice.Dim fLayer as IfeatureLayer = ctype(pMap.Layer(0), IFeatureLayer) SelectMapFeaturesByAttributeQuery(pMap.ActiveView, flayer, "PID = '00000000'")
Now there is always the chance the first layer in the map is not a featurelayer, in which case the first line would failif typeof pMap.Layer(0) is IFeatureLayer then Dim fLayer as IfeatureLayer = ctype(pMap.Layer(0), IFeatureLayer) SelectMapFeaturesByAttributeQuery(pMap.ActiveView, flayer, "PID = '00000000'") End if
or better yet, trycast returns nothing if the cast failsDim fLayer as IfeatureLayer = trycast(pMap.Layer(0), IFeatureLayer) if fLayer isnot Nothing then SelectMapFeaturesByAttributeQuery(pMap.ActiveView, flayer, "PID = '00000000'") end if
You can write a for loop to go from 0 to pMap.LayerCount -1 and look at the layer of featureclass name. However, if you hit a group layer, it will be skipped and you will never look at the sub layers and also the code will crash if your map is empty (you can check the layercount before calling.) I prefer to use the IMap.Layers method.Dim featLayerUid As New UIDClass featLayerUid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}" 'IFeatureLayer Dim enumLayer As IEnumLayer = pMap.Layers(featLayerUid , True) 'True argument looks at sub layers in a group enumLayer.Reset() Dim layer As ILayer = enumLayer.Next Do While Not (layer Is Nothing) dim fLayer as IFeatureLayer = CType(layer, IFeatureLayer) 'Guaranteed to work since only querying IFeatureLayers. if ctype(fLayer.FeatureClass, IDataset).Name = "Name_I_am_looking_for" then SelectMapFeaturesByAttributeQuery(pMap.ActiveView, fLayer, "PID = '00000000'") exit do end if layer = enumLayer.Next() Loop