Declaring inside a loop

NachitoMax 411 Reputation points
2021-09-22T14:19:47.347+00:00

Hi

I have done this 100's of times but after recently moving to 2019 im getting this issue...

In this code block, i am declaring 'val' as a string inside the loop

For i As Integer = 0 To GridView1.DataRowCount - 1
    Dim val As String = CType(view.GetRowCellDisplayText(i, view.Columns(0)), String)
Next

after the first iteration, it errors with an overload saying 'val is not declared'

if i change it to this

Dim val As String = String.Empty
For i As Integer = 0 To GridView1.DataRowCount - 1
    val = CType(view.GetRowCellDisplayText(i, view.Columns(0)), String)
Next

it works but leaves val exposed to an incorrect value if there was an error. I prefer to declare inline but for some reason, its throwing the error. Is it a 2019 setting?

Thanks

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 48,581 Reputation points
    2021-09-22T16:59:14.057+00:00

    Hold up, you're seeing this at runtime. We're back to this isn't a compiler error so your code is fine. The problem here is with the debugger evaluating symbols at runtime, not a coding issue. This is actually pretty common and can depend on how the value is set and/or what else is going on.

    In your case though I believe the debugger is correct. You are mousing over the variable at the point it is being declared. The symbol technically doesn't exist yet so the debugger is telling you it doesn't have a value, which is correct. It isn't until you step past the declaration and into the initializer expression that the variable exists (ignoring right associativity of assignment statements). I bet if you set your breakpoint on the next line, in the actual code that is line if Not Val, then you'll see the correct value in the debugger. Do you?

    As I mentioned the issue is that at the point you mouse over the cursor is sitting on the declaration but hasn't executed it yet because the breakpoint is on the declaration. If you were to step once then, depending upon what the right expression entails, it'll execute the declaration and step over to the expression and you'll see the value correctly. But personally I would just step over the assignment and confirm on the next line.

    So, in summary, your code is compiling and executing correctly. You are just looking at the variable's value too soon as it doesn't exist yet in the debug context.

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. NachitoMax 411 Reputation points
    2021-09-22T17:09:12.697+00:00

    Hi

    Yes thats correct. I get what you're saying, never seen it show that before but then again, 2019 has given some addition things that are not set in 2017. I had a recent Dispose issue that worked ok in 2017 but threw an error in 2019.

    by moving the stop to after the initializer, the error doesnt exist

    Thanks for the clarification :)

    0 comments No comments