Index was outside the bounds of the array vb.net n mapwingis

别来无恙 提交于 2020-01-06 19:28:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!