Re: Why does this work?

Tech-Archive recommends: Fix windows errors by optimizing your registry



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@xxxxxxxxxxxxxxxxxxxxxxx
The 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@xxxxxxxxxxxxxxxxxxxxxxx
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.





.



Relevant Pages

  • Re: Why does this work?
    ... the reference within the sub to 'color.AliceBlue' is a reference to the ... warning appears. ... compiler as accessing a shared member from a "type", ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why does this work?
    ... The compiler is able to tell the difference between the two by the context in which the word "color" is used. ... The point is that the compiler reads the original code as attempting to access a shared member of an instance. ... That's why the compiler is warning the programmer that it's not going to do what was coded. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why does this work?
    ... compiler settings. ... But, actually, you code is NOT accessing a shared member through an instance ... On *some* computers this produces the expected warning. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why does this work?
    ... the compiler believes that the instance exists. ... It's the compiler, not the programmer, that doesn't ... compiler as accessing a shared member from a "type", ... No. OP states that he WAS getting a warning on some machines. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... With the attendant warning about the constant expression. ... This is a common programming technique. ... Good programming practice would consist of using the compiler at ... One can write 'bool' in C++ but not C. ...
    (microsoft.public.vc.mfc)