RefactoringError in Visual Studio when adding a tuple in a standard for-next-loop
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/RefactoringError-in-Visual-Studio-when-a/11013273)._
---
\[severity:It bothers me. A fix would be nice\]
_Environment_
I have a WinForms-.NET-FX 4.8.1 VB. NET-project in Visual Studio Version 18.0.2 with November 2025 Featureupdate on a 64-bit Win11 PC, but it also happens in a WinForms-.NET 10.0 VB. NET-project and using Option Strict On. I am using Option Strict On and the german language pack, because some problems seem to be language specific.
**Problem description**
I want to add a tuple to a list inside of a classic for loop.
```
Private Sub Test()
Dim Foo As New List(Of String) From {"Bar", "Baz"}
Dim ValuePacksAsTuples As New List(Of (Index As Integer, Value As Integer))
For i = 0 To Foo.Count - 1
ValuePacksAsTuples.Add((i, 1))
Next
End Sub
```
If I select the code line with the `For i = 0`, an error appears, mentioning the `VisualBasicConvertForToForEachCodeRefactoringProvider`
**stack trace**
```
System.InvalidCastException : Das Objekt des Typs "Microsoft.CodeAnalysis.VisualBasic.Syntax.TupleExpressionSyntax" kann nicht in Typ "Microsoft.CodeAnalysis.VisualBasic.Syntax.ArgumentListSyntax" umgewandelt werden.
at Microsoft.CodeAnalysis.VisualBasic.LanguageService.VisualBasicSyntaxFacts.GetArgumentsOfArgumentList(SyntaxNode node)
at Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.<>c__DisplayClass5_0. <ComputeRefactoringsAsync>g__IterationVariableIsUsedForMoreThanCollectionIndex|1(SyntaxNode current)
at Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.<>c__DisplayClass5_0. <ComputeRefactoringsAsync>g__IterationVariableIsUsedForMoreThanCollectionIndex|1(SyntaxNode current)
at Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.<>c__DisplayClass5_0. <ComputeRefactoringsAsync>g__IterationVariableIsUsedForMoreThanCollectionIndex|1(SyntaxNode current)
at Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.<>c__DisplayClass5_0. <ComputeRefactoringsAsync>g__IterationVariableIsUsedForMoreThanCollectionIndex|1(SyntaxNode current)
at Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.<>c__DisplayClass5_0. <ComputeRefactoringsAsync>g__IterationVariableIsUsedForMoreThanCollectionIndex|1(SyntaxNode current)
at Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.<>c__DisplayClass5_0. <ComputeRefactoringsAsync>g__IterationVariableIsUsedForMoreThanCollectionIndex|1(SyntaxNode current)
at Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.<>c__DisplayClass5_0. <ComputeRefactoringsAsync>g__IterationVariableIsUsedForMoreThanCollectionIndex|1(SyntaxNode current)
at async Microsoft.CodeAnalysis.ConvertForToForEach.AbstractConvertForToForEachCodeRefactoringProvider`6.ComputeRefactoringsAsync[TStatementSyntax,TForStatementSyntax,TExpressionSyntax,TMemberAccessExpressionSyntax,TTypeNode,TVariableDeclaratorSyntax](<Unbekannte Parameter>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeRefactorings.CodeRefactoringService.<>c__DisplayClass12_0. <GetRefactoringFromProviderAsync>b__0(<Unbekannte Parameter>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unbekannte Parameter>)
```
**Video**

Contributor guide
Research direction
Start with the VisualBasicConvertForToForEachCodeRefactoringProvider named in the stack trace and reproduce the issue using the supplied VB.NET tuple and classic For loop. Trace the tuple expression through the refactoring analysis; done means selecting the loop no longer throws an InvalidCastException and the refactoring remains usable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- visualbasic
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100