惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
I
InfoQ
B
Blog RSS Feed
D
DataBreaches.Net
S
SegmentFault 最新的问题
P
Proofpoint News Feed
A
About on SuperTechFans
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
小众软件
小众软件
博客园 - Franky
有赞技术团队
有赞技术团队
D
Docker
T
Tailwind CSS Blog
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
V
Visual Studio Blog
宝玉的分享
宝玉的分享
爱范儿
爱范儿

博客园 - IT 笔记

Subversion Revert File will not be shown in Modification Properties.Settings show error 'System.Windows.Forms.PropertyStore' does not contain a definition for 'Settings' Add Image in MS Report RDLC by using Embedded Image 王爽汇编 Lab 13 Question 1, 用两个程序实现 Spring.NET Installation and setup the first program 王爽汇编语言 实验11 王爽汇编语言 检测点11.2 王爽 汇编语言程序课程设计1 王爽 汇编语言程序设计实验10,题三,数值转换。(Assembly experiment Convert HEX to Decimal and Show on screen ) 王爽 汇编语言程序设计 实验9 (Assembly Language Study) 王爽 汇编语言程序设计 检测点9.1 第2题 The NTVDM CPU has encountered an illegal instruction. CS:0006 IP:130a .... select files by multiple extension name Access Denied when running Spring.IocQuickStart.MovieFinder using vb.net read unix style text file How to install autotrace in fontforge My first postscript program .net 2 config A generic winform skeleton support interactive UI during large job process
Surprise vb.net WinForm don't require instance to acc...
IT 笔记 · 2014-05-08 · via 博客园 - IT 笔记

I was very surprise my colleage had the code below and it is working. I thought it should not be able to compile. 

See the line below, the Fom1 is class name instead of instance, and myName is also instance field.

MessageBox.Show(Form1.myName)
Public Class Form1
    Public myName As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myName = "Bin"
        Dim cl1 = New Class1()
        cl1.ShowFormPublicVariable() 
    End Sub 
End Class

Public Class Class1
    Public Sub ShowFormPublicVariable()
        MessageBox.Show(Form1.myName)
    End Sub
End Class

Checked microsoft that from visual studio 2005 it has put a default instance for winform.

A default instance is an instance of the class that is provided by the runtime and does not need to be declared and instantiated using the Dim and New statements. The following example demonstrates how you might have declared and instantiated an instance of a Form class called Form1, and how you are now able to get a default instance of this Form class through My.Forms.

' The old method of declaration and instantiation 
Dim myForm As New Form1
myForm.show()
' With My.Forms, you can directly call methods on the default  
' instance()
My.Forms.Form1.Show()

But some more funny thing can go, like code below messagebox show "Bin"

Public Class Form1
    Public myName As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myName = "Bin"
        Dim form1b As New Form1
        form1b.myName = "Yang" 
        Dim cl1 = New Class1()
        cl1.ShowFormPublicVariable() 
    End Sub 
End Class

Public Class Class1
    Public Sub ShowFormPublicVariable()
        MessageBox.Show(Form1.myName) 
    End Sub
End Class

And if the code like below, the message show empty "".

Public Class Form1
    Public myName As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Dim form1b As New Form1
        form1b.myName = "Yang" 
        Dim cl1 = New Class1()
        cl1.ShowFormPublicVariable() 
    End Sub 
End Class

Public Class Class1
    Public Sub ShowFormPublicVariable()
        MessageBox.Show(Form1.myName) 
    End Sub
End Class

if the code above changed to Console and load the form from console, then the code can not be compiled it shows the error "Error 1 Reference to a non-shared member requires an object reference. " at "Form1.myName".

The behaviour is by desgin by My.Form, below is some explanation for My.Forms.

The My.Forms object provides an instance of each form in the current project. The name of the property is the same as the name of the form that the property accesses. For information about adding forms to a project, see How to: Add Windows Forms to a Project.

You can access the forms provided by the My.Forms object by using the name of the form, without qualification. Because the property name is the same as the form's type name, this allows you to access a form as if it had a default instance. For example, My.Forms.Form1.Show is equivalent toForm1.Show.

The code show message "Bin" , then "Yang".

Public Class Form1
    Public myName As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myName = "Bin"
        Dim form1b As New Form1
        form1b.myName = "Yang" 
        Dim cl1 = New Class1()
        cl1.ShowFormPublicVariable(form1b)
    End Sub 
End Class

Public Class Class1
    Public Sub ShowFormPublicVariable(ByVal c1 As Form1)
        MessageBox.Show(Form1.myName)
        MessageBox.Show(c1.myName) 
    End Sub
End Class

So "Form1" the class name is actually pointed to a default instance capture in My.Forms.