Re: Why does this work?
- From: "James Hahn" <jhahn@xxxxxxxxx>
- Date: Thu, 23 Oct 2008 17:39:03 +1100
The variable 'color' is an instance of the class 'Color', created in the Sub declaration. The Color class includes shared readonly properties (the named colors), so the reference within the sub to 'color.AliceBlue' is a reference to the shared member (AliceBlue) using the instance 'color'. That's why the warning appears.
In OP's case the issue is confused by the fact that a class name has been used as the instance name. You can see what's actually happening by checking the definition of 'color' (the instance, that is, as in 'color.AliceBlue'). The compiler doesn't automatically use the class instead of the instance simply because there's a naming conflict. The compiler has detected that the instance variable is being used to reference the shared member and has provided a warning so that the programmer is aware that the compiler has taken the source code to mean something other than what the programmer wrote. But the code as written is using the instance variable, and that's why the warning has appeared.
"Scott M." <s-mar@xxxxxxxxxxxxx> wrote in message news:%23Ae3OEINJHA.728@xxxxxxxxxxxxxxxxxxxxxxx
No James. No instance variable is being used here.
Here's an example that would be accessing a shared member through an instance variable (pseudo-code)
Public Class Foo
Public Shared Property Value As String
Get
End Get
Set()
End Set
End Property
End Class
'Some other class...
Sub DoSomething()
dim f As New Foo ' <-- This is your instance variable
f.Value = "Something" '<--- This is an attempt to access a shared member through an instance variable
End Sub
-----------
As I said, in the original example, when the compiler sees "color.AliceBlue", it doesn't assume your are talking about the "color" parameter, it knows to use the Color class and access its shared AliceBlue property, which is entirely correct. No instance of the Color class has been made (either implicitly or explicitly) and so no error occurs.
-Scott
"James Hahn" <jhahn@xxxxxxxxx> wrote in message news:eDMQOgANJHA.276@xxxxxxxxxxxxxxxxxxxxxxxThe code IS accessing a shared member through an instance variable. The compiler will always make the access through the class or structure name instead of evaluating the expression. The warning exists because the syntax is legal, but the compiler action may be unexpected.
"Scott M." <s-mar@xxxxxxxxxxxxx> wrote in message news:%23XbF6r$MJHA.4456@xxxxxxxxxxxxxxxxxxxxxxxWhether 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.
.
- Follow-Ups:
- Re: Why does this work?
- From: Scott M.
- Re: Why does this work?
- References:
- Why does this work?
- From: Bob Altman
- Re: Why does this work?
- From: Scott M.
- Re: Why does this work?
- From: James Hahn
- Re: Why does this work?
- From: Scott M.
- Why does this work?
- Prev by Date: Re: Why does this work?
- Next by Date: Global Keypress Event Handler
- Previous by thread: Re: Why does this work?
- Next by thread: Re: Why does this work?
- Index(es):
Relevant Pages
|