???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? HENRY
????????????????????????? Han Rui
?
I started to admire the big Lee, and I am full of curiosity and understanding of VB.NET.
"In addition to the global variable, what is good?" I was forced to see the results.
"You can use shared members." Da Li is still so faint, "VB.NET is now supporting real object-oriented programming, inherits, use polymorphism, sharing members, and static members. Sharing members are all defined in all classes The method, attributes, fields, and events shared between the derived class. All objects created by the class can access the same data, share the implementation process, and receive the same excitation event. "
"So good, how to achieve it?" I seem to go back to the classroom, and I am ignorant.
"It's very simple, add the shared keyword after the public or private member modifier. Theshared keyword indicates one or more declared programming elements will be shared. The shared element does not associate a particular instance of a class or structure. You can access them by using the variable names of the class name or structure name or a specific instance of the class or structure. You can see this code.
?
Public Class ShareClass ??
?? Public Shared SharedValue As String ??
?? public shared sub shapemethod () ?????
????? msgbox ("this is a shared method.")???
?? End Sub
End classssub
Testshared () ??
?? Dim Shared1 as new shareclass () ?? 'creates an instance of the class.??
?? Dim Shared2 as new shareclass () ?? 'creates a second instance of the class.??
?? Shared1.sharedValue = "Share value 1" ??? 'assigns a value to the shared field.?
?? Shared2.sharedValue = "Share Value 2"? Rewrite the value of the shared field ??
?? ShareClass.ShareMethod ()? 'You can call the class without creating an instance.
End Sub
?
"There are two points, one is that if the shared member is a method or attribute, we can use the 'class name. Shared member' method without creating an instance; the second point, shared field is unique, you What is the value of shared1.sharesvalue after running? "
I have turned two laps in my mind. Since the shared field is the only one, then Shared1.SharedValue and Shared2.SharedValue are actually the same shared field, so I immediately said: "is Share Value 2. Because of Shared2 .SharedValue modified. "
"Yes!" Big Li once bloomed a smile, it is really the highest reward for me. "Then you still have to modify your program?"
it is good! Speaking of doing it. Since the shared properties are not instantiated when they call, it is best to implement it with a shared property. It is also possible to determine whether or not there is a new form instance if there is no destruction, if there is no existence, if there is a new form example, if there is, it is called:
Private shared m_vb6formdefinstance as form1
Public Shared Property Definstance () AS Form1
??? get
IF m_vb6formdefinstance is nothing orelse_
??????????? m_vb6formdefinstance.isdisposed the 'judgment whether the form instance exists ???????????????? M_VB6FORMDEFINSTANCE = New Form1 ()
??????? endiff
??????? definstance = m_vb6formdefinstance
???? End Get
???? set (byval value as form1)
???????? m_vb6formdefinstance = value
???? end set
End Property
?
Similar code is also added in Form2, but the FORM1 is changed to FORM2. Calling it is so convenient:
Code in Form1
Code in Form2
Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click
??????? Me.hide ()
??????? form2.definstance.show ()
End Sub
Private sub button1_click (Byval Sender AS _
System.Object, ByVal e as system.eventargs_
Handles button1.click
??????? me.hide ()
??????? form1.definstance.show ()
? End Sub
Private sub form2_closing (byval sender as object, _
ByVal e as system.componentmodel.canceleventargs)
? Handles mybase.closing
??????? form1.definstance.show ()
? End Sub
Hurry and run, no, the first form of text box is changed to "Henry's work", is it still disappeared? I looked up and looked at Da Li.
"If you think about it again, what is the first time it started?" Da Li was said.
"The first time launched is the FORM1 form for the main thread!" I muttered.
"So where is form1.definstance?" Da Li this sentence is really awakening people!
I immediately hand:
Module Module1
??? Sub main ()
??????? Application.run (Form1.definstance)?
??? End Sub
End module
And set Module1 to start the project. Run run ... Haha, everything is all right! I started to like VB.NET.
?
-------------------------------------------------- -------------
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com
Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/