473,473 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Interesting Results In VB.Net

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?

Mythran

Apr 24 '06 #1
23 1164

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?

Mythran


Is there any guarantee that short-scoped variables are really
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.

Apr 24 '06 #2

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):

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]

Apr 24 '06 #3

"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.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):

Aye, not suprising, but still interesting. I have been thinking all along
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).
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.
Yeah, I know. Before my original post, I tested that as well. That wasn't
interesting though, already knew it heh :P
--
Tom Shelton [MVP]


Thanks for the reply Tom...

Mythran

Apr 25 '06 #4
On 2006-04-24, Mythran <ki********@hotmail.comREMOVETRAIL> 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.


That's bitten me before, and I scoured the language spec at one point
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.



Apr 25 '06 #5
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
Apr 25 '06 #6

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

--
Larry Lard
Replies to group please

Apr 25 '06 #7
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?

Mythran

Eeeuuw.. dimming vars in a loop
--
Rinze van Huizen
C-Services Holland b.v
Apr 25 '06 #8
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


--
Rinze van Huizen
C-Services Holland b.v

Apr 25 '06 #9

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]

Apr 25 '06 #10
Tom,

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?.

Did you try it with Mono?

Cor

"Tom Shelton" <to*@mtogden.com> schreef in bericht
news:11**********************@y43g2000cwc.googlegr oups.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]

Apr 25 '06 #11

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ek**************@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


lol oops! I didn't even catch it after I re-read it...

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

Apr 25 '06 #12

"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.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 ;)
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

Apr 25 '06 #13
Mytrhan,

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 ;)

You should tell this to Herfried, I hate that "then", in the same way as I
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
Apr 25 '06 #14

Cor Ligthert [MVP] wrote:
Tom,

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?.


Actually, I goofed. I didn't notice the extra next. It really isn't a
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]

Apr 25 '06 #15

Mythran wrote:
"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.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 ;)


Doh! Your right it is optional in VB.NET. I guess, I never realized
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
"End If" .. my mistake on that.
I never noticed it... I just typed your code into VS - so it just did
it :)
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..


It is a pain that...

--
Tom Shelton [MVP]

Apr 25 '06 #16
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


Apr 25 '06 #17
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
Apr 26 '06 #18
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." <cs*@REMOVEcsh4u.nl> schreef in bericht
news:8P******************************@zeelandnet.n l...
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

Apr 26 '06 #19

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:e5**************@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." <cs*@REMOVEcsh4u.nl> schreef in bericht
news:8P******************************@zeelandnet.n l...
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

Apr 26 '06 #20

Mythran wrote:
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:e5**************@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." <cs*@REMOVEcsh4u.nl> schreef in bericht
news:8P******************************@zeelandnet.n l...
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


I pretty much try to dim my vars in one place - at the begining of the
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

Apr 26 '06 #21

C-Services Holland b.v. wrote:
Brian Gideon wrote:
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.


It may seem ugly to you, but to others who have to maintain the code it
is a little more difficult to decipher when and where it is used. It's
pretty much the defacto standard.

Brian

Apr 27 '06 #22
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
Apr 27 '06 #23
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

"C-Services Holland b.v." <cs*@REMOVEcsh4u.nl> schreef in bericht
news:5u******************************@zeelandnet.n l...
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

Apr 27 '06 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
23
by: Bruno R. Dias | last post by:
Perhaps it would be interesting to program a virtual machine simulating an ancient computer (such as the pdp-7). Then, it would be rather interesting to code for it (porting gcc to it maybe?). I...
1
by: Steve Lutz | last post by:
I have written a web service to provide some back-end functionality to a Web Site. The web service returns lists of items. If I use the webservice via a browser, it works fine and returns the...
8
by: Mythran | last post by:
Here is some code the provides some really interesting results! First, read over the two methods in class 'A' and compare. Just by looking at them, both results appear to return the EXACT same...
26
by: v4vijayakumar | last post by:
Happened to see my old (last millennium) c code. Almost forgot that I wrote it . It is interesting. :-) int sqrt(int no) { int t; no = t * t; return t; }
27
by: Frederick Gotham | last post by:
I thought it might be interesting to share experiences of tracking down a subtle or mysterious bug. I myself haven't much experience with tracking down bugs, but there's one in particular which...
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
12
by: Daniel Earwicker | last post by:
I wrote two trivial test programs that do a billion iterations of a virtual method call, first in C# (Visual Studio 2005): Thing t = new DerivedThing(); for (System.Int64 n = 0; n < 10000000000;...
12
by: Mike | last post by:
Hi I am wonderinf if there are interesting examples to learn C. Or any good idea? Mike
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.