VB Express 2012: Strange problem with For Next

Dinosaur

Member
Joined
Oct 30, 2007
Messages
11
Programming Experience
10+
Perhaps I am missing something very subtle or have just not noticed something pertient. I have the following very simple code.
    Private Function DuplicateSearchResult(KeyedValue As Integer, BoxNumber As Integer) As DuplicateInformation

        Dim TboxNumber As Integer 'For-Loop counter
        Dim Result As DuplicateInformation

        Result.DuplicateFound = False
        Result.TboxNumberA = BoxNumber

        For tboxn = 0 To 80

        Next

    End Function

I intended to use TboxNumber as the For-Loop variable. In previous versions of VB.Net, typing the first few characters of
a Dim'ed variable resulted in the Editor typing the rest of the variable name.

For the project I am currently developing, tboxn did not result in TboxNumber when I keyed the equals sign in the For Statement.

In other parts of this program, it has worked.

Is there some Edit Option I have turned off (or on) which would have this effect?

The editor completes the name TboxNumber in other contexts: It is the For-Loop which has the problem.
 
Last edited by a moderator:
I can't really test this theory because I use ReSharper and it changes things like Intellisense but I think you'll find that the reason this happens is because it is possible, and in fact preferable, to NOT declare a loop counter variable outside the loop. The For statement itself can declare the loop counter variable. As you can see from your code, it is quite legal to use that 'tboxn' variable as your loop counter, even though you haven't declared it anywhere beforehand. The variable is actually declared on that line. If the code editor automatically used an existing variable if it found a match then it would be very annoying when you didn't want to use an existing variable, which should be about 99% of the time. The only reason to declare the loop counter outside the loop is if you intend to use it outside the loop. So, in your case, if you intend to use TBoxCounter only in the For loop then you should be doing just this:
For TBoxCounter = 0 To 80
rather than this:
Dim TBoxCounter As Integer

For TBoxCounter = 0 To 80
Also, unless you have a very good reason for doing otherwise, you should follow the almost universal convention of using a lower-case letter to start the names of variables.
 
J McIhinney: Thanx for your prompt reply.

It seems to me that you have answered my question.

BTW: I am almost certain that VB express 2008/2010 did not function this way.
 
BTW: I am almost certain that VB express 2008/2010 did not function this way.

That might actually be the case, but maybe they changed it because they got complaints from the 99% of developers who declare the loop counter in the loop 99% of the time.
 
Intellisense worked exactly the same way in 2010. Intellisense essentially evaluates what the best match is to what it is you are typing by looking at things like context, scope, and probability of meaning. The For-Next loop has it's own scope, and you enter it the moment you type For, and yes it does assume that the loop counter variable is NOT declared previously. TBH declaring it like so would be a very silly thing to do, since the content of the variable only has context within the scope of the loop itself anyways, outside of it it means absolutely nothing. Using the exit value of the loop counter later in code without knowing if the loop was exited with an exception, an Exit statement, or simply from running through the range is very sloppy and only leads to badly written bordering undecypherable spaghetti code.
 
Back
Top