VB.net processes Variable Allocation and initialization in a loop

Source: Internet
Author: User
The following two paragraphs Code What output will each have:
1.
For I as integer = 0 to 3
Dim J as integer = 10
Debug. writeline (j)
J + = 1
Next
2.
For I as integer = 0 to 3
Dim J as integer
Debug. writeline (j)
J + = 1
Next

The answer is:
1.
10
10
10
10
2.
0
1
2
3
Through the above example, we can guess that the way VB.net processes the variable in the loop is: one allocation. If there is an initialization value, the initialization value will be assigned to each loop, otherwise, the initial value is (memory) 0 at the first allocation.
This also shows that the variable in the loop is not equivalent to the variable in the same defined function range.
Therefore, if you want to avoid the effect of keeping changes like code 2, you should specify the reason why the variable should be initialized at the beginning of the loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.