Summary of the VB. NET module (2)

Source: Internet
Author: User

 

'Lele1. VB file code:

Module mainclas
Sub main ()

Dim thefirstinstanceoftestscope as new testscope
Thefirstinstanceoftestscope. testscopesubmethodone ()
Dim astr as string = thefirstinstanceoftestscope. testscopefunctiongmethodone ()
Console. writeline (astr)

Dim thefirstinstanceofouttestscope as new outtestscope ()
'The same as above, instantiate the outtestscope class
'Dim thefirstinstanceofouttestscope as outtestscope
'Thefirstinstanceofouttestscope = new outtestscope ()
'The same as above, instantiate the outtestscope class
'Dim thefirstinstanceofouttestscope = new outtestscope ()
Thefirstinstanceofouttestscope. outtestscopesubmethodone ()
Dim aastr as string = thefirstinstanceofouttestscope. outtestscopefunctiongmethodone ()
Console. writeline (aastr)
Console. writeline (thefirstinstanceofouttestscope. CC)

Thefirstinstanceoftestscope. diaoyongw.hod () 'calls methods of other modules through class instances
Dim mtwoclassfiled as new mtwoclass
The mtwoclassfiled. mtwoclassmethod () module directly calls the methods in the classes of other modules.
The console. writeline (mthreemethod () 'module calls the methods of other modules directly.
Console. Readline ()

End sub
End Module

Class testscope
Dim A as string
Private B as string
Public C as string
Sub new () 'No parameter Constructor
Me. A = "dim_a"
Me. B = "private_ B"
Me. c = "public_c"
End sub
'Public static sub testscopemethodone () 'cannot use static to declare the sub method (or sub process). An error is prompted: The method cannot be declared as "static"
Public sub testscopesubmethodone ()
Console. writeline (string. Format ("testscopesubmethodone (): {0} {1} {2}", a, B, c ))
End sub
'Public static function testscopefunctiongmethodone () as string' 'cannot use static to declare the function method (or function). An error is prompted: The method cannot be declared as "static"
Public Function testscopefunctiongmethodone () as string
Return string. Format ("testscopefunctiongmethodone (): {0} {1} {2}", a, B, c)
End Function
Public sub diaoyongw.hod ()
'Mtwomethod ()
Console. writeline (mthreemethod ())
End sub
End Class

Module mtwo
'Public sub mtwomethod ()
Private mtwofiled as string = "mtwofiled"
The methods in the private sub mtwomethod () 'module can be modified in private mode, but they can only be used in the current module. They cannot be used in other modules.
Console. writeline (mtwofiled)
End sub
Class mtwoclass
Sub mtwoclassmethod ()
Mtwomethod ()
End sub
End Class
End Module

Module mthree
Private sub mprivatethreemethod ()
Console. writeline ("mprivatethreemethod ()")
End sub
'Public function mthreemethod () as string
Function mthreemethod () as string 'is shared by the method hermit, which can be accessed outside the module.
Mprivatethreemethod ()
Return "mthreemethod ()"
End Function
End Module

'In VB.net, static methods (sub and function) cannot be declared, and member variables cannot be declared. Static variables in methods (sub and function) can only be declared.
'Shared can be used to declare variables, declare methods, and declare member variables, which is just opposite to static.
'Shared in VB. Ne is more like static in C. The member variables (fields) and methods (sub and functions) declared with shared in VB.net can only be accessed by class names, but cannot be accessed by class instances.

'Multiple modules can be defined in one file.
'You can define a class in another file like C #. The usage of this class is the same as that of the class defined in a file (the package fields, methods, attributes, and so on)

'The method hermit sharing in the module can be directly accessed outside the module (other modules, classes, structures, etc.), and the addition of public is the same
'Methods in the module cannot be declared as protected, shared, or static. They can only be declared as public (default: public) and private.
'But the class (or structure) method in the module can be declared as public, private, protected, and shared.

Code in the 'outtestscope. VB file:

Public class outtestscope
Dim AA as string
Private BB as string
Public cc as string
Sub new () 'No parameter Constructor
Me. AA = "dim_aa"
Me. BB = "private_bb"
Me. Cc = "public_cc"
End sub
'Public static sub testscopemethodone () 'cannot use static to declare the sub method (or sub process). An error is prompted: The method cannot be declared as "static"
Public sub outtestscopesubmethodone ()
Console. writeline (string. Format ("outtestscopesubmethodone (): {0} {1} {2}", AA, BB, CC ))
End sub
'Public static function testscopefunctiongmethodone () as string' 'cannot use static to declare the function method (or function). An error is prompted: The method cannot be declared as "static"
Public Function outtestscopefunctiongmethodone () as string
Return string. Format ("outtestscopefunctiongmethodone (): {0} {1} {2}", AA, BB, CC)
End Function
End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.