question

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 asked nachoshaw-9496 answered

Declaring inside a loop

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

dotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 answered nachoshaw-9496 edited

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 answered

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




loop.gif (419.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 answered

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 :)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.