|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Not as 'explicit' as I thought --- What am I missingago 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 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 "sloan" <sl***@ipass.net> wrote in message yep. Same result (mostly)news:eAlmxjq6JHA.1568@TK2MSFTNGP06.phx.gbl... > Have you tried > > Option Strict On > > http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.80).aspx Dale Atkin wrote:
Show quoteHide quote > I've been a big fan of explicit variable declaration ever since many 1. With a for loop you can write:> 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. 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
Show quote
Hide quote
> That makes a lot of sense actually. Thanks for the detailed explanation. I'd > 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. 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
Show quote
Hide quote
"Dale Atkin" <labrad***@ibycus.com> wrote in message Option Infer is used quite a bit with Linq. I create classes for DB access 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 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 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 If your using VB2008, try:> 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 > Option Infer Off -- Tom Shelton 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
vb.net app not finding application configuration file on Windows 2003 Server Standard
Problem with Windows Server 2003 Standard R2 FILE I/O Inheritance? Interface? A question about a failing regular expression Is this Normal? Error adding a data provider to a datagridview control Autosize the last column in a ListView control using WndProc Pass Variable At Design Time mcisendstring stereo problem Add value to data-bound datagridview during run-time |
|||||||||||||||||||||||