zyf1097727546
2017-08-11 04:53
采纳率: 50%
浏览 2.2k

索引超出范围。必须为非负值并小于集合大小。这是怎么回事啊

public static ISpatialReference GetSpatialReference(IFeatureDataset pFeatureDataset)
{
IGeoDataset pGeoDataset = pFeatureDataset as IGeoDataset;
ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
return pSpatialReference;
}
public static List GetAllFeatureClass(IWorkspace workspace)
{
IEnumDataset dataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
IFeatureDataset featureDataset = dataset as IFeatureDataset;

             List<IFeatureDataset> featureDatasetList = new List<IFeatureDataset>();
             while (featureDataset != null)
             {
                 featureDatasetList.Add(featureDataset);
                 featureDataset = dataset.Next() as IFeatureDataset;
             }
             return featureDatasetList;
         }
         public static IWorkspace GetShapefileWorkspace(string sFilePath)
         {
             try
             {
                 IWorkspaceFactory factory = new ShapefileWorkspaceFactoryClass();
                 sFilePath = System.IO.Path.GetDirectoryName(sFilePath);
                 return factory.OpenFromFile(sFilePath, 0);
             }
             catch
             {
                 return null;
             }
         }
    private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
    {

        IWorkspace workspace = GetShapefileWorkspace(@"C:\Users\10977\Desktop\AE\培训数据\空间分析\1为新学校选址\新学校选址.mxd");
        List<IFeatureDataset> lfd = GetAllFeatureClass(workspace);

        ISpatialReference isr = GetSpatialReference(lfd[0]);
        string st=isr.Name.ToString();
        Form2 fr =new Form2();
        fr.Show();
    }
            ISpatialReference isr = GetSpatialReference(lfd[0]);这句话提示索引超出范围。必须为非负值并小于集合大小。求解啊
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 乱臣贼子 2017-08-11 05:26
    已采纳

    List lfd = GetAllFeatureClass(workspace);
    ISpatialReference isr = GetSpatialReference(lfd[0]);

    你看你写的代码,你要知道lfd是个引用类型,引用类型就可能会为null,那么你是不是需要判断lfd是否为null呢,再者,lfd即使不为null,但是可能这个集合里面没有元素呢,也就是长度为0 呢? 你说的这个错误就是长度为0 的时候,你去取下标为0的元素就会报错的。

    正确写法:
    using System.Linq;

    List lfd = GetAllFeatureClass(workspace);
    if(null != lfd && lfd.Any())
    {
    ISpatialReference isr = GetSpatialReference(lfd[0]);
    }

    已采纳该答案
    打赏 评论
  • 鼠小 2017-08-11 05:16

    GetAllFeatureClass(workspace);,,,可能是你这里没获取到 list ,,也就是 lfd 长度为 0

    打赏 评论
  • Tsui丶 2017-08-11 05:19

    示索引超出范围 即你的lfd并没有值(长度为0),所以导致lfd[0]提示索引超出范围

    打赏 评论
  • das白 2017-08-11 06:32

    楼上正解 里面是null 建议调试 看哪有问题

    打赏 评论

相关推荐 更多相似问题