问题
I am making a map app with VB.net and Mapwingis.
I want to save the map data with an array, but any error.
this code :
Dim scheme As New MapWinGIS.ColorScheme
Dim idx As Integer
idx = FormMain.TreeViewLayer.Nodes.IndexOf(FormMain.TreeViewLayer.SelectedNode)
sf = FormMain.AxMapMain.get_Shapefile(idx)
mainMapHandler = FormMain.AxMapMain.AddLayer(sf, True)
sf = FormMain.AxMapMain.get_Shapefile(mainMapHandler)
Dim fieldName As Integer
fieldName = CBSymVarNameUnique.SelectedIndex
sf.Categories.Generate(fieldName, MapWinGIS.tkClassificationType.ctUniqueValues, 0)
filcol = Convert.ToUInt32(RGB(255, 255, 255))
filcol2 = Convert.ToUInt32(RGB(0, 0, 255))
scheme.SetColors(filcol, filcol2)
Dim nama2() As String = New String() {}
For i As Integer = 0 To sf.Categories.Count - 1
Dim category As MapWinGIS.ShapefileCategory = sf.Categories.Item(i)
nama2(i) = category.Name.Substring(category.Name.IndexOf("=") + 1) 'error here
Next
Could you help me out here?
回答1:
In this line
Dim nama2() As String = New String() {}
you declare an array of string without any space to store strings. The Length of this array is zero.
Of course when you write
nama2(i) = category.Name.Substring(.....)
the code crashes because there is no space to store elements in the array
You need to declare the array with the appropriare space to store the strings that you plan to retrieve
Dim nama2(sf.Categories.Count-1) As String
However I suggest to use a List(Of String) and add the elements to this list as you need
Dim nama2 = New List(Of String)()
For i As Integer = 0 To sf.Categories.Count - 1
Dim category As MapWinGIS.ShapefileCategory = sf.Categories.Item(i)
nama2.Add(category.Name.Substring(category.Name.IndexOf("="))
Next
来源:https://stackoverflow.com/questions/31133178/index-was-outside-the-bounds-of-the-array-vb-net-n-mapwingis