RefactoringError in Visual Studio when adding a tuple in a standard for-next-loop
This issue has been moved from a ticket on Developer Community.
[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
