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.