Exception when trying to change Symbology for a layer

Jun 12, 2014 at 3:48 PM
Here's my piece of code:
' file is a .shp-file
Dim fs As FeatureSet = CType(FeatureSet.Open(file.FullName), FeatureSet)
Dim layer As New MapPolygonLayer(fs)
Dim scheme As New DotSpatial.Symbology.PolygonScheme()
Dim fillColor As Color
Dim outlineColor As Color

for each area in areaXML ' This will be looped 3 times
    fillColor = ColorTranslator.FromHtml("#" & area.ForegroundColor)
    outlineColor = ColorTranslator.FromHtml("#" & area.Color)

    Dim category As New DotSpatial.Symbology.PolygonCategory(fillColor, outlineColor, 1)
    category.FilterExpression = area.Filter.ToString()


layer.Symbology = scheme ' Exception will be thrown here! Message: Index was outside the bounds of the array.

   at DotSpatial.Symbology.FeatureLayer.AssignFastDrawnStates()
   at DotSpatial.Symbology.FeatureLayer.OnApplyScheme(IFeatureScheme scheme)
   at DotSpatial.Symbology.FeatureLayer.ApplyScheme(IFeatureScheme inScheme)
   at DotSpatial.Symbology.FeatureLayer.set_Symbology(IFeatureScheme value)
   at DotSpatial.Symbology.PolygonLayer.set_Symbology(IPolygonScheme value)
   at VitecDotSpatial.MainForm.ReadLayers() in C:\Src\Energy\NIS\VitecDotSpatial\VitecDotSpatial\MainForm.vb:line 1117
This part of the code works most of the time, but sometimes so does it throw an exception like above. And I don't understand why.
Jun 12, 2014 at 4:11 PM
I think it may depends somehow from FilterExpression. I think you should add some logging to find regularity.
Jun 16, 2014 at 12:42 PM
I read some documentation about .Symbology, and it pointed out that one should use .ApplyScheme() if one wants to set symbology.

So I changed this:
layer.Symbology = scheme
To this:
And it seems to have solved my problem.
Jun 17, 2014 at 9:24 AM
While the exception seems gone with the above change, the color category is all messed up. It gets different colors every time, even though I send in the exact same color code every time. It's like it doesn't care about my category.

Changing back to
layer.Symbology = scheme
will set the colors right again for the layers that doesn't get the exception.

Do I need to do something special to get
to work?
Jun 18, 2014 at 12:02 PM
You should not use ApplyScheme(scheme) instead "layer.Symbology = scheme" - this is incorrect way.
The correct way to set custom symbology:
layer.Symbology = scheme
Regarding to your issue. If this code works fine the most time, then the problem maybe in:
  1. Input file
  2. Filter expression
  3. In some other places of your application.