jgarcinuno

Enabled a BaseCommand using C# Depending on whether we have a layer with certain name

Discussion created by jgarcinuno on Oct 13, 2010
This was my VB6 Old Code, so I need to convert this logic into C#
Private Property Get ICommand_Enabled() As Boolean
  ' This property determines if the command is enabled based on what if there is a Layer
  ' in TOC called sites, otherwise the command will be disabled. For now the command will
  ' enabled for testing purposes
    Dim pMxdoc As IMxDocument
    Dim pActiview As IActiveView
    Dim pMap As IMap
    Dim pFeatLayer As IFeatureLayer2
    Dim i As Integer
    Set pMxdoc = m_pApp.Document
    Set pMap = pMxdoc.FocusMap
    If pMap.LayerCount = 0 Then
        Exit Property
    Else
    For i = 0 To pMap.LayerCount - 1
        If pMap.Layer(i).Name = "sites1" Then
            Set pFeatLayer = pMap.Layer(i)
            If pFeatLayer Is Nothing Then
                ICommand_Enabled = False
                Exit Property
            Else
                ICommand_Enabled = True
            End If
        End If
    Next i
    End If
End Property

So far I have this code written in C#, but I need to add a for clause to look into my TOC and it is not working at all.

Private Override bool Enabled
get
            {
                IMxDocument pMxDoc = pApp.Document as IMxDocument;
                IMap pMap = pMxDoc.FocusMap;
                            
               
                //===============================================
                if (pMap.LayerCount > 0)
                //for (int i = 0; i < pMap.LayerCount; i++)
                //                {
                //                    if (pMap.get_Layer(i).Name == "sites")
                //                    {
                //                        return true;
                //                    }
                //                    else
                //                    {
                //                        return false;
                //                    }
                //                }
                    return false;
                else
                    return true;
                //===============================================

             
            }

Any example it will be very welcome.

Outcomes