|
-
Jun 28th, 2009, 05:56 AM
#1
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????
-
Jun 28th, 2009, 07:13 AM
#2
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?
-
Jun 28th, 2009, 08:02 AM
#3
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.
-
Jun 28th, 2009, 08:16 AM
#4
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?
-
Jun 28th, 2009, 08:32 AM
#5
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.
-
Jun 28th, 2009, 08:57 AM
#6
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
 
-
Jun 28th, 2009, 09:05 AM
#7
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.
-
Jun 28th, 2009, 09:08 AM
#8
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?
-
Jun 28th, 2009, 09:25 AM
#9
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:rather than like this:
vb.net Code:
Dim instance As New CMCConsole 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.
-
Jun 28th, 2009, 09:32 AM
#10
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
 
-
Jun 28th, 2009, 09:33 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|