FeatureClass.Search(IQuery Filter,bool Recycling)参数说明
2011-07-19 8:32
IFeatureClass的Search方法大家经常用到,很多人对Search方法的其中一个参数bool Recycling不是很理解。目前网上关于这个参数的意义的解释有两个版本:1.第一版本是Isnotnull大牛的:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=13461他提出的观点的是:参数Recycling为True的时候是传引用,为False的时候为传值2.第二个版本是Echo兄的:http://gis.cnblogs.com/home.aspx?page=3他提出的观点是:参数Recycling为True的时候游标对象(Ifeaturecursor)是只包含一条记录,为False的时候游标对象(Ifeaturecursor)为10条记录(假设Featureclass有10条记录)我经过测试提出个人的观点:1.pFeatureCursor存储的是所有符合条件的Feature对象的引用,2.Recycling的意思是回收,参数Recycling为True的时当执行这个方法IFeature pFeature=pFeatureCursor.NextFeature()上一条记录的值在内存中所占的地址就会被销毁回收,为False的时候当执行这个方法IFeature pFeature=pFeatureCursor.NextFeature()上一条记录的值依然存在在内存中。Ok,接下来以实例证明本观点:1.参数Recycling设置为True IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass; IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true); //IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false); List<IFeature> pFeatures = new List<IFeature>(); IFeature pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) { pFeatures.Add(pFeature); pFeature = pFeatureCursor.NextFeature(); } for (int i = 0; i < pFeatures.Count; i++) { MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString()); }分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为TRUE也就是说没执行一个NextFeautre方法上一条记录的Feature值在内存中被回收,所以到最后遍历pFeatures集合的时候所有的IFeature引用指向的Feature对象都为Null。所以会引发一下错误(如下图所示):
2.参数Recycling设置为False IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass; //IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true); IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false); List<IFeature> pFeatures = new List<IFeature>(); IFeature pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) { pFeatures.Add(pFeature); pFeature = pFeatureCursor.NextFeature(); } for (int i = 0; i < pFeatures.Count; i++) { MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString()); }分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为False也就是说每执行一个NextFeautre方法上一条记录的Feature值在内存中依然存在,所以到最后遍历pFeatures集合的时候所有的 IFeature引用指向的Feature对象都依然存在。所以会执行的很Happy(如下图所示): |