[VB.NET] Implicit Form Object-My.Forms

2 minute read

Get closer to the mystery of My.Forms

First, create a VB.NET WinForms project and write code like the one below.
(Form1 has two buttons, Button1 and Button2, and I have set the Click event handler in the designer)

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Test.HelloWorldA(Me)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Test.HelloWorldB()
    End Sub

End Class

Public Class Test
    Public Shared Sub HelloWorldA(frm As Form1)
        frm.Text = "Hello World! A"
    End Sub

    Public Shared Sub HelloWorldB()
        Form1.Text = "Hello World! B"
    End Sub

End Class

The HelloWorldB method doesn’t pass an instance of Form1 and for some reason it looks like it’s directly accessing the Form1.Text property where it hasn’t been instantiated.

At first glance it looks like a compile error, but strangely this code doesn’t actually cause a compile error, and clicking Button1 gives “Hello World! A” and Button2 gives “Hello World! B”. The title will switch.

In Visual Studio, hover your mouse cursor over Form1 of HelloWorldB and you should see something like this:
VisualStudio表示

By the way, when I decompile to C # with ILSpy, it looks like this:
decomp.jpg

A namespace called WindowsApp1.My and a class called MyOO are created in it.
Inside the MyProject class is a static property called Forms, inside which is the Form1 property.
Looking at the code for the HelloWorldB method in C #,

MyProject.Forms.Form1.Text = "Hello World! B";

It has become. VB.NET is creating an instance of a form in My.MyProject.Forms without permission
[Form class name.property]
If you write, it will omit My.MyProject.Forms and interpret it as accessing it. ~~ Do something completely extra. ~~

After hunting for related documentation, I found the following: It seems that there are various other My objects. If you are not good at English, please use Google Translate.
My.Forms Object
My.Internals: Examining the Visual Basic My Feature
Objects implicitly instantiated in vb.net?

Problems with My.Forms

VB.NET is launched directly from the Form with the default project settings, in this case an instance of My.Forms is used, but like C #, create a Main method and change the startup object in the project settings. And suppose you create a form by launching it from the Main method.

Public Class Program

    <STAThread>
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form1)
    End Sub

End Class

What happens if you click Button2 on Form1 now? The title of the form is not set to “Hello World! B”.
The Form1 created by the Main method and the Form1 instance of MyProject.Forms.Form1 are different, and the one currently displayed on the screen is the Form1 created by the Main method. After updating the Text property of MyProject.Forms.Form1, the title of the form doesn’t change, right?

The mainstream of .NET development is C #, and VB.NET seems to stop developing and I think it will disappear in the future, but when porting existing VB.NET code to C #, implicit like My.Forms Since the object of is an element that C # does not have, if there is a reference part, it may be addictive, so be careful.