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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
Jina AI
Jina AI
博客园_首页
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
N
News | PayPal Newsroom
S
Security Archives - TechRepublic
量子位
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
C
Cisco Blogs
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
Scott Helme
Scott Helme
S
Securelist
Security Latest
Security Latest
爱范儿
爱范儿
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
L
LINUX DO - 最新话题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
月光博客
月光博客
T
Tailwind CSS Blog
Cloudbric
Cloudbric
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
D
DataBreaches.Net
博客园 - 【当耐特】
有赞技术团队
有赞技术团队

博客园 - 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 - IT 笔记 using vb.net read unix style text file How to install autotrace in fontforge My first postscript program .net 2 config - IT 笔记 A generic winform skeleton support interactive UI during large job process
Surprise vb.net WinForm don't require instance to access the instance value
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.