Module module1
Sub main ()
Dim thefirstinstanceoftestscope as new testscope
Thefirstinstanceoftestscope. testscopesubmethodone ()
'Testscope. testscopesubmethodone ()
Dim astr as string = thefirstinstanceoftestscope. testscopefunctiongmethodone ()
Console. writeline (astr)
Console. Readline ()
End sub
Class testscope
Dim A as string' works the same as private A as string. The fields (also called member variables) of the class are private and instance types by default.
Private B as string
Public C as string
Private shared D as string
Sub new () 'No parameter Constructor
Me. A = "dim_a"
Me. B = "private_ B"
Me. c = "public_c"
Testscope. d = "Private shared_d"
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 () 'cannot be accessed outside the class if it is changed to private
Console. writeline (string. Format ("testscopesubmethodone (): {0} {1} {2}", a, B, c ))
Console. writeline (testscope. d) 'instance method can access Shared members (actually static members)
Dim testscopefiled as new testscope
Testscopesharedsubmethodone (testscopefiled)
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 'cannot be accessed outside the class if it is changed to private
Return string. Format ("testscopefunctiongmethodone (): {0} {1} {2}", a, B, c)
End Function
Public shared sub testscopesharedsubmethodone (byval filedone as testscope)
'Return string. format ("testscopefunctiongmethodone (): {0} {1} {2}", a, B, c) 'error: no explicit instance of the class, you cannot reference the instance members of this class from the shared method or shared Member initial value setting.
'The error is the same as that of C #. C # allows the instance method to access static methods and static fields, however, C # does not allow static methods in the class and structure to access the instance methods and instance fields in the same class or structure. net also follows this rule.
Filedone. A = "Call the testscope class instance field in the form of passing Parameters" 'but you can use the instance of this class as the method parameter to call the instance field and instance method
Dim sharedmethodfiled as string = filedone. testscopefunctiongmethodone ()'
Console. writeline ("{0}/{1}", filedone. A, sharedmethodfiled)
'Fileone. testscopesubmethodone ()
Console. writeline ("testscopefunctiongsharedmethodone ")
Dim anotherfiled as new testscope 'You can also call the instance field of the class by instantiating the class in the static method.
Console. writeline (anotherfiled) 'This method will use the overloaded tostring method to print this object
End sub
Public overrides function tostring () as string
'Return mybase. tostring ()
Return string. Format ("{0 }::{ 1 }:{ 2}", a, B, c)
End Function
End Class
End Module
''''' Print as follows --------------
'Testscopesubmethodone (): dim_a private_ B public_c
'Private shared_d
'Call the testscope class instance field/testscopefunctiongmethodone () by passing the parameter: Call the testscope class instance field private_ B public_c
'Testscopefunctiongsharedmethodone
'Dim _ A: private_ B: public_c
'Testscopefunctiongmethodone (): dim_a private_ B public_c
'''''----------
'Summary"
'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.
'Infinite loops and infinite recursion will cause the unprocessed "system. stackoverflowexception" type exceptions to occur in mscorlib. dll.