Home All Groups Group Topic Archive Search About

Not as 'explicit' as I thought --- What am I missing

Author
11 Jun 2009 3:05 PM
Dale Atkin
I've been a big fan of explicit variable declaration ever since many years
ago when I spent nearly a whole day trying to track down a bug, that was the
result of a simple 'typo'. Ever since, I've made sure "Option Explicit" was
on in every program I write, and I've never looked back.

I've noticed however that in certain circumstances, the compiler doesn't
seem to mind inferring the data type of some variables.

For example, I would have expected that the following code would generate an
error:

    Private Function DoSomething() As String
        For i = 0 To 10000

        Next
        Return 0
    End Function

But it doesn't.  It seems that the For loop is somehow an exception to the
'explicit' variable declaration rule, and I'm not sure why, or what other
situations this same 'exception' might apply in, and its making me a bit
nervous.

Dale

Author
11 Jun 2009 3:26 PM
sloan
Have you tried

Option Strict On

http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.80).aspx


Show quoteHide quote
"Dale Atkin" <labrad***@ibycus.com> wrote in message
news:%23SC3LYq6JHA.1424@TK2MSFTNGP02.phx.gbl...
> I've been a big fan of explicit variable declaration ever since many years
> ago when I spent nearly a whole day trying to track down a bug, that was
> the result of a simple 'typo'. Ever since, I've made sure "Option
> Explicit" was on in every program I write, and I've never looked back.
>
> I've noticed however that in certain circumstances, the compiler doesn't
> seem to mind inferring the data type of some variables.
>
> For example, I would have expected that the following code would generate
> an error:
>
>    Private Function DoSomething() As String
>        For i = 0 To 10000
>
>        Next
>        Return 0
>    End Function
>
> But it doesn't.  It seems that the For loop is somehow an exception to the
> 'explicit' variable declaration rule, and I'm not sure why, or what other
> situations this same 'exception' might apply in, and its making me a bit
> nervous.
>
> Dale
Author
11 Jun 2009 3:32 PM
Dale Atkin
"sloan" <sl***@ipass.net> wrote in message
news:eAlmxjq6JHA.1568@TK2MSFTNGP06.phx.gbl...
> Have you tried
>
> Option Strict On
>
> http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.80).aspx

yep. Same result (mostly)
Author
11 Jun 2009 4:06 PM
Armin Zingler
Dale Atkin wrote:
Show quoteHide quote
> I've been a big fan of explicit variable declaration ever since many
> years ago when I spent nearly a whole day trying to track down a bug,
> that was the result of a simple 'typo'. Ever since, I've made sure
> "Option Explicit" was on in every program I write, and I've never
> looked back.
> I've noticed however that in certain circumstances, the compiler
> doesn't seem to mind inferring the data type of some variables.
>
> For example, I would have expected that the following code would
> generate an error:
>
>    Private Function DoSomething() As String
>        For i = 0 To 10000
>
>        Next
>        Return 0
>    End Function
>
> But it doesn't.  It seems that the For loop is somehow an exception
> to the 'explicit' variable declaration rule, and I'm not sure why, or
> what other situations this same 'exception' might apply in, and its
> making me a bit nervous.


1. With a for loop you can write:

    for i as integer = 0 to 10000

This has nothing to do with Option Explicit, it's just a simplified way to
declare the loop variable at block scope.

2. With Option Infer On, you can write

    dim i = 17

which is equal to

    dim i as integer = 17


3. If you put #1 and #2 together you can write

    For i = 0 To 10000

which explicitly declares variable i. Therefore it's not a violation of the
Option Strict rule. It's just it's type that is inferred from the start and
end values.



Armin
Author
11 Jun 2009 5:06 PM
Dale Atkin
Show quote Hide quote
>
> 1. With a for loop you can write:
>
>    for i as integer = 0 to 10000
>
> This has nothing to do with Option Explicit, it's just a simplified way to
> declare the loop variable at block scope.
>
> 2. With Option Infer On, you can write
>
>    dim i = 17
>
> which is equal to
>
>    dim i as integer = 17
>
>
> 3. If you put #1 and #2 together you can write
>
>    For i = 0 To 10000
>
> which explicitly declares variable i. Therefore it's not a violation of
> the Option Strict rule. It's just it's type that is inferred from the
> start and end values.

That makes a lot of sense actually. Thanks for the detailed explanation. I'd
noticed some of the 'option infer' stuff, but didn't tie it together with
how this would play out within a for loop.

I think I'll probably turn off Option Infer, and see if I like/dislike this
feature. If it starts to bug me, I'll turn it back on.

Dale
Author
11 Jun 2009 9:20 PM
Lloyd Sheen
Show quote Hide quote
"Dale Atkin" <labrad***@ibycus.com> wrote in message
news:uYsS4br6JHA.5932@TK2MSFTNGP03.phx.gbl...
> >
>> 1. With a for loop you can write:
>>
>>    for i as integer = 0 to 10000
>>
>> This has nothing to do with Option Explicit, it's just a simplified way
>> to declare the loop variable at block scope.
>>
>> 2. With Option Infer On, you can write
>>
>>    dim i = 17
>>
>> which is equal to
>>
>>    dim i as integer = 17
>>
>>
>> 3. If you put #1 and #2 together you can write
>>
>>    For i = 0 To 10000
>>
>> which explicitly declares variable i. Therefore it's not a violation of
>> the Option Strict rule. It's just it's type that is inferred from the
>> start and end values.
>
> That makes a lot of sense actually. Thanks for the detailed explanation.
> I'd noticed some of the 'option infer' stuff, but didn't tie it together
> with how this would play out within a for loop.
>
> I think I'll probably turn off Option Infer, and see if I like/dislike
> this feature. If it starts to bug me, I'll turn it back on.
>
> Dale

Option Infer is used quite a bit with Linq.  I create classes for DB access
using Linq and those classes have Infer On, all other classes have Infer
Off.  You can set that on a project basis and just set to On for those
classes that need it.

LS
Author
11 Jun 2009 4:36 PM
Tom Shelton
On 2009-06-11, Dale Atkin <labrad***@ibycus.com> wrote:
Show quoteHide quote
> I've been a big fan of explicit variable declaration ever since many years
> ago when I spent nearly a whole day trying to track down a bug, that was the
> result of a simple 'typo'. Ever since, I've made sure "Option Explicit" was
> on in every program I write, and I've never looked back.
>
> I've noticed however that in certain circumstances, the compiler doesn't
> seem to mind inferring the data type of some variables.
>
> For example, I would have expected that the following code would generate an
> error:
>
>     Private Function DoSomething() As String
>         For i = 0 To 10000
>
>         Next
>         Return 0
>     End Function
>
> But it doesn't.  It seems that the For loop is somehow an exception to the
> 'explicit' variable declaration rule, and I'm not sure why, or what other
> situations this same 'exception' might apply in, and its making me a bit
> nervous.
>
> Dale
>

If your using VB2008, try:

Option Infer Off

--
Tom Shelton
Author
12 Jun 2009 8:58 AM
Patrice
VB 2008 now has type inference. See :
http://msdn.microsoft.com/en-us/library/bb384937.aspx for details.

--
Patrice

"Dale Atkin" <labrad***@ibycus.com> a écrit dans le message de groupe de
discussion : #SC3LYq6JHA.1***@TK2MSFTNGP02.phx.gbl...
Show quoteHide quote
> I've been a big fan of explicit variable declaration ever since many years
> ago when I spent nearly a whole day trying to track down a bug, that was
> the result of a simple 'typo'. Ever since, I've made sure "Option
> Explicit" was on in every program I write, and I've never looked back.
>
> I've noticed however that in certain circumstances, the compiler doesn't
> seem to mind inferring the data type of some variables.
>
> For example, I would have expected that the following code would generate
> an error:
>
>    Private Function DoSomething() As String
>        For i = 0 To 10000
>
>        Next
>        Return 0
>    End Function
>
> But it doesn't.  It seems that the For loop is somehow an exception to the
> 'explicit' variable declaration rule, and I'm not sure why, or what other
> situations this same 'exception' might apply in, and its making me a bit
> nervous.
>
> Dale