索引超出数组 vb.net n mapwingis [英] Index was outside the bounds of the array vb.net n mapwingis

查看:19
本文介绍了索引超出数组 vb.net n mapwingis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VB.netMapwingis 制作地图应用程序.我想用数组保存地图数据,但有任何错误.这段代码:

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

你能帮我一下吗?

推荐答案

在这一行

 Dim nama2() As String = New String() {}

你声明一个字符串数组,没有任何空间来存储字符串.此数组的 Length 为零.

you declare an array of string without any space to store strings. The Length of this array is zero.

当然当你写

 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 

但是我建议使用 List(Of 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

这篇关于索引超出数组 vb.net n mapwingis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆