Results 1 to 11 of 11

Thread: Reference to a non-shared member requires an object reference

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Reference to a non-shared member requires an object reference

    I'm getting this message in the IMMEDIATE window when I try to look at the value of public variables from my main form.

    Main form - CMCConsole

    Code:
    Public Class CMCConsole
    .
    .
    .
        Public AddNewCaseClear As Boolean = True
        Public AddClientId As String = ""
        Public AddCaseType As String = ""
    And I pop-up a dialog form...

    Code:
    AddCase.ShowDialog(Me.casePanel)
    In AddCase_Load - I cannot examine CMCConsole.AddNewCaseClear

    Code:
        Private Sub AddCase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Text = "Add " & CaseType.Text
            If CMCConsole.AddNewCaseClear Then
                ClientId.Text = ""
                MatterNumber.Text = ""
                MatterNumberLst.Items.Clear()
                PatentApp.Text = ""
                PatentAppLst.Items.Clear()
            End If
            CMCConsole.AddNewCaseClear = False
        End Sub
    Code:
    ?CMCConsole.AddNewCaseClear
    Reference to a non-shared member requires an object reference.
    But it steps into the IF statement without error - so the boolean is TRUE.

    What's going on????

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Reference to a non-shared member requires an object reference

    Just to clarify - the code runs OK if you just run straight through, but you get an error in the immediate window if break the run and try and access the object?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    You're referencing the default instance in code, which is not supported in the Immediate window. You should be using Me in the immediate window. You should be using Me in code too. It doesn't really make sense for a form class to reference its own default instance. Frankly, you won't find many experienced VB.NET folk around here who like default instances at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Reference to a non-shared member requires an object reference

    I was in a different form - I thought ME was a reference to the "current" form you were in.

    Your saying that CMCConsole - the main form in the APP - is always called ME?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    Ah, sorry. I didn't notice it was a different form. Now I look at the code again it's obvious.

    That said, the simple fact is that default instances are not supported in any of the debugging windows. That may be because default instances are a bit of VB smoke and mirrors that's not supported in the non-VB-specific debugger, or it may be because default instances are thread-specific and debugging windows are inherently running in a different thread.

    You simply can't access default instances in debugging windows. You can't even access them in the Watch window. If you structure your code to avoid the use of default instances, as you'd have to in C# and other languages anyway, then you can access your data in the same way in code and in the debugger.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Reference to a non-shared member requires an object reference

    This is why the default instance sucks. It's not that it is a fundamentally 'bad' thing, but that it is a source of confusion, even for experienced programmers (and much worse for those just getting into it). Every other object has to be created, but SURPRISE...not this!

    People can learn to live with good or bad. It is inconsistency that we struggle with, and the default instance is introduced inconsistency.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    I'm with Shaggy. Default instances were introduced to make forms "work" the same way in VB.NET as they do in VB6, i.e. you can just access then inherently anywhere without the need to create an instance. The problem is, people use them without knowing what they are and then get confused, e.g. they create and display an explicit instance and then try to update a control using the default instance and wonder why it doesn't "work". Default instances simply make it easier for beginners to use a form without understanding how objects work. They simply can't use VB for anything useful without understanding that, so why give them the impression that form's are somehow different to other objects? Funnily enough, default instances were introduced mainly for beginners, yet it's those who are more experienced who can put them to more effective use, yet those people more often don't really like them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Reference to a non-shared member requires an object reference

    I'm in so many different languages...

    Argh!

    I still don't understand.

    This is my DIALOG FORM

    Code:
    Imports System.Windows.Forms
    
    Public Class VendorLookupPopUp
    
        Public Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As IntPtr) As Integer
    
        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            If VendorStatusChk.Checked Then
                'CMCConsole.VLvendorid = "0"
                CMCConsole.VLvendorstatus = "X"
            Else
                If VendorLookupLV.SelectedItems.Count > 0 Then
                    Dim vi As String = VendorLookupLV.SelectedItems(0).Tag.ToString
                    CMCConsole.VLvendorid = vi
                End If
            End If
            Me.Close()
        End Sub
    
        Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub
    ME refers to this dialog form - I can see that because the "auto-generated" OK and CANCEL events refer to Me.DialogResult.

    Should I be putting those PUBLIC variables in a MODULE and not referring to them as part of the main form?

    Is that better practice?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reference to a non-shared member requires an object reference

    Me always refers to the current instance no matter where you are. I was wrong about you using Me before because I thought you were in the same form as the default instance you were referring to.

    If you are going to use the default instance of a form anywhere in your code, e.g. as you are to set properties of the CMCConsole form, then you should be using it everywhere in your code, i.e. you should be displaying the form like this:
    vb.net Code:
    1. CMCConsole.Show()
    rather than like this:
    vb.net Code:
    1. Dim instance As New CMCConsole
    2.  
    3. instance.Show()
    The default instance is one that VB creates for you when you use the class name as though it was a variable. If no default instance currently exists then one is created. Note that if you explicitly create an instance then that is a different object to the default instance. That's why, as I say, people get confused when they create an instance and display, then update some control on the default instance and they don't see it. The instance they're looking at is not the default instance so changing the default instance doesn't affect what they see.

    There's nothing really wrong with using default instances but, as I've said, they are not supported by the debugging windows, e.g. Immediate, so you cannot get any information about that instance unless you are currently executing code in that class and you use Me to reference members. If you stop at a breakpoint in Form1 then you cannot get any information about the default instance of Form2 in the Watch or Immediate window.

    Think about how you would do this with objects other than forms. They have no default instances so you couldn't even try that. How would you accomplish what you want in that case? You can do the same with your forms. They are objects just like any other, despite how default instances may make them appear.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Reference to a non-shared member requires an object reference

    No, leaving them where you have them is ok. The issue is this:

    Dim nf as New CMCConsole

    will fail for you, because in the Add_Case form, you reference CMCConsole, rather than nf. CMCConsole is a type of form, but starting with .NET 2005, it is also an instance of a form of type CMCConsole. Your AddCase form will ONLY reference that one default instance of the form. As long as you never create a new instance of CMCConsole, as I did in that one line, then this should work ok (except that you won't be able to use some debugging tools). That's dangerous, because your AddCase form is not tied explicitly to just one specific instance of CMCConsole, and woe unto you if you ever make a different instance.

    A better alternative would be to pass the CMCConsole in to the constructor of the AddCase form.
    Code:
    Private mOwner as CMCConsole
    Public Sub New AddCase (Owner as CMCConsole)
      mOwner = Owner
    End Sub
    Then you replace all instances of CMCConsole in AddCase with mOwner. Now, AddCase will be refering to the specific instance of CMCConsole that was passed to it, rather than the one, global, confusing, default instance.

    Of course, this works best if you create and show the AddCase as an instance, as well:
    Code:
    Dim nf as New AddCase(Me)
    
    nf.ShowDialog(Me.Panel)
    My usual boring signature: Nothing

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Reference to a non-shared member requires an object reference

    I really shouldn't have started like that. I should have realized that somebody would slip a post in between mine and what I was replying to.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width