3

I am relatively new at creating tools with ArcObjects and I am having trouble finding a layer based on the layer's source name by looping through all layers. I've seen here that it is possible, but I am unable to get it to work. Am I missing something simple or is there another approach that I should be taking? I see here it is possible to enumerate layers and loop through them, but I am still left trying to determine the source name of a layer. My example below is looking for a layer whose source is a feature class named buildings. If it helps, the dataset variable has a value of "Nothing" when I use a MessageBox to print the value of dataset.Name at each iteration.

'--other code to get editor, set up other variables, and start edit operation
Dim findName As String = "buildings"
Dim isLayerFound As Boolean = False
Dim map as IMap = m_Editor.Map
Dim subfield as Integer = 'some subfield integer
'--Check For Point Layer In Editing
If Not map Is Nothing Then
'--Loop through all of the map layers to find the desired one
 For i = 0 To map.LayerCount - 1
 Dim currentLayer As ILayer = map.Layer(i)
 Dim dataset As IDataset = CType(currentLayer, IDataset) 
 If dataset.Name = findName Then
 isLayerFound = True
 '--Make sure layer is editable
 If m_editLayers.IsEditable(currentLayer) Then
 '--Set layer for editing
 m_editLayers.SetCurrentLayer(currentLayer, subfield)
 Exit For
 Else
 m_Editor.AbortOperation()
 MessageBox.Show("Layer " + findName + " is not editable. Operation aborted")
 Exit Sub
 End If
 End If
 Next i
 If isLayerFound = False Then
 m_Editor.AbortOperation()
 MessageBox.Show("Layer " + findName + " could not be found. Operation aborted")
 Exit Sub
 End If
End If
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 13, 2015 at 13:46

2 Answers 2

5

To find the name of the dataset as stored in the database use IDataset.BrowseName. IDataset.Name will give you the layer name as it is named in ArcMap.

You should also test that the layer can be cast to IDataset: If TypeOf currentLayer Is IDataset Then... Things like group layers don't implement IDataset, and will cause an error.

answered Jul 13, 2015 at 15:15
3
  • Great, this may be my problem. My layers are all grouped. Will my loop look into grouped layers? Commented Jul 13, 2015 at 15:20
  • 1
    No, for that you should use IEnumLayer. Here's an example of that: gis.stackexchange.com/questions/119610/… Commented Jul 13, 2015 at 15:33
  • That did the trick! Commented Jul 13, 2015 at 21:18
2

I just wanted to post my final working code:

'--Check For Point Layer In Editing
If Not map Is Nothing Then
 '--Loop through all of the map layers to find the desired one
 Dim enumLayer As IEnumLayer = map.Layers(Nothing, True)
 enumLayer.Reset()
 Dim currentLayer As ILayer = enumLayer.Next()
 Do Until currentLayer Is Nothing
 If TypeOf currentLayer Is IDataset And Not TypeOf currentLayer Is IRasterLayer Then
 Dim dataset As IDataset = currentLayer
 If dataset.BrowseName = findName Then
 isLayerFound = True
 '--Make sure layer is editable
 If m_editLayers.IsEditable(currentLayer) Then
 '--Set layer for editing
 m_editLayers.SetCurrentLayer(currentLayer, subfield)
 Exit Do
 Else
 m_Editor.AbortOperation()
 MessageBox.Show("Layer" + findName+ " is not editable. Operation aborted")
 Exit Sub
 End If
 End If
 End If
 currentLayer = enumLayer.Next()
 Loop
 If isLayerFound = False Then
 m_Editor.AbortOperation()
 MessageBox.Show("Layer " + findName + " could not be found. Operation aborted")
 Exit Sub
 End If
End If
answered Jul 15, 2015 at 15:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.