For Next Loop - Manual Might Be Wrong

M

Miro

Im using VB2003 and i just pulled an example out of the book that says this:

"Note: also that you can decalre the index variable in a For Loop using the
optional AS datatype clause, as long as the index variable isnt already
declared eslewhere. ( This is new to Visual Basic.net 2003 )

It then gives the example :

For intLoopIndex As Integer = 0 To 1
'This next line i have dummy'd down cause they use it to print an array
element so like Foo( intLoopIndex )
Console.WriteLine("Hello")
Next intLoopIndex

It does not mention having option strict on and option explicit on. I have
them both on.
My simple solution is to just dim it before, cause it complaines that
intLoopIndex is not declared.

Im assuming its either because of the Option Strict and Option Explicit
and not a typo ( somehow ) in the book.

Miro
 
G

Guest

this works fine for me...with Optin Strict on and off and Option Explicit on
and off

For x As Integer = 0 To 5

''Do Stuff

Next
 
G

GhostInAK

Hello Miro,

I've only tested this on VB6 and VB.Net on 2.0 framework.. Including the
indexer in your Next statement drasticly decreases performance in long-running
loops. You certainly wont notice a degredation on low to moderate interations
(say, less than 250,000).

-Boo
 
A

_AnonCoward

:
: Im using VB2003 and i just pulled an example out of the book that says
: this:
:
: "Note: also that you can decalre the index variable in a For Loop using
: the
: optional AS datatype clause, as long as the index variable isnt already
: declared eslewhere. ( This is new to Visual Basic.net 2003 )
:
: It then gives the example :
:
: For intLoopIndex As Integer = 0 To 1
: 'This next line i have dummy'd down cause they use it to print an array
: element so like Foo( intLoopIndex )
: Console.WriteLine("Hello")
: Next intLoopIndex
:
: It does not mention having option strict on and option explicit on. I
: have
: them both on.
: My simple solution is to just dim it before, cause it complaines that
: intLoopIndex is not declared.
:
: Im assuming its either because of the Option Strict and Option Explicit
: and not a typo ( somehow ) in the book.
:
: Miro


The example given is correct. The following compiles just fine in VB.net
2.0:

'----------------------------------------
Option Strict

Imports System

Public Module [module]
Public Sub Main()

Dim Arr(1) As String
Arr(0) = "Hello"
Arr(1) = "World"

For intLoopIndex As Integer = 0 To 1
Console.WriteLine(Arr(intLoopIndex))
Next intLoopIndex

End Sub
End Module
'----------------------------------------


Ralf
 
M

Miro

And by pasting my pic in the previous post, I have just realized I am using
2002.

I should RTFS

Sorry for the post.

Miro

Miro said:
Attached is a jpg with my mouse overs of the same code you have.

Maybe I have to download a version upgrade on my vb.net ?
Its an old version of 2003,
Its not a big deal, i just wanted to see if it really was possible.

I guess Ill just have to Dim :)

Miro


_AnonCoward said:
:
: Im using VB2003 and i just pulled an example out of the book that says
: this:
:
: "Note: also that you can decalre the index variable in a For Loop using
: the
: optional AS datatype clause, as long as the index variable isnt already
: declared eslewhere. ( This is new to Visual Basic.net 2003 )
:
: It then gives the example :
:
: For intLoopIndex As Integer = 0 To 1
: 'This next line i have dummy'd down cause they use it to print an array
: element so like Foo( intLoopIndex )
: Console.WriteLine("Hello")
: Next intLoopIndex
:
: It does not mention having option strict on and option explicit on. I
: have
: them both on.
: My simple solution is to just dim it before, cause it complaines that
: intLoopIndex is not declared.
:
: Im assuming its either because of the Option Strict and Option Explicit
: and not a typo ( somehow ) in the book.
:
: Miro


The example given is correct. The following compiles just fine in VB.net
2.0:

'----------------------------------------
Option Strict

Imports System

Public Module [module]
Public Sub Main()

Dim Arr(1) As String
Arr(0) = "Hello"
Arr(1) = "World"

For intLoopIndex As Integer = 0 To 1
Console.WriteLine(Arr(intLoopIndex))
Next intLoopIndex

End Sub
End Module
'----------------------------------------


Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
 
T

Tom Shelton

Miro said:
Im using VB2003 and i just pulled an example out of the book that says this:

"Note: also that you can decalre the index variable in a For Loop using the
optional AS datatype clause, as long as the index variable isnt already
declared eslewhere. ( This is new to Visual Basic.net 2003 )

It then gives the example :

For intLoopIndex As Integer = 0 To 1
'This next line i have dummy'd down cause they use it to print an array
element so like Foo( intLoopIndex )
Console.WriteLine("Hello")
Next intLoopIndex

VB.NET 2003
Option Strict On
Option Explicit On

Imports System

Module Module1

Sub Main()
For i As Integer = 0 To 10
Console.WriteLine(i)
Next
End Sub

End Module

Works just peachy... Now, it would be an error if you tried to access
i out side of the for loop.
 
H

Herfried K. Wagner [MVP]

Miro said:
Im using VB2003 and i just pulled an example out of the book that says
this:

"Note: also that you can decalre the index variable in a For Loop using
the optional AS datatype clause, as long as the index variable isnt
already declared eslewhere. ( This is new to Visual Basic.net 2003 )

It then gives the example :

For intLoopIndex As Integer = 0 To 1
'This next line i have dummy'd down cause they use it to print an array
element so like Foo( intLoopIndex )
Console.WriteLine("Hello")
Next intLoopIndex

It does not mention having option strict on and option explicit on. I
have them both on.
My simple solution is to just dim it before, cause it complaines that
intLoopIndex is not declared.

Are you sure you are using VS.NET/VB.NET 2003 or newer? The inline syntax
is not supported by previous versions of Visual Basic.
 
M

Miro

Geeze as soon as i Posted it I realized it.
I looked at the "about box" 3 times, and to think I didnt even notice it
even as I made the jpg.
But sure enough...once I hit send it hit me.

I was playing with VB.Express 2003 on another machine that came on a cd with
a different book,
and totally messed myself up.

Thanks for the posts everyone.
As suspected - Book is correct - and Im a Dumb Bum.

Miro
 
S

Smokey Grindel

VB Express is verison 2.0 aka 2005 not 2003 ;)

Miro said:
Geeze as soon as i Posted it I realized it.
I looked at the "about box" 3 times, and to think I didnt even notice it
even as I made the jpg.
But sure enough...once I hit send it hit me.

I was playing with VB.Express 2003 on another machine that came on a cd
with a different book,
and totally messed myself up.

Thanks for the posts everyone.
As suspected - Book is correct - and Im a Dumb Bum.

Miro
 
M

Miro

No I think it would be a 2003 version.

It came with a book called:

Microsoft Visual Basic.net Reloaded by Diane Zak,
It comes iwth a Product Key and on the cd it says 2003

( Personally I think the book isnt worth it.) but it is a 2003 version.

I bought 3 books and the one that has helped me out the most is
Sams Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days
-for a good reference.

Ps...it has been more than 21 days ;)

Miro.
 
G

GhostInAK

Hello Miro,

That may be, but Smokey is correct.. the "Express" versions of visual studio
are version 2005 and run against the 2.0 framework. There was no "Express"
version for 2003 / 1.x framework (unless you count notepad and the .NET SDK).

-Boo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top