关于块级范围&可变寿命 [英] Regarding Block level Scope & Variable Life Time
问题描述
大家好!
VB.NET增加了创建仅在
块中可见的变量的功能。块是代码的任何部分,以一个单词
End,Loop或Next结尾。这个
表示For ... Next和If ... End If块可以拥有自己的
变量。
所以,
虽然y> 6
Dim x as interger
......
结束时
但是x具有生命周期的程序级别。手段,
Dim x as interger - 这条线路的效果。仅在第一时间。是吧
对吗?
谢谢&问候
Elankathir,
B''lore,
印度。
***通过开发人员指南 http://www.developersdex.com 发送***
不要只参加USENET ......获得奖励!
如果你问如何使用循环中的vars我会说
(伪)
私人子bla
dim x作为对象
loop
x =新对象
结束循环
end sub
本地的for也可以
for i as integer = 0到100 step 1
array(i)。 bla = bla
next i
有些想法
eric
ElanKathir .S.N < EL ************* @ yahoo.co.in>在消息中写道
新闻:OV ************** @ tk2msftngp13.phx.gbl ...大家好!
变量。
所以,
而y> ; 6
Dim x as interger
......
结束时
但是x有一生的程序级别。手段,
Dim x as interger - 这条线的效果。仅在第一时间。是吧
谢谢&问候
Elankathir,
B''lore,
印度。
***通过Developersdex发送 http://www.developersdex.com ***
不要只是参加USENET ...获得奖励它!
Elan,
是的,它们否决了在<更高级别上声明的变量br />
方法。
但是你不能在方法中多次声明变量
它也在每个块中来自尝试捕获错误块的每个块由
实例。
我希望这有帮助吗?
Cor
Elankathir和其他人,
这是一个样本:
私人Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim i As Short
For i = 1到10
Dim x As Short
x = 1
下一页
Dim x As Short
MessageBox.Show(" ; x = &安培; x)
结束子
如图所示,我在第一次声明x时出错,说x隐藏了
a在封闭块中声明的变量。这很有意思,因为
声明在For循环之后。这似乎表明所有
声明都在块中的任何代码执行之前发生,而不管Dim语句所在的是什么
。这是正确的吗?
当我注释掉第二个声明时,我在消息框中收到错误
行说在这种情况下无法访问x因为它是私人的。
它无法更改为受保护或公开。
我没有尝试过,但我很确定在C ++中,类似的
结构是完全可以接受的。内部x将用于
for循环,消息框将显示一个随机值,因为外部x
永远不会被初始化。没有检查一个范围内的变量
声明是否隐藏了另一个范围内的声明。最多会产生一个
警告,而不是错误。
我试着评论内部声明并留下外部
声明。我得到一个错误,说明在宣布
之前不能使用x。
啊,好吧。这似乎不是一个问题我现在需要担心
。
Rob
Hi all !
VB.NET adds the ability to create variables that are visible only within
a block. A block is any section of code that ends with one of the words
End , Loop , or Next . This
means that For...Next and If...End If blocks can have their own
variables.
So,
While y>6
Dim x as interger
......
End while
But x have lifetime of procedure level. Means,
Dim x as interger -- This line geting eff. at first time only. is it
right ?
Thanks & Regards
Elankathir,
B''lore,
India.
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!
if you are asking how to use vars in loops i would say
(pseudo)
private sub bla
dim x as object
loop
x = new object
end loop
end sub
local in a for could also be
for i as integer = 0 to 100 step 1
array(i). bla = bla
next i
yust some thoughts
eric
"ElanKathir .S.N" <el*************@yahoo.co.in> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...Hi all !
VB.NET adds the ability to create variables that are visible only within
a block. A block is any section of code that ends with one of the words
End , Loop , or Next . This
means that For...Next and If...End If blocks can have their own
variables.
So,
While y>6
Dim x as interger
......
End while
But x have lifetime of procedure level. Means,
Dim x as interger -- This line geting eff. at first time only. is it
right ?
Thanks & Regards
Elankathir,
B''lore,
India.
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!
Hi Elan,
Yes and they overrule the variables declared on a higher level outside the
method.
However you can not declare variables more times inside a method
It is in every block too every block from a try catch error block by
instance.
I hope this helps?
Cor
Elankathir and everybody else,
Here''s a sample:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Short
For i = 1 To 10
Dim x As Short
x = 1
Next
Dim x As Short
MessageBox.Show("x = " & x)
End Sub
As shown, I get an error at the first declaration of x, saying that x hides
a variable declared in the enclosing block. This is interesting, since the
declaration is after the For loop. This appears to show that all
declarations happen before any code in a block is executed, regardless of
where the Dim statements are. Is that correct?
When I comment out the second declaration, I get an error in the message box
line saying that x is not accessible in this context because it is private.
It cannot be changed to protected or public.
I haven''t tried it, but I''m pretty durn sure that in C++, a similar
construct would be perfectly acceptable. The inner x would be used in the
for loop, and the message box would show a random value because the outer x
would never have been initialized. There is no check for whether a variable
declaration in one scope hides a declaration in another scope. At most, a
warning, not an error, would have been generated.
I tried commenting out the inner declaration and leaving the outer
declaration. I got an error saying that x cannot be used before it is
declared.
Ah, well. This doesn''t seem to be an issue I''m going to need to worry about
much in real life.
Rob
这篇关于关于块级范围&可变寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!