Home All Groups Group Topic Archive Search About

What exactly does this message mean?

Author
27 Aug 2006 12:36 AM
Brian Shafer
I have VS2005 installed on my destop and on my laptop. I created this app
with my desktop and i don't get this. Run the IDE on my laptop and this is
what I get?

Access of shared member, constant member, enum member or nested type through
an instance; qualifying expression will not be evaluated.

Author
27 Aug 2006 12:48 AM
Herfried K. Wagner [MVP]
"Brian Shafer" <BrianSha***@discussions.microsoft.com> schrieb:
>I have VS2005 installed on my destop and on my laptop. I created this app
> with my desktop and i don't get this. Run the IDE on my laptop and this is
> what I get?
>
> Access of shared member, constant member, enum member or nested type
> through
> an instance; qualifying expression will not be evaluated.

You are attempting to access a member which is marked as 'Shared' using a
reference referencing an instance of the type.  Instead, simply write '<type
name>.<member name>'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
27 Aug 2006 2:27 AM
JimmyKoolPantz
I just want to make a comment, I am not sure what the error refers to,
however, I have seen similiar warning signs on some of the programs I
have written.  Example, If I want to sort an array, I would assume I
could use syntax such as:

MyArray.Sort(MyArray)

This would cause the same warning that you have mentioned, but if I
change my syntax to something like:

system.Array.Sort(MyArray) and the error warning would go away.

I personally think its a coding issue, you might want to check to see
if both of your programs are referencing the same NameSpaces (import
statements).


Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> "Brian Shafer" <BrianSha***@discussions.microsoft.com> schrieb:
> >I have VS2005 installed on my destop and on my laptop. I created this app
> > with my desktop and i don't get this. Run the IDE on my laptop and this is
> > what I get?
> >
> > Access of shared member, constant member, enum member or nested type
> > through
> > an instance; qualifying expression will not be evaluated.
>
> You are attempting to access a member which is marked as 'Shared' using a
> reference referencing an instance of the type.  Instead, simply write '<type
> name>.<member name>'.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
27 Aug 2006 7:43 AM
Brian Shafer
It is the same source code... just copied to my laptop...

Show quoteHide quote
"JimmyKoolPantz" wrote:

> I just want to make a comment, I am not sure what the error refers to,
> however, I have seen similiar warning signs on some of the programs I
> have written.  Example, If I want to sort an array, I would assume I
> could use syntax such as:
>
> MyArray.Sort(MyArray)
>
> This would cause the same warning that you have mentioned, but if I
> change my syntax to something like:
>
> system.Array.Sort(MyArray) and the error warning would go away.
>
> I personally think its a coding issue, you might want to check to see
> if both of your programs are referencing the same NameSpaces (import
> statements).
>
>
> Herfried K. Wagner [MVP] wrote:
> > "Brian Shafer" <BrianSha***@discussions.microsoft.com> schrieb:
> > >I have VS2005 installed on my destop and on my laptop. I created this app
> > > with my desktop and i don't get this. Run the IDE on my laptop and this is
> > > what I get?
> > >
> > > Access of shared member, constant member, enum member or nested type
> > > through
> > > an instance; qualifying expression will not be evaluated.
> >
> > You are attempting to access a member which is marked as 'Shared' using a
> > reference referencing an instance of the type.  Instead, simply write '<type
> > name>.<member name>'.
> >
> > --
> >  M S   Herfried K. Wagner
> > M V P  <URL:http://dotnet.mvps.org/>
> >  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
>
>
Author
28 Aug 2006 3:37 PM
Jim Wooley
Show quote Hide quote
> It is the same source code... just copied to my laptop...

>>> "Brian Shafer" <BrianSha***@discussions.microsoft.com> schrieb:
>>>
>>>> I have VS2005 installed on my destop and on my laptop. I created
>>>> this app with my desktop and i don't get this. Run the IDE on my
>>>> laptop and this is what I get?
>>>>
>>>> Access of shared member, constant member, enum member or nested
>>>> type
>>>> through
>>>> an instance; qualifying expression will not be evaluated.
>>> You are attempting to access a member which is marked as 'Shared'
>>> using a reference referencing an instance of the type.  Instead,
>>> simply write '<type name>.<member name>'.

You are probably seeing the FxCop rules being run where they aren't run on
your other computer's IDE (pre 2005 or without the Code Analysis turned on
in the project).

Here's an explanation of the issue. Let's say you have a class called foo
with a shared method bar as follows:

Public Class Foo
  Public Shared Sub Bar()
    'Do Something
  End Sub
End Class

Now, lets consider how you call this method. You can not have a calling method
that calls Bar on an instance of Foo as follows:
Public Sub Main()
  Dim instance as new Foo
  instance.Bar 'This is invalid
End Sub

Instead, you need to call Bar directly without an instance as follows:
Public Sub Main()
  Foo.Bar()
End Sub

Sometimes, you can end up with conflicts in namespaces and although you think
you are calling the shared method directly, you are trying to call it from
an instance as follows:
Public Sub Main()
  Dim foo as new Foo
  foo.Bar()  'Incorrect
  MyApp.Foo.Bar() 'Correct assuming MyApp is the namespace for the application.
End Sub

I hope this helps you understand the situation. Typically, this is a relatively
easy oversight to make and luckily fix as well.
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Author
27 Aug 2006 8:55 AM
Heikki Leivo
> MyArray.Sort(MyArray)
> system.Array.Sort(MyArray) and the error warning would go away.

The difference in these statements is that MyArray is a variable, so it may
be nothing and null reference exception occurs. Of course, this is a bad
example since the same variable is used as parameter as well, so an
exception would actually be thrown in both cases, but you got the point?

-h-
Author
27 Aug 2006 10:31 AM
Herfried K. Wagner [MVP]
"Heikki Leivo" <heikkidotleivo@cadworksdotfi.removethis> schrieb:
>> MyArray.Sort(MyArray)
>> system.Array.Sort(MyArray) and the error warning would go away.
>
> The difference in these statements is that MyArray is a variable, so it
> may be nothing and null reference exception occurs. Of course, this is a
> bad example since the same variable is used as parameter as well, so an
> exception would actually be thrown in both cases, but you got the point?

Note that 'Array.Sort' is a shared method, so it can eben be called on a
'Nothing' reference.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
27 Aug 2006 2:34 PM
Heikki Leivo
> Note that 'Array.Sort' is a shared method, so it can eben be called on a
> 'Nothing' reference.

Well, thanks for correcting. This ain't my first time being wrong. I have
always thought that this was the true meaning of the mentioned error
message.

-h-