Home All Groups Group Topic Archive Search About

More VS2003 to VS2005 questions

Author
20 Jun 2006 12:16 PM
BK
Converting a rather large solution from 2003 to 2005.  All the errors
are worked out, but I'm trying to clean up the warnings as best I can.
The good news is that the application and it's supporting assemblies
are all working now.

The followins snippet is from a base form class I use in the
application.  Pretty typical stuff, when a data entry form is closing
and if the user is editing data, I want to allow them to save their
work.  Here is part of the code:

Dim Result As New DialogResult
Result = MessageBox.Show("Save Work?", "Close Form",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1)

If Result = DialogResult.Yes Then
        Me.Save()
End If

Hovering over the DialogResult.Yes portion of the snippet, I am
"greeted" with the following warning:

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

I *think* I understand the warning, but I don't understand how to
resolve it.  It runs just fine, but I won't feel right until I correct
the warning. 

What should I change?

Will

Author
20 Jun 2006 1:17 PM
Larry Lard
BK wrote:
> Converting a rather large solution from 2003 to 2005.  All the errors
> are worked out, but I'm trying to clean up the warnings as best I can.
> The good news is that the application and it's supporting assemblies
> are all working now.

Hurray!

> The followins snippet is from a base form class I use in the
> application.  Pretty typical stuff, when a data entry form is closing
> and if the user is editing data, I want to allow them to save their
> work.  Here is part of the code:
>
> Dim Result As New DialogResult

The New is unnecessary here.

Show quoteHide quote
> Result = MessageBox.Show("Save Work?", "Close Form",
> MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
> MessageBoxDefaultButton.Button1)
>
> If Result = DialogResult.Yes Then
>         Me.Save()
> End If
>
> Hovering over the DialogResult.Yes portion of the snippet, I am
> "greeted" with the following warning:
>
> Access of shared member, constant member, enum member or nested type
> through an instance; qualifying expression will not be evaluated.
>
> I *think* I understand the warning

Could you explain it to someone who didn't understand it at all? Go on,
try it :)

> , but I don't understand how to
> resolve it.

Do you not get the little Error Correction Options supertooltip (or
whatever they are called)? I do when I paste your code.

> It runs just fine, but I won't feel right until I correct
> the warning.

An excellent attitude.

>
> What should I change?

Well, as I said I get the supertooltip thing that suggests changing
DialogResult.Yes to Windows.Forms.DialogResult.Yes, which works. Now
for the explanation (most of which you will already know):

Shared members of classes (eg the Yes member of DialogResult)
conceptually 'belong' to the class itself (DialogResult), not to any
particular instance (eg Result here). So, for example, if you referred
to Result.No (which you will note doesn't make much sense,
semantically) you would get the same 'No' item; and what's more, even
if you had a *function call* that returned an instance of DialogResult,
and you referred to *its* .No, the function call _would not_ be made.
Deeply artifical example:

    Private Function SideEffects() As DialogResult
        MsgBox("omg")
        Return Windows.Forms.DialogResult.Yes
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        If SideEffects().No = Windows.Forms.DialogResult.Yes Then
            Msgbox("aha")
        End If

    End Sub

Clicking Button1 produces *no* messageboxes, not even the one in
SideEffects. Because .No is a Shared member, the compiler knows that
*whatever* DialogResult SideEffects returns, the expression
'SideEffects().No' will always have the same value - so it doesn't even
bother calling SideEffects(). *This* is what the compiler is warning
you about.

But hang on.

Your code:

If Result = DialogResult.Yes Then

isn't making this mistake! You are comparing an instance variable to a
value expressed as <classname>.<member>, which is correct, so why the
warning? Well, the clue comes if you put the cursor in this word
DialogResult and Shift+F2. You will be taken into the Object Browser
at:

Public Property DialogResult() As System.Windows.Forms.DialogResult
     Member of: System.Windows.Forms.Form
Summary:
Gets or sets the dialog result for the form.

Aha. In the code for a WinForm, just saying 'DialogResult' means not
the *class* of that name, but the *property* of that name, because it's
'closer' syntactically. And the *property* of that name is an
*instance*, so you are referencing a Shared member through an instance
variable, see above etc etc.

Really, I'd say it's slightly quirky of MS to have this property which
has the same name as the class - although these are separate
namespaces, so it's allowed, it's still slightly odd to see

Public Property DialogResult() As System.Windows.Forms.DialogResult

But hey, it's their Framework.

As to the fix, and why it works? Easy to see now. By spelling out that
we mean the *class* System.Windows.Forms.DialogResult, rather than the
*property* DialogResult, the compiler is satisfied, and the warning
goes away.

--
Larry Lard
Replies to group please
Author
20 Jun 2006 1:18 PM
M. Posseth
Hello Will

You must type the full name of the constant cause it is shared across the
project

Windows.Forms.DialogResult.OK

this warning is to protect you from accidently calling a method through an
instance of a class

so remember all shared members ( also sub routines and functions ) marked
with the shared keyword or contained in a shared class should be called with
there full name  



regards

Michel Posseth [MCP]





Show quoteHide quote
"BK" wrote:

> Converting a rather large solution from 2003 to 2005.  All the errors
> are worked out, but I'm trying to clean up the warnings as best I can.
> The good news is that the application and it's supporting assemblies
> are all working now.
>
> The followins snippet is from a base form class I use in the
> application.  Pretty typical stuff, when a data entry form is closing
> and if the user is editing data, I want to allow them to save their
> work.  Here is part of the code:
>
> Dim Result As New DialogResult
> Result = MessageBox.Show("Save Work?", "Close Form",
> MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
> MessageBoxDefaultButton.Button1)
>
> If Result = DialogResult.Yes Then
>         Me.Save()
> End If
>
> Hovering over the DialogResult.Yes portion of the snippet, I am
> "greeted" with the following warning:
>
> Access of shared member, constant member, enum member or nested type
> through an instance; qualifying expression will not be evaluated.
>
> I *think* I understand the warning, but I don't understand how to
> resolve it.  It runs just fine, but I won't feel right until I correct
> the warning. 
>
> What should I change?
>
> Will
>
>
Author
20 Jun 2006 2:36 PM
Cor Ligthert [MVP]
Which is seen by some in this newsgroup including me as a kind of bug in the
VB IDE.

We have the opinion that some warnings are overdone in the current version
and don't add anything to the language, moreover, some lead to not wanted
code because they are only equal by C# warnings. But in that language it has
effect.

Cor

Show quoteHide quote
"M. Posseth" <MPoss***@discussions.microsoft.com> schreef in bericht
news:7B04344C-4BA3-4A83-AB36-127D340A32CC@microsoft.com...
> Hello Will
>
> You must type the full name of the constant cause it is shared across the
> project
>
> Windows.Forms.DialogResult.OK
>
> this warning is to protect you from accidently calling a method through an
> instance of a class
>
> so remember all shared members ( also sub routines and functions ) marked
> with the shared keyword or contained in a shared class should be called
> with
> there full name
>
>
>
> regards
>
> Michel Posseth [MCP]
>
>
>
>
>
> "BK" wrote:
>
>> Converting a rather large solution from 2003 to 2005.  All the errors
>> are worked out, but I'm trying to clean up the warnings as best I can.
>> The good news is that the application and it's supporting assemblies
>> are all working now.
>>
>> The followins snippet is from a base form class I use in the
>> application.  Pretty typical stuff, when a data entry form is closing
>> and if the user is editing data, I want to allow them to save their
>> work.  Here is part of the code:
>>
>> Dim Result As New DialogResult
>> Result = MessageBox.Show("Save Work?", "Close Form",
>> MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
>> MessageBoxDefaultButton.Button1)
>>
>> If Result = DialogResult.Yes Then
>>         Me.Save()
>> End If
>>
>> Hovering over the DialogResult.Yes portion of the snippet, I am
>> "greeted" with the following warning:
>>
>> Access of shared member, constant member, enum member or nested type
>> through an instance; qualifying expression will not be evaluated.
>>
>> I *think* I understand the warning, but I don't understand how to
>> resolve it.  It runs just fine, but I won't feel right until I correct
>> the warning.
>>
>> What should I change?
>>
>> Will
>>
>>
Author
20 Jun 2006 2:52 PM
BK
First off, thanks to both you and Larry.  Last night I just wanted what
you provided (short, to the point, and a "fix").  It was late, I was
tired, etc.  Today, I'm glad to have the extended play version Larry
provided, something for me to use in one of my infamous weekly brown
bag sessions here at work!!!  Thanks for your help....


M. Posseth wrote:
Show quoteHide quote
> Hello Will
>
> You must type the full name of the constant cause it is shared across the
> project
>
> Windows.Forms.DialogResult.OK
>
> this warning is to protect you from accidently calling a method through an
> instance of a class
>
> so remember all shared members ( also sub routines and functions ) marked
> with the shared keyword or contained in a shared class should be called with
> there full name
>
>
>
> regards
>
> Michel Posseth [MCP]
>
>
>
>
>
> "BK" wrote:
>
> > Converting a rather large solution from 2003 to 2005.  All the errors
> > are worked out, but I'm trying to clean up the warnings as best I can.
> > The good news is that the application and it's supporting assemblies
> > are all working now.
> >
> > The followins snippet is from a base form class I use in the
> > application.  Pretty typical stuff, when a data entry form is closing
> > and if the user is editing data, I want to allow them to save their
> > work.  Here is part of the code:
> >
> > Dim Result As New DialogResult
> > Result = MessageBox.Show("Save Work?", "Close Form",
> > MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
> > MessageBoxDefaultButton.Button1)
> >
> > If Result = DialogResult.Yes Then
> >         Me.Save()
> > End If
> >
> > Hovering over the DialogResult.Yes portion of the snippet, I am
> > "greeted" with the following warning:
> >
> > Access of shared member, constant member, enum member or nested type
> > through an instance; qualifying expression will not be evaluated.
> >
> > I *think* I understand the warning, but I don't understand how to
> > resolve it.  It runs just fine, but I won't feel right until I correct
> > the warning. 
> >
> > What should I change?
> >
> > Will
> >
> >
Author
22 Jun 2006 7:53 PM
Frans_Clasener
I have the same warning on accessing a constant.
It stays when I select/change the suggestion (className.constName) from
the IDE ToolTip....
Seems like a bug.

Any ideas?

Best regards, Frans
Author
23 Jun 2006 8:49 AM
Larry Lard
Frans_Clase***@hotmail.com wrote:
> I have the same warning on accessing a constant.
> It stays when I select/change the suggestion (className.constName) from
> the IDE ToolTip....
> Seems like a bug.

Which class and constant?

--
Larry Lard
Replies to group please
Author
23 Jun 2006 2:06 PM
Frans_Clasener
Hi Larry,
--------------------------
Public Class AttmMainWin 'Generated by Form designer

'constant example, just beneath this code line

    Public Const treePathDelimiter As String = "."
--------------------------
'I access it from another object as
My.Forms.AttmMainWin.treePathDelimiter

'or, as suggested by the tooltip
AttmMainWin.treePathDelimiter

'both continue to prduce the warning and green squigly line....
----------------------------
Would love to se if you have any suggestions.

Thanks, Frans

Larry Lard wrote:
Show quoteHide quote
> Frans_Clase***@hotmail.com wrote:
> > I have the same warning on accessing a constant.
> > It stays when I select/change the suggestion (className.constName) from
> > the IDE ToolTip....
> > Seems like a bug.
>
> Which class and constant?
>
> --
> Larry Lard
> Replies to group please
Author
23 Jun 2006 2:44 PM
Larry Lard
Frans_Clase***@hotmail.com wrote:
Show quoteHide quote
> Hi Larry,
> --------------------------
> Public Class AttmMainWin 'Generated by Form designer
>
> 'constant example, just beneath this code line
>
>     Public Const treePathDelimiter As String = "."
> --------------------------
> 'I access it from another object as
> My.Forms.AttmMainWin.treePathDelimiter
>
> 'or, as suggested by the tooltip
> AttmMainWin.treePathDelimiter
>
> 'both continue to prduce the warning and green squigly line....
> ----------------------------
> Would love to se if you have any suggestions.

Good one. I suspect it's a bug; I have started another thread (titled
"VB2005 default instances; cannot refer to Form Const without a
warning") to discuss and confirm.

--
Larry Lard
Replies to group please