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,118 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 47,806 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. Michael Taylor 47,806 Reputation points
    2021-09-22T14:55:26.003+00:00

    Your original code is valid in VB.NET if that is what you're using. VB.NET isn't interpreted though so it would fail at compilation if it weren't valid. This isn't a variable declaration issue. Please post the exact error you're getting as it appears this is a runtime error.

    If you're not using VB.NET then please identify the language you are using.

    0 comments No comments

  2. NachitoMax 411 Reputation points
    2021-09-22T15:01:34.66+00:00

    Hi

    im using VB.Net (.Net 4.8). Im not getting the error in the callstack, its shown inline as

    error BC30451: 'val' is not declared. It may be inaccessible due to its protection level'

    This also works (putting the val declaration on a separate line inside the the code block)...

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

    Cant see why my original code block throws this error

    0 comments No comments

  3. Michael Taylor 47,806 Reputation points
    2021-09-22T15:14:07.4+00:00

    It seems like you are only showing us a portion of the code and not the entire block. Can you identify what exact line the compiler is complaining about as it doesn't seem like it is the assignment.

    0 comments No comments

  4. NachitoMax 411 Reputation points
    2021-09-22T16:45:25.833+00:00

    Hi

    There isnt much else in the block. Im looping through a grid and for each row, getting the row value in column 0 and creating a comma string like this

    ROW1, ROW2, ROW3, ROW4

    here is a view of the error in action
    134278-loop.gif

    in the image, the first iteration qualifies, every iteration after that throws an error. The full code is below

    Private Function CreateString(byval sender As Object) As String  
          
           Dim ret As String = String.Empty  
           Dim view As GridView = CType(sender, GridView)  
           For i As Integer = 0 To view.DataRowCount - 1  
               Dim val As String = view.GetRowCellDisplayText(i, view.Columns(0))  
               If Not val Is Nothing Then  
                  ret = ret & "," & val  
               End If  
           Next  
          
           ret = ret.SubString(1, ret.Length - 1)  
          
           Return ret  
          
        End Function  
      
    
    0 comments No comments