Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reference to a non-shared member requires an object reference.

215 views
Skip to first unread message

michael

unread,
Sep 19, 2006, 10:59:03 AM9/19/06
to
I want to use the Immediate window during a debugging session to reveal the
value of a fully referenced and public attribute. However, when I type "?
Form1.dsTheDataSet.Tables("TheTable").DefaultView.RowFilter" I get the error
referenced in the Subject line above.

If I place the same attribute in a
Console.Writeline(Form1.dsTheDataSet.Tables("TheTable").DefaultView.RowFilter), the value of the attribute displays correctly in the output window.

How can I get this value to print in the Immediate window with an '?'
--
Michael Hockstein

Jeffrey Tan[MSFT]

unread,
Sep 19, 2006, 11:40:05 PM9/19/06
to
Hi Michael,

Since you are using ")" instead of "]" in your code, I assume you are using
VB.net language. Then can you tell me which version of Visual Studio you
are using, VS.net2003 or VS2005? Thanks.

Based on my review to you question, I think you should use the form's
reference to get the dsTheDataSet reference, instead of using the form's
class name. The *Form1* is the class name of your form, we can only use it
to refer the static/shared member of Form1 class, while "dsTheDataSet" is a
field of *Form1* class, so we should use the *Form1* class instance *Me* to
refer the "dsTheDataSet".

For testing purpose, I have added a typed dataset in VS.net2003 and
connected it to "jobs" table in "pubs" database in SQL Server. In
Form1_Load event, if I type the following code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Console.WriteLine(Me.DataSet11.Tables(0).DefaultView.RowFilter)
Console.WriteLine(Form1.DataSet11.Tables(0).DefaultView.RowFilter)
End Sub

I will get "G:\Program\winform\2006\9-20\WinformTestVB\Form1.vb(124):
Reference to a non-shared member requires an object reference." compilation
error message for the second code, but the first line code will compile
without any problem.

After removing the second line and debugging Form1_Load event, I tried the
following 2 commands in Immediate Command Window:

? Me.DataSet11.Tables(0).DefaultView.RowFilter
""
? Form1.DataSet11.Tables(0).DefaultView.RowFilter
Reference to a non-shared member requires an object reference.

As you can see, we will get the same error for using "Form1", while using
the form reference "Me" works well.

So you should type "?
Me.dsTheDataSet.Tables("TheTable").DefaultView.RowFilter" in Immediate
Command Window, and this will work for you.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

michael

unread,
Sep 20, 2006, 12:03:01 PM9/20/06
to
I can see how Me is the correct choice but only if the "Me" in question is in
fact the current object in the debugger. When I'm trying to issue the ?
command in the Immediate window, the debugger is stopped in a .vb file other
than the Form1.vb.

I'm using VS 2005. I may have made an incorrect assumption however I thought
that if you could navigate to the object that you wanted by using
Intellisense in the Immediate window, then you should be able to explore the
object's values.

For example, I have the debugger stop in a particular .vb file which uses
"Friend" visibility objects which were created in Form1. I open the Immediate
window, type "Form1" then a "dot" and Intellisense lets me navigate all the
way to: Form1.dsTheDataSet.Tables("TheTable").DefaultView.RowFilter. However,
even though Intellisense seems to know the way, when I hit the Enter key, I
get the error mentioned int he Subject line.

Maybe I'm not using the Immediate window as intended. However, the data is
available from the .vb file that the debugger is stopped in because I can use
Console.Writeline() to output the same information. I just can't seem to get
it through the Immediate window.

--
Michael Hockstein

Jeffrey Tan[MSFT]

unread,
Sep 21, 2006, 1:33:03 AM9/21/06
to
Hi Michael,

Thanks for the feedback of the debugger version!

Oh, yes, I originally used VS.net2003 debugger to do the test, so I am
seeing different behavior from you. By using the VB2005, I can reproduce
the behavior with the testing code below:

Public Class Form1


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

test.Test_method()
End Sub
End Class

Public Class test
Public Shared Sub Test_method()
Console.WriteLine(Form1.DataSet11.Tables(0).DefaultView.RowFilter)
End Sub
End Class

Yes, in VB2005 we can use the Form1(class name) to refer the dataset
instance without using the real Form1 reference(like Me keyword). This is a
new language enhancement in VB2005. However, we still can not use this
approach in the debugger "Immediate Window" or "Watch Window", below is the
confirmed test result:

From Watch Window:


Form1.DataSet11.Tables(0).DefaultView.RowFilter Reference to a non-shared
member requires an object reference.

From Immediate Window:
? me.DataSet11.Tables(0).DefaultView.RowFilter


""
? Form1.DataSet11.Tables(0).DefaultView.RowFilter
Reference to a non-shared member requires an object reference.

Below is the analysis details:
After some investigation, I find that the VB2005 compiler does the magic in
this issue. If we are using *Form1* class name instead of form reference in
the code, the VB2005 compiler will emit an extra "ImmediateWindowTest.My"
namespace (my testing application's name is "ImmediateWindowTest") which
contains several compiler synthesized hidden classes:
MyApplication
MyComputer
MyProject
MySettings
MySettingsProperty

These classes are used to support the VB2005 language enhancement. For
example, ImmediateWindowTest.My.MyComputer class is used to support
My.Computer.Audio.Play() usage in VB2005. So was the
Form1.DataSet11.Tables(0).DefaultView.RowFilter usage in our source code.

If we use Reflector to view the compiled code of Test_method(), we will get
the following code:

Public Shared Sub Test_method()

Console.WriteLine(MyProject.Forms.Form1.DataSet11.Tables.Item(0).DefaultView
.RowFilter)
End Sub

So the *Form1* class name in our code is actually redirected to
"ImmediateWindowTest.My.MyProject.Forms" shared property, which is emitted
dynamically by VB2005 compiler. Using Reflector to view this shared
property, we will get this:

<HelpKeyword("My.Forms")> _
Friend Shared ReadOnly Property Forms As MyForms
Get
Return MyProject.m_MyFormsObjectProvider.GetInstance
End Get
End Property

So this "Forms" shared property returns a MyProject.MyForms class instance.
"MyForms" is another internal class included in "MyProject" class, if you
use Reflector to naviate to "MyProject.MyForms" class, you find there is an
extra "Form1" shared property which is listed below:

Public Property Form1 As Form1
Get
Me.m_Form1 = MyForms.Create__Instance__(Of Form1)(Me.m_Form1)
Return Me.m_Form1
End Get
Set(ByVal Value As Form1)
If (Not Value Is Me.m_Form1) Then
If (Not Value Is Nothing) Then
Throw New ArgumentException("Property can only be
set to Nothing")
End If
Me.Dispose__Instance__(Of Form1)(Me.m_Form1)
End If
End Set
End Property

Now, the VB2005 implementation is clear: VB2005 compiler will synthesize
some hidden internal classes to contain the Form reference in "m_Form1"
field, so that we can leverage *Form1* class name to access the member
field of the Form1 class. All the magic is taken care of by VB2005 compiler
while generating the assembly.

Let's turn our attention to the original issue. Why the VB2005 debugger can
not recognize and use the *Form1* magic? This is because both the "Watch
Window" and "Immediate Window" will execute what we typed code in the
context of the debugging application runtime. Since this is runtime, VB2005
has not chance to detect *Form1* class name and redirect it into
*MyProject.Forms.Form1* shared property to obtain the form reference. The
runtime debugger will only interpret the code directly, so it will treat
*Form1* only as class name, which generates "Reference to a non-shared

member requires an object reference".

Based on our analysis, we can see this behavior is by design. Without the
magic help from VB2005 compiler, it is hard for the VB2005 runtime debugger
to recognize the "Form1" should be redirected to *MyProject.Forms.Form1*
shared property.

Hope this information helps.

michael

unread,
Sep 22, 2006, 8:32:01 AM9/22/06
to
Thanks for the very detailed response. Probably one of the best replies I've
ever had from a post! Thanks

If I understand correctly (and getting to the point), I should use
MyProject.Forms.Form1... from withing the Immediate window to explore objects
outside of the .vb file that I'm currently paused in within the debugger.

However, I can't get Intellisense to recognize MyProject. When a manually
type a command such as the following I get another error:

? MyProject.Forms.Form1.text
'MyProject.Forms.Form1.text' is not declared or the module containing it is
not loaded in the debugging session.

Maybe I'm missing the whole point of your explanation.

Thanks for your patience

--
Michael Hockstein

Jeffrey Tan[MSFT]

unread,
Sep 24, 2006, 11:32:32 PM9/24/06
to
Hi Michael,

Glad to see my work can help you.

Your further question reveals some other deeper things, see the below
analysis:

Yes, in my testing VB2005 project, I also can not use
MyProject.Forms.Form1.DataSet11.Tables(0).DefaultView.RowFilter in
Watch/Immediate Window to let the debugger recognize the property. It looks
strange at this moment.

Further investigation makes me to concentrate on the
HideModuleNameAttribute marked on "MyProject" class(from Reflector):
<HideModuleName, StandardModule, GeneratedCode("MyTemplate", "8.0.0.0")> _
Friend NotInheritable Class MyProject
..

From MSDN link below, we know that HideModuleNameAttribute will hide the
"MyProject" class name in the usage(please see the Example in below link):
http://msdn2.microsoft.com/en-US/library/microsoft.visualbasic.hidemodulenam
eattribute.aspx

Also, inside the "MyProject" class, we can see the HelpKeywordAttribute is
marked for the "Forms" shared property:


<HelpKeyword("My.Forms")> _
Friend Shared ReadOnly Property Forms As MyForms

Now, this again confirms that we can(or must) really eliminate "MyProject"
class name between "My" namespace and "Forms".

To prove my analysis, I used the "My.Forms" representation to pass to the
VB2005 debugger, I got the following output:
? My.Forms.Form1.DataSet11.Tables(0).DefaultView.RowFilter
""

Oh, yes, we got the correct syntax.

You may apply the same logic to test the statement below:
? My.Forms.Form1.dsTheDataSet.Tables("TheTable").DefaultView.RowFilter

Hope this helps.

michael

unread,
Sep 27, 2006, 11:52:01 AM9/27/06
to
OK! So, I tried the My.Forms prefix and so far so good. You really worked
overtime on this one and I really appreciate it!!!
--
Michael Hockstein


""Jeffrey Tan[MSFT]"" wrote:

> Hi Michael,
>
> Glad to see my work can help you.
>
> Your further question reveals some other deeper things, see the below
> analysis:
>
> Yes, in my testing VB2005 project, I also can not use
> MyProject.Forms.Form1.DataSet11.Tables(0).DefaultView.RowFilter in
> Watch/Immediate Window to let the debugger recognize the property. It looks
> strange at this moment.
>
> Further investigation makes me to concentrate on the
> HideModuleNameAttribute marked on "MyProject" class(from Reflector):
> <HideModuleName, StandardModule, GeneratedCode("MyTemplate", "8.0.0.0")> _
> Friend NotInheritable Class MyProject

> ...

0 new messages