anikameyer

My.ArcMap.Document.SelectedLayer Is Nothing - Error: VB.Net

Discussion created by anikameyer on Mar 28, 2012
Latest reply on Nov 9, 2012 by alexander1984
Dear all:
The following syntax shows an error when trying to get to the line: "If myLayer Is Nothing Then ..."
I am trying to get to the Msgbox and Exit Sub when the user hasn't selected a layer before but this part of the code doesn't work. Have you any idea?
Thank you very much,
Anika

Here the code:

Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Try
            'Attributnamen der Variable FCName von der Textbox des Formulares zuweisen
            Dim FCName As String
            FCName = txtFieldName.Text

            'Felddatentyp für das neue Feld von der ausgewählten Radiobox übernehmen
            Dim myFieldType As esriFieldType

            If optDouble.Checked = True Then
                myFieldType = esriFieldType.esriFieldTypeDouble
            ElseIf optDate.Checked = True Then
                myFieldType = esriFieldType.esriFieldTypeDate
            Else
                myFieldType = esriFieldType.esriFieldTypeString
            End If

            Dim theDocument As IMxDocument
            Dim myLayer As ILayer
            Dim myFeatureClass As IFeatureClass
            Dim fieldEdit As IFieldEdit

            theDocument = My.ArcMap.Document
            ' Set myLayer to current selected Layer
            myLayer = theDocument.SelectedLayer

            If myLayer Is Nothing Then
                MsgBox("Select a layer you want to add a field to!", "Warning")
                Exit Sub
            End If



            ' Get Feature Class information from selected Layer
            myFeatureClass = ReturnFCFromLayer(myLayer)
            ' Set fieldEdit to New Field
            fieldEdit = New Field
            'Set Name of column to add
            fieldEdit.Name_2 = FCName
            'Set Type of column to add
            fieldEdit.Type_2 = myFieldType

            ' Add the field
            myFeatureClass.AddField(fieldEdit)
            MsgBox(String.Format("Successfully added Column {0} to selected layer", FCName))
        Catch ex As Exception
            MsgBox(ex.ToString(), vbExclamation, "Error")
        End Try


    End Sub

    Public Function ReturnFCFromLayer(ByVal pLayer As ILayer) As IFeatureClass
        Dim pFlayer As IFeatureLayer = pLayer
        Return pFlayer.FeatureClass
    End Function

Outcomes