2

I am trying to convert my VBA code to vb.net. I use "Select by location" to select all the points that intersect lines. Then I want to click a button to put the points into a selection set. Then I can use two other buttons to cycle through the selection set.

When I try to convert my code I keep on getting the following error in vb.net.:

In vb I keep on getting error for ‘pFeatureCursor’ saying "Variable ‘pFeatureCursor’ is passed by reference before it has been assigned a value. A null reference exception could result at runtime.

Here is the VBA code:

Public Sub GetSelSet()
 Dim pMxDoc As IMxDocument
 Dim pMap As IMap
 Dim pActiveView As IActiveView
 Dim pFeatureLayer As IFeatureLayer
 Dim pFeatureSelection As IFeatureSelection
 Dim pSelectionSet As ISelectionSet
 Dim pFeatureCursor As IFeatureCursor
 Dim pCurFeat As IFeature
 Dim intLayer As Integer
 Set pMxDoc = Application.Document
 Set pMap = pMxDoc.FocusMap
 Set pActiveView = pMap
 Set pSelSet = New esriSystem.Set
 CurrentIndex = 0
 For intLayer = 0 To pMap.LayerCount - 1
 If TypeOf pMap.Layer(intLayer) Is IFeatureLayer Then
 Set pFeatureLayer = pMap.Layer(intLayer)
 If Not pFeatureLayer.FeatureClass Is Nothing Then
 If pFeatureLayer.FeatureClass.featureCount(Nothing) > 0 Then
 Set pFeatureSelection = pFeatureLayer
 Set pSelectionSet = pFeatureSelection.SelectionSet
 pSelectionSet.Search Nothing, False, pFeatureCursor
 Set pCurFeat = pFeatureCursor.NextFeature
 
 Do While Not pCurFeat Is Nothing
 pSelSet.Add pCurFeat
 Set pCurFeat = pFeatureCursor.NextFeature
 Loop
 
 End If
 End If
 End If
 Next intLayer
End Sub
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 2, 2021 at 4:32
2
  • You say the error is occurring in your VB .Net code but you have uploaded your VBA code which I assume is the code you are trying to convert, may be you should show the code that is actually causing the error message to occur? Commented Feb 22, 2021 at 11:59
  • 1
    Also is this actually an error and not a WARNING given by visual studio? Commented Feb 22, 2021 at 12:01

1 Answer 1

0

This is only a warning and not an error, the compiler is making you aware that there's a trap you might hit.. however you've got quite a few lines of code to enumerate the feature selection from IMap.Layers which will not traverse group layers - that's a more serious concern, if a layer isn't valid (red exclamation mark indicating the data isn't found) your code will crash and burn!

Rather than walking down that stony path IMap implements FeatureSelection which does ignore invalid layers and find selection in group layers (this is VB.net code, you'll need to add SET keyword when assigning objects):

Dim pCnt As Long
Dim pSelection As IEnumFeature
Dim pSelSet As ISelectionSet
pSelSet = New esriSystem.Set
pMap = pDoc.FocusMap 'pMap is of type IMap and pDoc is IMxDocument
If pMap.SelectionCount = 0 Then Exit Sub
' pMap.FeatureSelection is of type ISelection wich is implemented by EnumFeature
pSelection = CType(pMap.FeatureSelection, IEnumFeature)
pMaxFeat = pMap.SelectionCount
For pCnt = 0 To pMaxFeat - 1
 pSelSet.Add pSelection.Next
Next

Will get you to the same goal in less lines with an added layer of safety; note that in the selection set features will derive from different feature classes, use IFeature.Class as IFeatureClass to locate the source for each feature. This code can easily be converted to use IEditor.EditSelection to find all the features that are selected and editable implemented as IEnumFeature.

I hope that helps.

answered Mar 4, 2021 at 3:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.