I have created a toolbar add in for arc with a button that opens a form with 3 selections that are shapefiles, once the shapefile is selected I click the Add Layer button. Currently I can only get one shapefile at a time, unless I duplicate the code for each shp and once I do add the one shapefile it keeps on adding it. Here's the code I have so far:

Public Function AddLayers()
        Dim pMxDoc As IMxDocument
        Dim pMap As IMap
        Dim pWorkspaceFactory As IWorkspaceFactory
        Dim pFeatureWorkspace As IFeatureWorkspace
        Dim pFeatureLayer As IFeatureLayer
        Dim pFeatureClass As IFeatureClass
        ' Specify the workspace and the feature class.
        pWorkspaceFactory = New ShapefileWorkspaceFactory
        pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("E:\Customization\Data\Cities", 0)
        pFeatureClass = pFeatureWorkspace.OpenFeatureClass("Cities")
        ' Prepare a feature layer.
        pFeatureLayer = New FeatureLayer
        pFeatureLayer.FeatureClass = pFeatureClass
        pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName
        ' Add the feature layer to the active map.
        pMxDoc = My.ArcMap.Application.Document
        pMap = pMxDoc.FocusMap
        ' Refresh the active view.
        Return AddLayers()
End Function
  • 2
    I am a little rusty with ArcObjects...but can you not just add an argument to your function so you can pass the layer name instead of hardcoding "Cities". Then call the function based on the number of selections you have and the layer name. Does this have to be a function? Can you just change this to a Procedure...why return AddLayers()?
    – Adam
    Commented Feb 12, 2014 at 17:00

1 Answer 1


First of all you have declared a Function yet you return no result. You should be declaring this a Sub

Your very last line is Return AddLayers() I believe this is the problem. You are returning a call to the function itself so I suspect it is entering the function which then enters itself ad infinitum.

So turn this into a Sub and remove the last line of code.

Your Answer

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

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