dotnet / dotnet/vblang

“For each” skips elements when previous element is disposed

Open
#378 12 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

Not sure it is related to vblang.
Please consider the following

```
For each joy in MetroPanelSinottico.Controls
If TypeOf (joy) Is myJOYlib.myJOYlib Then
joy.dispose()
End If
Next
```

Disposing an element in a collection causes the collection to shrink by one element. Supposing a collection of n controls, and I remove the i-th element, the next control is the (i+1)th element of the (n-1)-elements collection, which is actually the (i+2)th element of the collection having n elements. The workaround is this:

```
For joy = MetroPanelSinottico.Controls.Count - 1 To 0 Step -1
If TypeOf (MetroPanelSinottico.Controls(joy)) Is myJOYlib.myJOYlib Then
MetroPanelSinottico.Controls(joy).Dispose()
End If
Next
```

Now: if I write "for each" I mean each of the elements in the collection, disregarding their actual index.
Is the present behaviour expected, or is this an unwanted side effect or am I asking too much?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.