VB -> C#: problems with pulling variables out of scope
See original GitHub issueVB.Net input code
Private Shared Sub ProblemsWithPullingVariablesOut()
' example 1
For Each a In New List(Of String)
Dim b As Long
If a = "" Then
b = 1
End If
DoSomeImportantStuff()
Next
' example 2
Dim c As String
Do While True
Dim d As Long
If c = "" Then
d = 1
End If
DoSomeImportantStuff()
Exit Do
Loop
End Sub
Private Shared Sub DoSomeImportantStuff()
Debug.Print("very important")
End Sub
Erroneous output
// example 1
private static void ProblemsWithPullingVariablesOut()
{
;
// example 2
var c = default(string);
;
}
private static void DoSomeImportantStuff()
{
Debug.Print("very important");
}
Details
- Product in use: VS extension
- Version in use: 9.0.1.0
- Did you see it working in a previous version, which? yes (8.5)
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
What happens when a variable goes out of scope?
They simply go out of scope, and for builtin types the code won't do anything - and for objects, the destructor is called....
Read more >In C#, why are variables declared inside a try block limited ...
As I've said, you can define them before the try blocks in which they are assigned so they will remain in scope afterwards,...
Read more >Local variables: scope vs. lifetime (plus closures)
However, those of you who tried it out discovered that in VB the lifetime of a local variable does not equal its scope....
Read more >VB -> C#: Nested scopes should have variables renamed ...
In VB you can shadow the name of a variable from an outer scope. Within C# you can't do this for variables in...
Read more >How to: Control the Scope of a Variable - Visual Basic ...
In some cases, the variable's access level can influence its scope. For more information, see Scope in Visual Basic.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I like the general idea of not pulling out variables when it isn’t needed. But the mechanism - making decisions based on the C# syntax tree - is usually a bad idea. You can never know exactly why it ended up that way. It may well be due to sensible flow analysis, or it could just be a style preference. I don’t know in this case if any analysis takes into account this possibility properly. I’d rather re-do the analysis (even calling the same method) at the point we expect something particular of it for clarity if needed and not in the same method.
I think I can see the problem - it has a null initializer. I’ll try to put in a quick fix now