accuracy of warning?

G

Guest

Hi,

I've created a console app in VB.NET 2005 that consists of :

Sub Main()
Dim x As DateTime
x = x.Now

End Sub

I get the following warning:
Access of shared member, constant member, enum member or nested type through
an instance; qualifying expression will not be evaluated.

The warning itself makes sense mostly. There's no reason for me to use an
object instance to access a shared member. However, it states that the
qualifying expression will not be evaluated. This does not seem to be the
case. I get an accurate result with this code; the same result I get with x =
datetime.now . Is this warning inaccurate? Also, what is the harm in using an
object to access a shared member? If you've already got a datetime variable
for whatever reason, why is it discouraged to use it rather than the datetime
type to access the 'now' property?

-Ben
 
M

Marina

It means that 'x' is really ignored. It doesn't actually try to go get the
object that 'x' is pointing to.

The reason it is discouraged, is because the compiler is going to treat it
as if you had said DateTime.Now anyway. Putting a variable name there just
makes it more confusing to read, because it seems like the object that the
variable is pointing to is important, that having 'x' instead of 'y' there
could make a difference - which it would not.
 
G

Guest

Thanks!

-Ben

Marina said:
It means that 'x' is really ignored. It doesn't actually try to go get the
object that 'x' is pointing to.

The reason it is discouraged, is because the compiler is going to treat it
as if you had said DateTime.Now anyway. Putting a variable name there just
makes it more confusing to read, because it seems like the object that the
variable is pointing to is important, that having 'x' instead of 'y' there
could make a difference - which it would not.
 
H

Herfried K. Wagner [MVP]

Ben R. said:
I've created a console app in VB.NET 2005 that consists of :

Sub Main()
Dim x As DateTime
x = x.Now

End Sub

I get the following warning:
Access of shared member, constant member, enum member or nested type
through
an instance; qualifying expression will not be evaluated.

The warning itself makes sense mostly. There's no reason for me to use an
object instance to access a shared member. However, it states that the
qualifying expression will not be evaluated. This does not seem to be the
case. I get an accurate result with this code; the same result I get with
x =
datetime.now . Is this warning inaccurate? Also, what is the harm in using
an
object to access a shared member? If you've already got a datetime
variable
for whatever reason, why is it discouraged to use it rather than the
datetime
type to access the 'now' property?

Consider this sample:

\\\
Public Class Foo
Public Function Goo() As Bla
MsgBox("Foo.Goo")
Return New Bla()
End Function
End Class

Public Class Bla()
Public Shared Sub Baz()
MsgBox("Bla.Baz")
End Sub
End Class
....
Dim x As New Foo()
x.Goo().Baz()
///

In the sample above, 'Foo.Goo' won't be called and thus the message box
showing "Foo.Goo" won't be shown.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top