|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Interesting Results In VB.NetDim sb As StringBuilder If i = 0 sb = New StringBuilder(i.ToString()) Next i Console.WriteLine(sb.ToString()) Next i The above code gives the following output: 0 0 0 0 0 Kinda wierd how the StringBuilder (or any object) saves it state after the first iteration of the loop. The StringBuilder's text stays the same and doesn't re-initialize. Comments? Mythran Mythran wrote:
Show quoteHide quote > For i As Integer = 0 To 5 Is there any guarantee that short-scoped variables are really> Dim sb As StringBuilder > If i = 0 > sb = New StringBuilder(i.ToString()) > Next i > Console.WriteLine(sb.ToString()) > Next i > > The above code gives the following output: > > 0 > 0 > 0 > 0 > 0 > > Kinda wierd how the StringBuilder (or any object) saves it state after the > first iteration of the loop. The StringBuilder's text stays the same and > doesn't re-initialize. > > Comments? > > Mythran short-life variables? They may have the same life-time as the subroutine or function they are in, depending upon how the compiler wishes to compile the code. Mythran wrote:
Show quoteHide quote > For i As Integer = 0 To 5 Not really that suprising... VB.NET supports block scope. So, you> Dim sb As StringBuilder > If i = 0 > sb = New StringBuilder(i.ToString()) > Next i > Console.WriteLine(sb.ToString()) > Next i > > The above code gives the following output: > > 0 > 0 > 0 > 0 > 0 > > Kinda wierd how the StringBuilder (or any object) saves it state after the > first iteration of the loop. The StringBuilder's text stays the same and > doesn't re-initialize. > > Comments? declare a reference type at the begining of the scope (the scope being the for/next): Dim sb As StringBuilder That reserves storage - but does not initialize the value. You only initialize the value once, when i = 0. If you had modified the code to look like: For i As Integer = 0 To 5 Dim sb As New StringBuilder If i = 0 Then sb.Append(i.ToString()) End If Console.WriteLine(sb) Next Then you would have gotten very different results. -- Tom Shelton [MVP]
Show quote
Hide quote
"Tom Shelton" <t**@mtogden.com> wrote in message Aye, not suprising, but still interesting. I have been thinking all along news:1145921361.015189.240390@j33g2000cwa.googlegroups.com... > > Mythran wrote: >> For i As Integer = 0 To 5 >> Dim sb As StringBuilder >> If i = 0 >> sb = New StringBuilder(i.ToString()) >> Next i >> Console.WriteLine(sb.ToString()) >> Next i >> >> The above code gives the following output: >> >> 0 >> 0 >> 0 >> 0 >> 0 >> >> Kinda wierd how the StringBuilder (or any object) saves it state after >> the >> first iteration of the loop. The StringBuilder's text stays the same and >> doesn't re-initialize. >> >> Comments? > > Not really that suprising... VB.NET supports block scope. So, you > declare a reference type at the begining of the scope (the scope being > the for/next): > that it gets re-dimmed (not ReDim but re-dimensioned) because it would be out of scope at the end of each iteration...but I had been mistaken. It does NOT get re-dimensioned. It gets dimensioned a single time and seem to not even look at it after every iteration after (I haven't check the IL so not too sure). Show quoteHide quote > Dim sb As StringBuilder Yeah, I know. Before my original post, I tested that as well. That wasn't > > That reserves storage - but does not initialize the value. You only > initialize the value once, when i = 0. If you had modified the code to > look like: > > For i As Integer = 0 To 5 > Dim sb As New StringBuilder > If i = 0 Then > sb.Append(i.ToString()) > End If > Console.WriteLine(sb) > Next > > Then you would have gotten very different results. > interesting though, already knew it heh :P > -- Thanks for the reply Tom...> Tom Shelton [MVP] > Mythran On 2006-04-24, Mythran <kip_potter@hotmail.comREMOVETRAIL> wrote:
Show quoteHide quote > For i As Integer = 0 To 5 That's bitten me before, and I scoured the language spec at one point> Dim sb As StringBuilder > If i = 0 > sb = New StringBuilder(i.ToString()) > Next i > Console.WriteLine(sb.ToString()) > Next i > > The above code gives the following output: > > 0 > 0 > 0 > 0 > 0 > > Kinda wierd how the StringBuilder (or any object) saves it state after the > first iteration of the loop. The StringBuilder's text stays the same and > doesn't re-initialize. for a discussion of it, but couldn't find anything one way or another about it. It feels wrong to me, but I'm pretty vague about why it feels wrong. However, declarations with initializers *are* executed at each iteration of the loop. For i as Integer = 0 to 5 Dim sb As StringBuilder = Nothing ''' Since VS2005 pretty much insists on the initializer anyway, that makes the issue go away, for me at least. Mythran,
I did not test it however does this build.. > For i As Integer = 0 To 5 The Then is missing> Dim sb As StringBuilder > If i = 0 > sb = New StringBuilder(i.ToString()) It is a multiline If so there should be an End IF> Next i There is only one For while there are two "Next" for that.> Console.WriteLine(sb.ToString()) > Next i Strange in my opinion. Cor
Show quote
Hide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message lol oops! I didn't even catch it after I re-read it...news:ekEYmSCaGHA.4580@TK2MSFTNGP03.phx.gbl... > Mythran, > > I did not test it however does this build.. > >> For i As Integer = 0 To 5 >> Dim sb As StringBuilder >> If i = 0 > > The Then is missing > >> sb = New StringBuilder(i.ToString()) > > It is a multiline If so there should be an End IF > >> Next i > > >> Console.WriteLine(sb.ToString()) >> Next i > > There is only one For while there are two "Next" for that. > > Strange in my opinion. > > Cor > > Anywho, looks like For i As Integer = 0 To 5 Dim sb As StringBuilder If i = 0 sb = New StringBuilder(i.ToString()) End If Console.WriteLine(sb.ToString()) Next i And, on the first point, the "Then" keyword is not required :) I never use it in VB.Net...makes my if's one word shorter..and doesn't look bad either ;) Thanks for pointing that mistake out, :) Mythran Mytrhan,
> And, on the first point, the "Then" keyword is not required :) I never You should tell this to Herfried, I hate that "then", in the same way as I > use it in VB.Net...makes my if's one word shorter..and doesn't look bad > either ;) > hate the "Dim". In the current VB there is with Strict and Explicit on not any need for that. But a lot find that nice, some nostalgie as they describe it to me why it is needed. Although for me these keyword could stay optional to keep it backwards compatible. Cor Mythran wrote:
> For i As Integer = 0 To 5 No, the above code does not compile. What did you actually run?> Dim sb As StringBuilder > If i = 0 '1 > sb = New StringBuilder(i.ToString()) '1 > Next i > Console.WriteLine(sb.ToString()) '2 > Next i '3 > > The above code gives the following output: Errors: 1: If without End If 2: 'sb' not declared (because the scope it was in was closed by the previous line) 3: Next without For -- Larry Lard Replies to group please Larry Lard wrote:
Show quoteHide quote > Mythran wrote: Add the "Then" to the if statement, and it compiles just fine (well, as> > For i As Integer = 0 To 5 > > Dim sb As StringBuilder > > If i = 0 '1 > > sb = New StringBuilder(i.ToString()) '1 > > Next i > > Console.WriteLine(sb.ToString()) '2 > > Next i '3 > > > > The above code gives the following output: > > No, the above code does not compile. What did you actually run? > > Errors: > 1: If without End If > 2: 'sb' not declared (because the scope it was in was closed by the > previous line) > 3: Next without For > long as you import System.Text). -- Tom Shelton [MVP] Tom,
> Add the "Then" to the if statement, and it compiles just fine (well, as Are you sure, one For loop which has two Next and no End If in a multiline > long as you import System.Text). > If?. Did you try it with Mono? Cor Show quoteHide quote "Tom Shelton" <t**@mtogden.com> schreef in bericht news:1145976995.312423.256840@y43g2000cwc.googlegroups.com... > > Larry Lard wrote: >> Mythran wrote: >> > For i As Integer = 0 To 5 >> > Dim sb As StringBuilder >> > If i = 0 '1 >> > sb = New StringBuilder(i.ToString()) '1 >> > Next i >> > Console.WriteLine(sb.ToString()) '2 >> > Next i '3 >> > >> > The above code gives the following output: >> >> No, the above code does not compile. What did you actually run? >> >> Errors: >> 1: If without End If >> 2: 'sb' not declared (because the scope it was in was closed by the >> previous line) >> 3: Next without For >> > > Add the "Then" to the if statement, and it compiles just fine (well, as > long as you import System.Text). > > -- > Tom Shelton [MVP] > Cor Ligthert [MVP] wrote:
> Tom, Actually, I goofed. I didn't notice the extra next. It really isn't a> > > > Add the "Then" to the if statement, and it compiles just fine (well, as > > long as you import System.Text). > > > > Are you sure, one For loop which has two Next and no End If in a multiline > If?. proper if statement. I didn't cut and paste his code when I tried it. So, I just typed it the way it should have been :) > Did you try it with Mono? No. I tried it in VS.NET 2003 :)-- Tom Shelton [MVP]
Show quote
Hide quote
"Tom Shelton" <t**@mtogden.com> wrote in message Grr...news:1145976995.312423.256840@y43g2000cwc.googlegroups.com... > > Larry Lard wrote: >> Mythran wrote: >> > For i As Integer = 0 To 5 >> > Dim sb As StringBuilder >> > If i = 0 '1 >> > sb = New StringBuilder(i.ToString()) '1 >> > Next i >> > Console.WriteLine(sb.ToString()) '2 >> > Next i '3 >> > >> > The above code gives the following output: >> >> No, the above code does not compile. What did you actually run? >> >> Errors: >> 1: If without End If >> 2: 'sb' not declared (because the scope it was in was closed by the >> previous line) >> 3: Next without For >> > > Add the "Then" to the if statement, and it compiles just fine (well, as > long as you import System.Text). > > -- > Tom Shelton [MVP] > 1: If DOES NOT REQUIRE a "Then" keyword. Try it and find out ;) 2: The code DOES compile if you change the first instance of "Next i" to "End If" .. my mistake on that. 3: See #2 for that fix. Sorry, that's what I get for typing off top of head...I hate how OE tries to re-format my code when I copy and paste, so I either open notepad and do 2 copies/pastes, or just type off top of head.. HTH, Mythran Mythran wrote:
Show quoteHide quote > "Tom Shelton" <t**@mtogden.com> wrote in message Doh! Your right it is optional in VB.NET. I guess, I never realized> news:1145976995.312423.256840@y43g2000cwc.googlegroups.com... > > > > Larry Lard wrote: > >> Mythran wrote: > >> > For i As Integer = 0 To 5 > >> > Dim sb As StringBuilder > >> > If i = 0 '1 > >> > sb = New StringBuilder(i.ToString()) '1 > >> > Next i > >> > Console.WriteLine(sb.ToString()) '2 > >> > Next i '3 > >> > > >> > The above code gives the following output: > >> > >> No, the above code does not compile. What did you actually run? > >> > >> Errors: > >> 1: If without End If > >> 2: 'sb' not declared (because the scope it was in was closed by the > >> previous line) > >> 3: Next without For > >> > > > > Add the "Then" to the if statement, and it compiles just fine (well, as > > long as you import System.Text). > > > > -- > > Tom Shelton [MVP] > > > > Grr... > > 1: If DOES NOT REQUIRE a "Then" keyword. Try it and find out ;) it because VS just automatically adds it - and it used to be required in VB5/6. I'm glad they made it optional... > 2: The code DOES compile if you change the first instance of "Next i" to I never noticed it... I just typed your code into VS - so it just did> "End If" .. my mistake on that. it :) > 3: See #2 for that fix. It is a pain that...> > Sorry, that's what I get for typing off top of head...I hate how OE tries to > re-format my code when I copy and paste, so I either open notepad and do 2 > copies/pastes, or just type off top of head.. -- Tom Shelton [MVP] Mythran wrote:
Show quoteHide quote > For i As Integer = 0 To 5 Eeeuuw.. dimming vars in a loop> Dim sb As StringBuilder > If i = 0 > sb = New StringBuilder(i.ToString()) > Next i > Console.WriteLine(sb.ToString()) > Next i > > The above code gives the following output: > > 0 > 0 > 0 > 0 > 0 > > Kinda wierd how the StringBuilder (or any object) saves it state after > the first iteration of the loop. The StringBuilder's text stays the > same and doesn't re-initialize. > > Comments? > > Mythran > -- Rinze van Huizen C-Services Holland b.v Rinze,
> Eeeuuw.. dimming vars in a loop As I do forever. Those dims should be in the stack from the loop and with > that in my opinion nicely be cleaned up. Strange is that it does not as we have seen. Cor Show quoteHide quote > > -- > Rinze van Huizen > C-Services Holland b.v What's wrong with making the scope of variables as narrow as possible?
Brian C-Services Holland b.v. wrote: Show quoteHide quote > Eeeuuw.. dimming vars in a loop > > > -- > Rinze van Huizen > C-Services Holland b.v Brian Gideon wrote:
Show quoteHide quote > What's wrong with making the scope of variables as narrow as possible? I just don't like dimming variables all over the place. It's a sure fire > > Brian > > C-Services Holland b.v. wrote: > >>Eeeuuw.. dimming vars in a loop >> >> >>-- >>Rinze van Huizen >>C-Services Holland b.v > > way to lose one. I don't care if it works, I just find it plain ugly. -- Rinze van Huizen C-Services Holland b.v Rinze,
You should try it, beside more efficient is it even quicker when you are making a program. Declaring global variables in the Main Section is something from the Cobol time and very inefficient. Cor Show quoteHide quote "C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht news:8PadnYAIxYH7w9LZnZ2dnUVZ8tydnZ2d@zeelandnet.nl... > Brian Gideon wrote: >> What's wrong with making the scope of variables as narrow as possible? >> >> Brian >> >> C-Services Holland b.v. wrote: >> >>>Eeeuuw.. dimming vars in a loop >>> >>> >>>-- >>>Rinze van Huizen >>>C-Services Holland b.v >> >> > > I just don't like dimming variables all over the place. It's a sure fire > way to lose one. I don't care if it works, I just find it plain ugly. > > > -- > Rinze van Huizen > C-Services Holland b.v
Show quote
Hide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message I used to be the same way, only dimensioning my vars at the top of the news:e5bJO0SaGHA.4248@TK2MSFTNGP03.phx.gbl... > Rinze, > > You should try it, beside more efficient is it even quicker when you are > making a program. > > Declaring global variables in the Main Section is something from the Cobol > time and very inefficient. > > Cor > > "C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht > news:8PadnYAIxYH7w9LZnZ2dnUVZ8tydnZ2d@zeelandnet.nl... >> Brian Gideon wrote: >>> What's wrong with making the scope of variables as narrow as possible? >>> >>> Brian >>> >>> C-Services Holland b.v. wrote: >>> >>>>Eeeuuw.. dimming vars in a loop >>>> >>>> >>>>-- >>>>Rinze van Huizen >>>>C-Services Holland b.v >>> >>> >> >> I just don't like dimming variables all over the place. It's a sure fire >> way to lose one. I don't care if it works, I just find it plain ugly. >> >> >> -- >> Rinze van Huizen >> C-Services Holland b.v > > method block...but now, after I tried it and used it...it isn't as ugly as I thought it to be. Just dimension at the first point you need to use it. I usually try to dimension and initialize at the same time, that way, if I don't need the var, it's easy to track down :) HTH, Mythran Mythran wrote:
Show quoteHide quote > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message I pretty much try to dim my vars in one place - at the begining of the> news:e5bJO0SaGHA.4248@TK2MSFTNGP03.phx.gbl... > > Rinze, > > > > You should try it, beside more efficient is it even quicker when you are > > making a program. > > > > Declaring global variables in the Main Section is something from the Cobol > > time and very inefficient. > > > > Cor > > > > "C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht > > news:8PadnYAIxYH7w9LZnZ2dnUVZ8tydnZ2d@zeelandnet.nl... > >> Brian Gideon wrote: > >>> What's wrong with making the scope of variables as narrow as possible? > >>> > >>> Brian > >>> > >>> C-Services Holland b.v. wrote: > >>> > >>>>Eeeuuw.. dimming vars in a loop > >>>> > >>>> > >>>>-- > >>>>Rinze van Huizen > >>>>C-Services Holland b.v > >>> > >>> > >> > >> I just don't like dimming variables all over the place. It's a sure fire > >> way to lose one. I don't care if it works, I just find it plain ugly. > >> > >> > >> -- > >> Rinze van Huizen > >> C-Services Holland b.v > > > > > > I used to be the same way, only dimensioning my vars at the top of the > method block...but now, after I tried it and used it...it isn't as ugly as I > thought it to be. Just dimension at the first point you need to use it. I > usually try to dimension and initialize at the same time, that way, if I > don't need the var, it's easy to track down :) > > HTH, > Mythran scope they are used. But, I also try to limit the scope to as small as possible. So - My code might look like (well, I mostly code C#, but since this is a VB group :) Public Sub TheBestSubEver () Dim anInteger As Integer Dim aString As String anInteger = SomeValue For i As Integer = 0 To SomeLargeIntegerValue Dim anIntegerInThisScope As Integer ' Do really cool loopy stuff with i and anIntegerInThisScope Next aString = "Hurrray, were done!" End Sub Anyway... That's what I do :) -- Tom Shelton Cor Ligthert [MVP] wrote:
> Rinze, Why do you assume I'm declaring everything global in main? I only > > You should try it, beside more efficient is it even quicker when you are > making a program. > > Declaring global variables in the Main Section is something from the Cobol > time and very inefficient. > > Cor > declare vars global when it's absolutely nescesary. Normally I declare every var I need in a function/sub at the top of that particular function or sub. I really don't see any advantage to declaring a variable halfway in a sub in a loop. This has always been my little pet peeve with Basic (or Clipper for that matter), plucking vars out of the air when you need them. -- Rinze van Huizen C-Services Holland b.v Rinze,
Feel free to do it your way. As I always say that there is seldom a best method in VB. As Mythran I Use For I as integer = 0 to end Next. But as you insist on Dim I as integer bla bla bla bla bla for I = 0 to end next Than that is your own decission Cor Show quoteHide quote "C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht news:5uidnd7b1M7WJM3ZnZ2dnUVZ8qOdnZ2d@zeelandnet.nl... > Cor Ligthert [MVP] wrote: >> Rinze, >> >> You should try it, beside more efficient is it even quicker when you are >> making a program. >> >> Declaring global variables in the Main Section is something from the >> Cobol time and very inefficient. >> >> Cor >> > > Why do you assume I'm declaring everything global in main? I only declare > vars global when it's absolutely nescesary. Normally I declare every var I > need in a function/sub at the top of that particular function or sub. I > really don't see any advantage to declaring a variable halfway in a sub in > a loop. This has always been my little pet peeve with Basic (or Clipper > for that matter), plucking vars out of the air when you need them. > > > -- > Rinze van Huizen > C-Services Holland b.v C-Services Holland b.v. wrote:
> Brian Gideon wrote: It may seem ugly to you, but to others who have to maintain the code it> > What's wrong with making the scope of variables as narrow as possible? > > > I just don't like dimming variables all over the place. It's a sure fire > way to lose one. I don't care if it works, I just find it plain ugly. > is a little more difficult to decipher when and where it is used. It's pretty much the defacto standard. Brian
Shared dll paths
Accessing Foxpro Database VB.NET reading CSV files (odbc or oledb) Make font Bold Register DLL for COM interop vb.net 2005 - how to populate a listbox i need Code Generator... Performance testing tools for VB.NET application COM reference (X) as Interop.X Sql query..... Sum of another aggragate function |
|||||||||||||||||||||||