为什么这样做? [英] Why does this work?
本文介绍了为什么这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码(Visual Studio 2005)应该通过实例变量(
&)为我正在访问共享成员的
效果生成编译警告;颜色变量:
私人Sub X(ByVal颜色为颜色)
Me.BackColor = color.AliceBlue
结束子
在某些*计算机上,这会产生预期的警告。在其他人身上它确实没有b $ b。有什么设置导致某些计算机无法投诉吗?
TIA - Bob
This code (Visual Studio 2005) should generate a compilation warning to the
effect that I''m accessing a shared member through an instance variable (the
"color" variable):
Private Sub X(ByVal color As Color)
Me.BackColor = color.AliceBlue
End Sub
On *some* computers this produces the expected warning. On others it does
not. Any clue what setting is causing some computers to fail to complain?
TIA - Bob
推荐答案
Vb .net不区分大小写,所以一般来说,有一个名为color
的变量可能不好。
将参数更改为colorparam,然后将行更改为Me.BackColor =
colorparam.AliceBlue,我打赌你到处都会收到警告。
" Bob Altman" < rd*@nospam.nospamwrote in message
news:ur ************** @ TK2MSFTNGP06.phx.gbl ...
Vb.net is not case sensitive, so in general, having a variable called color
could be bad.
Change the parameter to colorparam and then the line to Me.BackColor =
colorparam.AliceBlue, and I bet you get the warning everywhere.
"Bob Altman" <rd*@nospam.nospamwrote in message
news:ur**************@TK2MSFTNGP06.phx.gbl...
此代码(Visual Studio 2005)应该生成一个编译警告
我通过实例变量访问共享成员的效果
(颜色变量):
私有子X(ByVal颜色为颜色)
Me.BackColor = color.AliceBlue
结束子
在某些*计算机上,这会产生预期的警告。在其他人身上它确实没有b $ b。有什么设置导致某些计算机无法投诉吗?
TIA - Bob
This code (Visual Studio 2005) should generate a compilation warning to
the effect that I''m accessing a shared member through an instance variable
(the "color" variable):
Private Sub X(ByVal color As Color)
Me.BackColor = color.AliceBlue
End Sub
On *some* computers this produces the expected warning. On others it does
not. Any clue what setting is causing some computers to fail to complain?
TIA - Bob
''实例变量访问共享成员''警告是可配置的,因此
在某些机器上设置为Warning,而在其他机器上设置为None。
Bob Altman < rd*@nospam.nospamwrote in message
news:ur ************** @ TK2MSFTNGP06.phx.gbl ...
''Instance variable accesses shared member'' warning is configurable, so
it is set to Warning on some of those machines, and None on the others.
"Bob Altman" <rd*@nospam.nospamwrote in message
news:ur**************@TK2MSFTNGP06.phx.gbl...
此代码(Visual Studio 2005)应该生成一个编译警告
我通过实例变量访问共享成员的效果
(颜色变量):
私有子X(ByVal颜色为颜色)
Me.BackColor = color.AliceBlue
结束子
在某些*计算机上,这会产生预期的警告。在其他人身上它确实没有b $ b。有什么设置导致某些电脑无法投诉吗?
TIA - Bob
This code (Visual Studio 2005) should generate a compilation warning to
the effect that I''m accessing a shared member through an instance variable
(the "color" variable):
Private Sub X(ByVal color As Color)
Me.BackColor = color.AliceBlue
End Sub
On *some* computers this produces the expected warning. On others it does
not. Any clue what setting is causing some computers to fail to complain?
TIA - Bob
是否得到错误或警告与否取决于特定的
编译器设置(在项目的属性中)。
但实际上,你的代码是不通过实例访问共享成员
变量。
实例变量是您创建实例的变量(隐式或
明确地)并为其分配一个变量 - 你的代码没有这样做,它是b / b
关联一个变量(颜色)来表示一个静态类型。但是,当你用
写color.AliceBlue时,编译器看不到你的大写字母
颜色,它看到你试图访问一个类型的成员
称为颜色并且由于.NET框架中有一个名为
" color"的类型,编译器知道你使用的是颜色的共享成员
类型而不是你的颜色参数名称。
在C#中,这段代码没有问题,因为它是区分大小写的语言,
但是在VB中它可以使你的代码变得强硬排除故障。正如另一个回复
所说,当命名变量不仅仅是改变这种情况。
-Scott
Bob Altman ; < rd*@nospam.nospamwrote in message
news:ur ************** @ TK2MSFTNGP06.phx.gbl ...
Whether you get an error or warning or not is dependant on that particular
compiler settings (in the project''s properties).
But, actually, you code is NOT accessing a shared member through an instance
variable.
An instance variable is one where you make an instance (implicitly or
explicitly) and assign a variable to it - - your code is not doing that, it
is associating a variable (color) to represent a static type. But, when you
write color.AliceBlue, the compiler doesn''t see your capitalization of
color, it sees that you are attempting to access the members of a type
called "color" and since there is a type in the .NET framework called
"color", the compiler knows you are using the shared members of the color
type and not your color parameter name.
In C#, this code would be no problem since it is a case-sensitive language,
but in VB it can make your code tough to troubleshoot. As the other reply
said, when naming variables do more than change the case.
-Scott
"Bob Altman" <rd*@nospam.nospamwrote in message
news:ur**************@TK2MSFTNGP06.phx.gbl...
此代码(Visual Studio 2005)应该生成一个编译警告
我通过实例变量访问共享成员的效果
(颜色变量):
私有子X(ByVal颜色为颜色)
Me.BackColor = color.AliceBlue
结束子
在某些*计算机上,这会产生预期的警告。在其他人身上它确实没有b $ b。有什么设置导致某些电脑无法投诉吗?
TIA - Bob
This code (Visual Studio 2005) should generate a compilation warning to
the effect that I''m accessing a shared member through an instance variable
(the "color" variable):
Private Sub X(ByVal color As Color)
Me.BackColor = color.AliceBlue
End Sub
On *some* computers this produces the expected warning. On others it does
not. Any clue what setting is causing some computers to fail to complain?
TIA - Bob
这篇关于为什么这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文