Working with in memory FeatureSet and Feature DotSpatial

Apr 28 at 12:37 PM
Edited Apr 28 at 9:35 PM
Hello All
I'm a beginner with DotSpatial and I'm stuck with a problem. I try to work with in memory feature (to keep them only in Map) until the user hit save button. The basic idea is that the user imports some dxf files and creates featureset based on layer name if the FeatureSet doesn't all ready exist, and for each polyline from dxf creates feature which will be added into a featureset.
public IFeature AddPoligons(EntityObject polyline, List<Text> textInDxf)
    IFeatureSet featureSet = null;
    //Getting the right featureset from map
    foreach (var x in appManager.Map.GetPolygonLayers())
        if (!string.Equals(x.DataSet.Name, layerName)) continue;

        featureSet = x.DataSet as FeatureSet;
    var polygon = CreatePolygon(polyline);

    //Creating feature and adding to FeatureSet
    var feature = featureSet.AddFeature(polygon);

    //Populating DataRow with information
    AddinfoIntoDataTable(feature, polygon, textInDxf);
    return feature;
Everything thing looks fine, the map is updated with new polygons, can select them, view information from datatable BUT, when I try to delete a selected feature (polygon) it throws an IndexOutOfRange exception.

What I'm doing wrong there?

Please help! Thank you and sorry for my poor English.

P.S. When I created the featureSet first I saved it on the disk and then loaded into map with Map.AddLayer() method.
Apr 28 at 9:58 PM

Meanwhile I found the solution somewhere on this forum, and was posted by Jany
The idea is to call this pice of code after each action against a FeatureSet (add, remove, move)
where featureLayer is IFeatureLayer.

There is one problem:

If we save the project with AppManager.Serialization.SaveProject() and then open the project AppManager.Serialization.OpenProject() the issue appear again.
As a solution for this I call my Save method after open the project and on short is look like this:
public void Save()
     foreach(var featureLayer in AppManager.Map.GetPolygonLayers())
Hope that helps someone.