This came out of a thread explaining to "BK" about error BC42025
("Access of shared member through an instance; qualifying expression
will not be evaluated"); Frans Clasener then came up with another
similar problem, which I believe shows up a bug (well, a problem) in
VB2005.
As many will know, VB2005 saw the reintroduction of the 'default
instance' of Form classes, allowing one to write code such as
Form1.Show
without ever explicitly creating an instance of Form1. But a problem
shows up when this syntax collides with the existing use of
'Form1.<something>', namely to refer to Shared members of the Form1
class. Searching the MSDN feedback area at
<http://connect.microsoft.com/feedback/default.aspx?SiteID=210> for
BC42025 shows that some problems have already been identified in this
area; what Frans has brought up is another, related but distinct,
problem.
To reproduce in VS2005:
- Create a Form class, say TestForm
- In TestForm, put
Public Const MyConstant As Integer = 1
- Note that "By default, all constants are Shared", so MyConstant is
now a Shared member of TestForm
- Create a module, and in that module put:
Public Sub foo()
Dim x As Integer
x = My.Forms.TestForm.MyConstant
End Sub
- Note that you get a green squiggly (with error BC42025 referenced),
with an error correction supertooltip that suggests changing this to
x = TestForm.MyConstant
- Do this. You will see that you _still_ have a green squiggly
referencing BC42025; but now no correction options. There is no way to
refer to a form's Const from outside the form, without getting this
warning.
The problem is that the compiler is thinking that 'TestForm' here is a
reference to the default instance of the class, rather than the class
itself; so it thinks a Shared member is being referenced through an
instance variable, and warns us something is amiss. Of course, we are
actually going through the class, but because the evil default
instances have the same name as the class, it gets confused. I suspect
it should prefer to resolve the name to the class rather than the
default instance.
Any comments before I submit this as a bug?
--
Larry Lard
Replies to group please