Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this infinite recursion? How does default variable initialization work in VB.NET?

I just made an interesting mistake in my code:

Dim endColumn As Integer = startColumn + endColumn - 1

The code was actually supposed to be:

Dim endColumn As Integer = startColumn + numColumns - 1

The interesting thing is, I would think that this code should be recursive and loop indefinitely, as the initialization of endColumn sort of calls itself. However, it seems that the code just treats the uninitialized variable as a 0 and so I get startColumn + 0 - 1. What is happening here behind the scenes? When does a variable get assigned a default value?

like image 420
froadie Avatar asked Nov 24 '25 15:11

froadie


1 Answers

The spec says, that:

All variables are initialized to the default value of their type before any variable initializers are executed.

like image 180
tanascius Avatar answered Nov 26 '25 20:11

tanascius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!