未初始化的变量 [英] Uninitialized variables
问题描述
这是预期的行为吗?
Winform按钮和列表框:
Dim i As Integer
对于i = 0到50
Dim s As String
如果i = 1那么
s ="只有一次?"
结束如果
Me.ListBox1.Items.Add(" Test" s)
Next
如果我运行此代码,我发现s的值仍然是
只有一次?通过i = 1之后的所有迭代,尽管
它没有被声明为静态。
这肯定是(可能)在C中发生的一个
未初始化的变量,但是我虽然VB是超级安全的并且
会自动将变量初始化为合理的默认值?
如果没有当然VB编译器应该做C#
编译器做的事情并检查未初始化的变量?
Tim
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
推荐答案
您使用的是什么版本的.NET?
-
HTH,>
- Tom Spink,überGeek
请回复新闻组,
所以所有人都可以受益
也许它是一款名为''惩罚用户''的游戏,
Tim Anderson" < TI ***** @ hotmail.com>在消息中写道
news:e8 ************* @tk2msftngp13.phx.gbl ...
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...
这是预期的行为吗?带按钮和列表框的Winform:
Dim i As Integer
对于i = 0到50
Dim s As String
如果i = 1然后
s =只有一次?
结束如果
Me.ListBox1.Items.Add(" Test" s)
Next
如果我运行此代码,我发现s的值仍然是
只有一次?通过i = 1之后的所有迭代,虽然它没有被声明为静态。
这肯定是(可能)在C中发生的
未初始化变量,但我虽然VB是超级安全的,并且会自动将变量初始化为合理的默认值?
如果没有,那么VB编译器肯定应该执行C#
编译器所做的事情并检查未初始化的变量?
Tim
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
你好,
"蒂姆安德森 < TI ***** @ hotmail.com> schrieb:
Hello,
"Tim Anderson" <ti*****@hotmail.com> schrieb:
这是预期的行为吗?
Winform with button and listbox:
Dim i As Integer
for i = 0 To 50
Dim s As String
如果i = 1那么
s =只有一次?
结束如果
Me.ListBox1.Items.Add(" ;测试& s)
如果我运行此代码,我发现s的值仍然是
只有一次?通过i = 1后的所有迭代,虽然它没有被声明为静态。
那是恕我直言的预期行为:
\\\
Dim i As整数
对于i = 1到2
Dim s As String
如果i = 1那么
s =" ;只有一次?
结束如果
MsgBox
下一页i
///
将显示仅一次?两次。请注意,在循环的每次迭代中,s将被_not_销毁并重新分配。
这肯定是(可能)在C中发生的
未初始化的变量,但我虽然VB是超级安全的,并且
会自动将变量初始化为合理的默认值?
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?
变量将在迭代中保持不变环。让我们看看下面的代码:
\\\
Dim i As Integer
对于i = 1到2
Dim s As String =""
如果i = 1那么
s ="只有一次?
结束如果
MsgBox
下一页我
///
上面的代码与此相同:
\\\
Dim i As Integer
For i = 1 to 2
Dim s As String
s =""
如果i = 1那么
s ="只有一次?
结束如果
MsgBox
接下来我们
///
在这两种情况下,s只会被分配一次并且不会被销毁
(好的,为字符串变量分配一个新值会创建一个新字符串,但是
这不是我想说的)。
问候,
Herfried K. Wagner
-
MVP·VB Classic,VB .N ET
http://www.mvps.org/dotnet
Tim,
您的字符串是使用块范围定义的,其中块是For / Next
循环。你永远不会离开它,所以它不会被重新定义。
拉里伍兹
Tim Anderson < TI ***** @ hotmail.com>在消息中写道
news:e8 ************* @tk2msftngp13.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.
Larry Woods
"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:e8*************@tk2msftngp13.phx.gbl...
这是预期的行为吗?带按钮和列表框的Winform:
Dim i As Integer
对于i = 0到50
Dim s As String
如果i = 1然后
s =只有一次?
结束如果
Me.ListBox1.Items.Add(" Test" s)
Next
如果我运行此代码,我发现s的值仍然是
只有一次?通过i = 1之后的所有迭代,虽然它没有被声明为静态。
这肯定是(可能)在C中发生的
未初始化变量,但我虽然VB是超级安全的,并且会自动将变量初始化为合理的默认值?
如果没有,那么VB编译器肯定应该执行C#
编译器所做的事情并检查未初始化的变量?
Tim
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
这篇关于未初始化的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!