局部变量 - 快速问题 [英] Local variables - quick question
问题描述
我这样做了:
for(int i = 0; i< 10; i ++){。 ..}
并且在i变量的本地声明之后我试图将其理解为
int i = 0;
VS.NET 2003给了我错误:
"名为''i'的局部变量不能在此范围内声明,因为它会给'b'赋予不同的含义,''我''已在''子'范围内用于
表示别的东西
任何人都可以解释我这是 - 我怎么能改写这个?
-
最好的问候
stic
Hi,
I done sth like this:
for(int i=0; i<10; i++) {...}
and after this local declaration of i variable I try to inicialize
int i=0;
And VS.NET 2003 gives me error:
" local variable named ''i'' cannot be declared in this scope because it would
give a different meaning to ''i'', which is already used in a ''child'' scope to
denote something else"
Anyone could explain me this is - how can I rewrite this ?
--
best regards
stic
推荐答案
Stefan Turalski(stic)< st ** ***********@kruk-inkaso.com.pl>写道:
Stefan Turalski (stic) <st*************@kruk-inkaso.com.pl> wrote:
我这样做了:
for(int i = 0; i< 10; i ++){...}
并且在i变量的本地声明之后我尝试将其理解为
int i = 0;
并且VS.NET 2003给了我错误:
名为我的局部变量不能在此范围内声明,因为它会给我赋予不同的含义,这已经在孩子范围内使用,表示
表示别的什么
任何人都可以解释我这是 - 我怎么能重写这个?
I done sth like this:
for(int i=0; i<10; i++) {...}
and after this local declaration of i variable I try to inicialize
int i=0;
And VS.NET 2003 gives me error:
" local variable named ''i'' cannot be declared in this scope because it would
give a different meaning to ''i'', which is already used in a ''child'' scope to
denote something else"
Anyone could explain me this is - how can I rewrite this ?
基本上使用不同的变量名。局部变量
的范围是它声明的整个块,并且你不能声明一个变量
,其名称已经是采取"在这个范围内。
或者,你可以开始一个新的范围,取决于你正在做的事情:
for(int i = 0; i< 10; i ++)
{
....
}
{
int i = 5;
//
}
>
没关系。
-
Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
Use different variable names, basically. The scope of a local variable
is the whole block it''s declared in, and you can''t declare a variable
with a name which is already "taken" within that scope.
Alternatively, you could possibly start a new scope, depending on
exactly what you were doing:
for (int i=0; i < 10; i++)
{
....
}
{
int i = 5;
//
}
would be okay.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Uzytkownik" Jon Skeet [C#MVP]" < SK *** @ pobox.com> napisal w wiadomosci
新闻:MP ************************ @ msnews.microsoft.c om ...
Uzytkownik "Jon Skeet [C# MVP]" <sk***@pobox.com> napisal w wiadomosci
news:MP************************@msnews.microsoft.c om...
Stefan Turalski(stic)< st ************* @ kruk-inkaso.com.pl>写道:
Stefan Turalski (stic) <st*************@kruk-inkaso.com.pl> wrote:
我这样做了:
for(int i = 0; i< 10; i ++){...}
并且在i变量的本地声明之后我尝试将其理解为
int i = 0;
并且VS.NET 2003给了我错误:
名为''i'的局部变量不能在此范围内声明,因为
会给''i'赋予不同的含义,这已经在''child''
范围中用来表示别的什么
任何人都可以解释我这是 - 我怎么能重写这个?
I done sth like this:
for(int i=0; i<10; i++) {...}
and after this local declaration of i variable I try to inicialize
int i=0;
And VS.NET 2003 gives me error:
" local variable named ''i'' cannot be declared in this scope because it would give a different meaning to ''i'', which is already used in a ''child'' scope to denote something else"
Anyone could explain me this is - how can I rewrite this ?
基本上使用不同的变量名。局部变量的范围
是它声明的整个块,你不能声明一个名称已经被采取的变量
。在这个范围内。
或者,你可以开始一个新的范围,取决于你正在做什么:
for(int i = 0; i < 10; i ++)
{
...
}
{i / 5;
//
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复群组,请不要给我发邮件
Use different variable names, basically. The scope of a local variable
is the whole block it''s declared in, and you can''t declare a variable
with a name which is already "taken" within that scope.
Alternatively, you could possibly start a new scope, depending on
exactly what you were doing:
for (int i=0; i < 10; i++)
{
...
}
{
int i = 5;
//
}
would be okay.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
感谢您的快速回答 - 我所做的就是重命名这个变量。
顺便说一句。有没有办法提前读取变量范围?或者msdn给出了关于变量范围的信息
- 我不期望本地for() - 循环在这个循环范围内具有
范围; - )
-
最好的问候
stic
Hi,
Thanks for your quick answer - all what I did was to rename this variable.
btw. Is there a way to read ahead variable scope? Or where msdn gives info
about scope of variables - I wasn''t expecting that local for() - loop have
scope over this loop scope ;-)
--
best regards
stic
>
Stefan Turalski(stic)< st ************* @ kruk-inkaso.com.pl>写道:
Stefan Turalski (stic) <st*************@kruk-inkaso.com.pl> wrote:
感谢您的快速回答 - 我所做的就是重命名这个变量。
顺便说一下。有没有办法提前读取变量范围?
究竟是什么意思预读变量范围?
或者msdn提供有关变量范围的信息 - 我不是不要期望本地for() - 循环具有超出此循环范围的范围; - )
Thanks for your quick answer - all what I did was to rename this variable.
btw. Is there a way to read ahead variable scope?
What exactly do you mean by "read ahead variable scope"?
Or where msdn gives info
about scope of variables - I wasn''t expecting that local for() - loop have
scope over this loop scope ;-)
查看ECMA C#规范的第10.7节
( http://www.jaggersoft.com/ csharp_standard / 10.7.htm )
或MS规范的第3.7节
http://msdn.microsoft.com/library/de...l=/library/en-
us / csspec / html / vclrfcsharpspec_3_7.asp,
又名 http ://tinyurl.com/394xc
-
Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet
如果回复小组,请不要给我发邮件
Look at section 10.7 of the ECMA C# spec
(http://www.jaggersoft.com/csharp_standard/10.7.htm)
or section 3.7 of the MS spec
http://msdn.microsoft.com/library/de...l=/library/en-
us/csspec/html/vclrfcsharpspec_3_7.asp,
aka http://tinyurl.com/394xc
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于局部变量 - 快速问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!