Vb. NET class Summary (i)

Source: Internet
Author: User
Tags instance method
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 ' acts like private A as String. The field of the class (also known as the member variable) defaults to private and instance types
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 declare a sub method (or Sub procedure) with Static, prompting an error: Method cannot be declared "Static"
Public Sub Testscopesubmethodone () is not accessible 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 (in fact, static members)
Dim testscopefiled as New testscope
Testscopesharedsubmethodone (testscopefiled)
End Sub
The ' Public Static function Testscopefunctiongmethodone () as String ' cannot declare a function method (or function functions) with Static, prompting an error: The method cannot be declared as " Static "
The 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: an instance of the class cannot be referenced from a shared method or a shared member initializer without an explicit instance of the class Rapporteur
' This error is the same as C #, and C # allows instance methods to access static methods and static fields, but C # does not allow static methods in classes and structs to access instance methods and instance fields in the same class or struct, VB. NET also complies with this rule.
Filedone.a = "Call the instance field of the Testscope class in the form of a pass parameter" but can invoke instance fields and instance methods with an instance of the class as a parameter to the method
Dim sharedmethodfiled as String = Filedone.testscopefunctiongmethodone () ' But you can call instance fields and instance methods with an instance of the class as a parameter to the method
Console.WriteLine ("{0}/{1}", Filedone.a, sharedmethodfiled)
' Fileone.testscopesubmethodone ()
Console.WriteLine ("Testscopefunctiongsharedmethodone")

Dim anotherfiled as New Testscope ' can also invoke instance fields of the class by instantiating the class in a static method
Console.WriteLine (anotherfiled) ' This method prints the object with an overloaded ToString method

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 the following--------------
' Testscopesubmethodone ():D im_a private_b Public_c
' Private shared_d
' Invokes an instance field of the Testscope class in the form of a pass parameter/testscopefunctiongmethodone (): Invokes an instance field of the Testscope class in the form of a pass parameter Private_b Public_c
' Testscopefunctiongsharedmethodone
' Dim_a::P rivate_b::P ublic_c
' Testscopefunctiongmethodone ():D im_a private_b Public_c

' ''''----------


' A little summary '
' You cannot use static to declare methods (sub and function) in vb.net, and you cannot declare member variables, only to declare static variables in methods (sub and function).
' Shared can be used to declare a variable or to declare a method, or to declare a member variable, which is just the opposite of static.
' Shared in Vb.ne is more like a static role in C #. The member variables (fields) and methods (sub and function) declared in vb.net with shared declarations can only be accessed using the class name, not with instances of the class

' Infinite loops and infinite recursion can result in an unhandled ' system.stackoverflowexception ' type of exception appearing in mscorlib.dll

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.