The form cannot refer to itself through its default instance when shared

Consider the following code in a new WinForms.NET 4.0 application with default settings:

Public Class Form1 Private Sub AAA() Form1.AAA(Nothing) 'cannot refer to itself through its default instance; use 'Me' instead. End Sub Private Shared Sub AAA(str As String) End Sub End Class 

I get this error:

{FORM_CLASS_NAME} cannot refer to itself through its default instance; Use the ego instead.

I also get this warning on the same line:

Access to a common member, permanent member, enumeration member, or nested type through an instance; qualification expression will not be evaluated.

Assuming a default instance is used here, it ends with an endless loop - VS suggests changing Me.AAA() to Form1.AAA() and then back. AAA() works in both.

Converting Private Sub AAA() to Shared resolves the error. It seems that from the point of view of Microsoft, all overloads should be separated, if only one. Or you get this confusion by default. Why?

To clarify, I don't want to use the default instance here, just make a generic call.

If someone is faced with the same situation, consult.

+6
source share
4 answers

Creating an alias for a variable that has the same name as the type of the Form class is without a doubt the biggest problem with VB.NET. But it was necessary to give VB6 developers a chance to switch to VB.NET.

The workaround is to stop trying to be explicit about which method you want to call. This compiles fine and unambiguous, at least in your snippet:

  Private Sub AAA() AAA(Nothing) '' fine End Sub 

If it really, really hurts, then just replacing these two methods removes the ambiguity:

 Private Shared Sub AAA(str As String) End Sub Private Sub AAA() Form1.AAA(Nothing) '' fine End Sub 
+4
source

Can you get away from this? Your use will be very similar to Form1.AAA() vs. code.AAA() .

 Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load code.AAA() End Sub Private Class code Public Shared Sub AAA() End Sub End Class End Class 
+2
source

EDIT

Given the new information in the OP - another solution to your problem might be to use optional parameters - that is:

 Private Shared Sub AAA(Optional ByVal str As String = Nothing) 

Also - permission works in the "right" way, if you just change the order of declarations - this avoids the compiler error:

 Private Shared Sub AAA(ByVal str As String) End Sub Private Sub AAA() Form1.AAA(Nothing) End Sub 

-

Keeping this below, because it may be useful in other circumstances.

Perhaps your larger application did something like this - VB is full of the kind of mess you can get into. This will compile but crash:

 Public Class Form1 Private Shared Sub AAA() Form1.Text = "this" End Sub Private Sub Label1_TextChanged(sender As System.Object, _ e As System.EventArgs) _ Handles Label1.TextChanged Form1.AAA() End Sub End Class 

The same thing, it is actually "excellent" (I use this term freely) ...

 Public Class Form1 Private Shared dont As Boolean = True Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _ Handles MyBase.Load dont = False End Sub Private Shared Sub AAA() If Not dont Then Form1.Text = "this" End Sub Private Sub Label1_TextChanged(sender As System.Object, _ e As System.EventArgs) _ Handles Label1.TextChanged Form1.AAA() End Sub End Class 

This is because the text change handler will fire before Form1 finishes loading (i.e., during InitializeComponent() !) And will refer to the default instance that has not been created yet, so VB is trying to create a new one for so that you can call a generic method that rotates you in an infinite loop.

Oddly enough, the Load handler is "excellent" (again, free) to call Form1.AAA() inside - as in your initial code, because the default instance ( Form1 instance of the Form1 class) is completed at that moment, and the other will not be created to meet the call. Any other code path, however, begins in a common call and ultimately ends up touching any instance data, no matter how the torturous path spins and spins.

See also: Why is there a default instance for each form in VB.Net but not in C #?

+1
source

It is not clear what you are trying to accomplish as a whole. In OP Form1.AAA should be just AAA.

 Private Sub AAA() AAA(Nothing) End Sub Private Sub AAA(str As String) If str IsNot Nothing Then MsgBox(str) ' else ??? End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click AAA() AAA("hello") End Sub 
0
source

All Articles