question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VB -> C#: problems with pulling variables out of scope

See original GitHub issue

VB.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:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
GrahamTheCodercommented, Jul 3, 2022

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.

2reactions
GrahamTheCodercommented, Jul 3, 2022

I think I can see the problem - it has a null initializer. I’ll try to put in a quick fix now

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found