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
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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.