Is this expected behavior?
Winform with button and listbox:
Dim i As Integer
For i = 0 To 50
Dim s As String
If i = 1 Then
s = "Only once?"
End If
Me.ListBox1.Items.Add("Test " & s)
Next
If I run this code I find that the value of s remains at
"Only once?" through all the iterations after i=1, although
it is not declared as static.
This is certainly what (might) happen in C with an
uninitialized variable, but I though VB was super-safe and
would automatically initialize variables to a sensible default?
If not, surely the VB compiler should do what the C#
compiler does and check for uninitialized variables?
Tim 23 6295
What version of .NET are you using?
--
HTH,
-- Tom Spink, Über Geek
Please respond to the newsgroup,
so all can benefit
"Maybe it's a game called 'Punish the User'"
"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:e8*************@tk2msftngp13.phx.gbl... Is this expected behavior?
Winform with button and listbox:
Dim i As Integer For i = 0 To 50 Dim s As String If i = 1 Then s = "Only once?" End If Me.ListBox1.Items.Add("Test " & s) Next
If I run this code I find that the value of s remains at "Only once?" through all the iterations after i=1, although it is not declared as static.
This is certainly what (might) happen in C with an uninitialized variable, but I though VB was super-safe and would automatically initialize variables to a sensible default?
If not, surely the VB compiler should do what the C# compiler does and check for uninitialized variables?
Tim
Hello,
"Tim Anderson" <ti*****@hotmail.com> schrieb: Is this expected behavior?
Winform with button and listbox:
Dim i As Integer For i = 0 To 50 Dim s As String If i = 1 Then s = "Only once?" End If Me.ListBox1.Items.Add("Test " & s) Next
If I run this code I find that the value of s remains at "Only once?" through all the iterations after i=1, although it is not declared as static.
That's IMHO the expected behavior:
\\\
Dim i As Integer
For i = 1 To 2
Dim s As String
If i = 1 Then
s = "Only once?"
End If
MsgBox(s)
Next i
///
Will show "Only once?" two times. Notice that s will _not_ be destroyed and
re-allocated in every iteration of the loop.
This is certainly what (might) happen in C with an uninitialized variable, but I though VB was super-safe and would automatically initialize variables to a sensible default?
The variable will persist over the iterations of the loop. Let's have a
look at the code below:
\\\
Dim i As Integer
For i = 1 To 2
Dim s As String = ""
If i = 1 Then
s = "Only once?"
End If
MsgBox(s)
Next i
///
The code above is the same as this:
\\\
Dim i As Integer
For i = 1 To 2
Dim s As String
s = ""
If i = 1 Then
s = "Only once?"
End If
MsgBox(s)
Next i
///
In both cases, 's' will be allocated only one time and will not be destroyed
(OK, assigning a new value to a string variable creates a new string, but
that's not what I want to say).
Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET http://www.mvps.org/dotnet
Tim,
Your string is defined with block scope, where the block is the For/Next
loop. You never leave it, so it doesn't get redefined.
Larry Woods
"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:e8*************@tk2msftngp13.phx.gbl... Is this expected behavior?
Winform with button and listbox:
Dim i As Integer For i = 0 To 50 Dim s As String If i = 1 Then s = "Only once?" End If Me.ListBox1.Items.Add("Test " & s) Next
If I run this code I find that the value of s remains at "Only once?" through all the iterations after i=1, although it is not declared as static.
This is certainly what (might) happen in C with an uninitialized variable, but I though VB was super-safe and would automatically initialize variables to a sensible default?
If not, surely the VB compiler should do what the C# compiler does and check for uninitialized variables?
Tim
Hi Tim,
Also, in VB6 there was no concept of Block scope. You will notice that in
VB6 the variable is not re-initialized. In VB.NET we have what is called
Block Scope. Notice that in VB.NET you cannot use the variable 's' outside
of the For/Next loop. Since the scope of 's' is inside of the For/Next and
*not* the procedure it is initialized each time through the For/Next loop.
This is described in: http://msdn.microsoft.com/library/de...us/vbcn7/html/
vbconscopelevels.asp
The static variable lifetime is different so it doesn't get re-initialized.
Look up lifetime under visual basic language concepts in help and you will
find this explanation:
More stuff on LifeTime: http://msdn.microsoft.com/library/de...us/vbcn7/html/
vbconlifetime.asp
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
-------------------- From: "Tim Anderson" <ti*****@hotmail.com> References: <e8*************@tk2msftngp13.phx.gbl>
<OA**************@TK2MSFTNGP12.phx.gbl>Subject: Re: Uninitialized variables Date: Tue, 2 Sep 2003 11:15:36 +0100 Lines: 25 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: <uZ**************@tk2msftngp13.phx.gbl> Newsgroups: microsoft.public.dotnet.languages.vb NNTP-Posting-Host: pc4-stap2-3-cust205.nott.cable.ntl.com 81.103.228.205 Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:133461 X-Tomcat-NG: microsoft.public.dotnet.languages.vb
"Larry Woods" <la***@lwoods.com> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl... Tim,
Your string is defined with block scope, where the block is the For/Next loop. You never leave it, so it doesn't get redefined.
Thanks (and to others) for the comments. I notice VB6 is the same.
I still find the logic of this somewhat puzzling. The Dim keyword declares
andallocates storage space for variables. In this situation though, although
theDim statement is executed on each iteration of the loop, it is only
effectiveonce. However, if I type:
Dim s As String = String.Empty
then the empty string is assigned on each iteration of the loop. However, if I type:
Static s as String = String.Empty
the assignment only happens once (which is what I would expect).
Tim
"Peter Huang [MSFT]" <v-******@online.microsoft.com> wrote in message news:2%****************@cpmsftngxa06.phx.gbl... Since the scope of 's' is inside of the For/Next and *not* the procedure it is initialized each time through the For/Next loop. This is described in:
But it is *not* initialized each time, as your referenced tech note makes clear:
"Even if the scope of an element is limited to a block, its lifetime is still that
of the entire procedure. If you enter the block more than once during the
procedure, a block variable retains its previous value."
I don't see the logic of this, particularly since if you want this behavior you
can have it via the static declaration (I realise that static is different in that
its value will persist if the procedure is re-entered, but you could reset the
value at the beginning of the procedure).
However I understand how it works now so hopefully it won't catch me
out (I'm a believer in initializing all variables anyway).
Tim
Hi Tim,
If you change the code
Static s as String = String.Empty
to
Static s as String
s = String.Empty
Then it will be initialized every time you enter the for-next loop. I think
it is by design.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
-------------------- From: "Tim Anderson" <ti*****@hotmail.com> References: <e8*************@tk2msftngp13.phx.gbl>
<OA**************@TK2MSFTNGP12.phx.gbl>
<uZ**************@tk2msftngp13.phx.gbl>
<2#**************@cpmsftngxa06.phx.gbl>Subject: Re: Uninitialized variables Date: Wed, 3 Sep 2003 07:30:17 +0100 Lines: 23 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: <OW**************@tk2msftngp13.phx.gbl> Newsgroups: microsoft.public.dotnet.languages.vb NNTP-Posting-Host: pc4-stap2-3-cust205.nott.cable.ntl.com 81.103.228.205 Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:133699 X-Tomcat-NG: microsoft.public.dotnet.languages.vb
"Peter Huang [MSFT]" <v-******@online.microsoft.com> wrote in message
news:2%****************@cpmsftngxa06.phx.gbl... Since the scope of 's' is inside of the For/Next and *not* the procedure it is initialized each time through the For/Next
loop. This is described in: But it is *not* initialized each time, as your referenced tech note makes
clear: "Even if the scope of an element is limited to a block, its lifetime is
still thatof the entire procedure. If you enter the block more than once during the procedure, a block variable retains its previous value."
I don't see the logic of this, particularly since if you want this
behavior youcan have it via the static declaration (I realise that static is different
in thatits value will persist if the procedure is re-entered, but you could reset
thevalue at the beginning of the procedure).
However I understand how it works now so hopefully it won't catch me out (I'm a believer in initializing all variables anyway).
Tim
Peter,
I don't have any problem getting the code to do what I want,
now that I know what VB does.
I just doubt the logic of it, that's all :-)
Given that VB auto-initializes strings to Nothing, one would think
that:
Dim s as String
and
Dim s as String = Nothing
would be equivalent. But they are not!
Thanks,
Tim
"Peter Huang [MSFT]" <v-******@online.microsoft.com> wrote in message news:CA**************@cpmsftngxa06.phx.gbl... Hi Tim,
If you change the code Static s as String = String.Empty to Static s as String s = String.Empty
Then it will be initialized every time you enter the for-next loop. I think it is by design.
Tim,
I am following this thread from the start.(I even have it in a
testsituation) :-)
If string=nothing means in VB.net string exist but have no value
If string Is nothing means in VB.net if string does not exist.
I would find it very unregular if that logic was not followed in your
examles.
Cor
"Cor" <no*@non.com> wrote in message news:3f***********************@reader20.wxs.nl... Tim, I am following this thread from the start.(I even have it in a testsituation) :-) If string=nothing means in VB.net string exist but have no value If string Is nothing means in VB.net if string does not exist. I would find it very unregular if that logic was not followed in your examles.
Not sure if I fully understand your comment. Consider this code:
Dim s as String = Nothing
Console.WriteLine(s = String.Empty)
Console.WriteLine(isNothing(s))
Console.WriteLine(isNothing(String.Empty))
This outputs:
True
True
False
Is that what you expect? It strikes me as odd.
Tim
Tim,
I find it mean from you to mean that what I try to say is mean.
In VB.net we have the Microsoft.Visual Basic commando's and the System.Net
commando's.
That they maybe are look alike doesn't mean they are the same.
Nobody in this thread has spoken about the isNothing function.
But I stop argue with you because it isNothing
I was stupid that I did it not let it be for me Is Nothing.
Succes with others.
Cor
"Tim Anderson" <ti*****@hotmail.com> schrieb "Cor" <no*@non.com> wrote in message news:3f***********************@reader20.wxs.nl... Tim, I am following this thread from the start.(I even have it in a testsituation) :-) If string=nothing means in VB.net string exist but have no value If string Is nothing means in VB.net if string does not exist. I would find it very unregular if that logic was not followed in your examles.
Not sure if I fully understand your comment. Consider this code:
Dim s as String = Nothing Console.WriteLine(s = String.Empty) Console.WriteLine(isNothing(s)) Console.WriteLine(isNothing(String.Empty))
This outputs:
True True False
Is that what you expect? It strikes me as odd.
- String is a reference type
- String.Empty is a zero-length String.
- When comparing Strings using "=", Nothing is converted to a zero-length
string before comparison (see docs for "=" comparison operator). That's why
the first line returns "True".
--
Armin
"Armin Zingler" <az*******@freenet.de> wrote in message news:ek**************@TK2MSFTNGP11.phx.gbl... - When comparing Strings using "=", Nothing is converted to a zero-length string before comparison (see docs for "=" comparison operator). That's why the first line returns "True".
Well yes, so it seems. I'm not sure though why VB does this.
Tim
"Cor" <no*@non.com> schrieb: If string=nothing means in VB.net string exist but have no value
It has the value "" ("empty" string, string of length 0).
If string Is nothing means in VB.net if string does not exist. I would find it very unregular if that logic was not followed in your examles.
???
--
Herfried K. Wagner
MVP · VB Classic, VB.NET http://www.mvps.org/dotnet
"Tim Anderson" <ti*****@hotmail.com> schrieb "Armin Zingler" <az*******@freenet.de> wrote in message news:ek**************@TK2MSFTNGP11.phx.gbl...
- When comparing Strings using "=", Nothing is converted to a zero-length string before comparison (see docs for "=" comparison operator). That's why the first line returns "True".
Well yes, so it seems. I'm not sure though why VB does this.
Because the docs say that it has to work this way.
;-))
--
Armin
Herfried,
Normaly I just see to this discussions to have fun, I have them seen so
often and they are as the water in the Donau. If string Is nothing means in VB.net string does not exist. I would find it very unregular if that logic was not followed in your examples.
The discussion was about the declaration from strings and what was the logic
behind some things,
But it was moving up and everytime there was a new argument.
When I came in it was about that it was not logic that
string = nothing was equal to string = ""
But in VB is | If String = nothing | something else than | If String Is
nothing|.
Therefore it is logical to keep that logic constant in the language.
What it in another language means is not relevant.
And then came again a new item "IsNothing" which has of course nothing to do
with it.
Or as much as Mid, Len, etc. (just as example not completly)
So I stopped arguing with my standard examples about natural languages.
A word has different meanings, but it has to be posible to evaluate that.
It looks if Tim thinks that a language is a table of mathimatical
instructions.
You know how we think about that.
Cor
ps.
You know of course that mean has minimal 3 different meanings in English.
Does what?
This seems logical. = is a comparison operator, 'Is' is an object reference
comparison.
Reference Types pass themselves around as pointers, so if two pointers are
the same, hurrah! same object reference.
'Is' is used for comparing these pointers.
= is used for comparing values.
So, in the case of =, Nothing is a zero-length string, and in the case of
'Is', Nothing is a null reference pointer.
--
HTH,
-- Tom Spink, Über Geek
Please respond to the newsgroup,
so all can benefit
"Maybe it's a game called 'Punish the User'"
"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:#B*************@TK2MSFTNGP11.phx.gbl... "Armin Zingler" <az*******@freenet.de> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl... - When comparing Strings using "=", Nothing is converted to a
zero-length string before comparison (see docs for "=" comparison operator). That's
why the first line returns "True".
Well yes, so it seems. I'm not sure though why VB does this.
Tim
"Tom Spink" <th**********@ntlworld.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl... Does what?
So, in the case of =, Nothing is a zero-length string,
IMO Nothing is never a zero-length string.
Tim
"Cor" <no*@non.com> wrote in message news:3f***********************@reader20.wxs.nl... It looks if Tim thinks that a language is a table of mathimatical instructions.
Not something I recall saying.
Cor ps. You know of course that mean has minimal 3 different meanings in English.
3 words in common usage from different roots. But many different meanings.
Tim
Tim,
That is the basic why you and me cann't discuss about this.
We have basically different idea's about a language.
It has no sence to talk about a detail when you disagree about the basics.
From your point of view you are right.
From my point of view I am right.
So it is not something personaly, but this discussion between you and me
leads to = nothing.
Cor
"Tim Anderson" <ti*****@hotmail.com> schrieb "Tom Spink" <th**********@ntlworld.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl... Does what?
So, in the case of =, Nothing is a zero-length string,
IMO Nothing is never a zero-length string.
Tom meant that "in the case of =", Nothing is handled as a zero-length
string, and that is correct. That's what the docs to the "=" comparison
operator say and how it actually works.
IMO, Nothing should never be used with value types at all (as I've already
written several times...), i.e. the compiler should not even allow the usage
with value types, like C# does. Nothing should really mean Nothing, no
object, no thing, not anything... How can I set an Integer variable to
Nothing? Does the variable disappear?? Zero *is* a value, not nothing.
--
Armin
"Cor" <no*@non.com> wrote in message news:3f***********************@reader20.wxs.nl... Tim, That is the basic why you and me cann't discuss about this. We have basically different idea's about a language.
Fair enough, but I'd be interested in a link to something that
summarises your ideas about a language so I can make sure
I disagree with it :-)
Tim
"Armin Zingler" <az*******@freenet.de> wrote in message news:eS*************@TK2MSFTNGP11.phx.gbl... Tom meant that "in the case of =", Nothing is handled as a zero-length string, and that is correct. That's what the docs to the "=" comparison operator say and how it actually works.
Yes, I wasn't disagreeing with Tom's explanation (apologies if it was
taken that way), merely giving my opinion.
IMO, Nothing should never be used with value types at all (as I've already written several times...), i.e. the compiler should not even allow the usage with value types, like C# does. Nothing should really mean Nothing, no object, no thing, not anything... How can I set an Integer variable to Nothing? Does the variable disappear?? Zero *is* a value, not nothing.
Makes sense to me.
Tim
Tim
That I will do,
In my opinion is a language to express you. (I always talk about natural
languages)
That can be just talking but too giving commands.
You are able to do that in a language in different ways with different
words.
That is because of your emotions, the situations etc. etc.
The first program languages, I am very long in this business, so I have seen
a lot, where totally bound to the computer or totally mathematical.
They disappeared.
I did VB always find a terrible program language, (Interpreter language with
row numbers and goto's)
VB net has the strange thing that is like the English language a mixture of
2 cultures.
I do not know if you are from Europe, but when you are talking with someone
from Germany in English, they use often other English words than people from
France.
That is possible in VB.net too, and in my opinion that is a benefit I never
saw before in a program language.
Cor This discussion thread is closed Replies have been disabled for this discussion. Similar topics
22 posts
views
Thread by steve |
last post: by
|
13 posts
views
Thread by rswanster |
last post: by
|
12 posts
views
Thread by jyu.james |
last post: by
|
3 posts
views
Thread by julien |
last post: by
|
21 posts
views
Thread by sanjaymeher |
last post: by
|
148 posts
views
Thread by onkar |
last post: by
|
18 posts
views
Thread by Spoon |
last post: by
| | | | | | | | | | |