Programming 笔记

工作中遇到的问题就记载这里

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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.

 

posted on 2014-05-08 09:44  IT 笔记  阅读(304)  评论(0编辑  收藏  举报