Surprise vb.net WinForm don't require instance to access the instance value

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.

VB
 
' The old method of declaration and instantiation 
Dim myForm As New Form1
myForm.show()
VB
 
' 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.

 

转载于:https://www.cnblogs.com/yangbin990/p/3715485.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值