An example.
Value:
And finally we have the Value() Function, which returns an expression based on a field.
New:
In the Main entry point, an instance of Example is created and Value() is invoked.
Private, Public:
A private member cannot be accessed outside the class.
Based on: .NET 4.5
Private _value As Integer
_value = 2
End Sub
Return _value * 2
End Function
End Class
Sub Main()
Dim x As Example = New Example()
Console.WriteLine(x.Value())
End Sub
End Module
Here:
This example uses a formal argument in the Class constructor (Sub New).
Tip:
Often classes will have argument validation—for example,
VB.NET program that uses MyClass
Private _name As String
MyClass._name = name
Console.WriteLine(name)
Console.WriteLine(_name)
End Sub
End Class
Sub Main()
Dim p As Perl = New Perl("Sam")
End Sub
End Module
Sam
Sam
Class A:
Let's begin by looking at Class A: this class contains a field (_value) as well as a Sub (Display()).
Class B, C:
Class B and Class C both use the Inherits keyword and are derived from Class A.
Tip:
You can see the New B() and New C() will do slightly different things when called.
VB.NET program that uses Inherits keyword
Public _value As Integer
Console.WriteLine(_value)
End Sub
End Class
Public Sub New(ByVal value As Integer)
MyBase._value = value
End Sub
End Class
Public Sub New(ByVal value As Integer)
MyBase._value = value * 2
End Sub
End Class
Sub Main()
Dim b As B = New B(5)
b.Display()
c.Display()
End Sub
End Module
10
Tip:
This eliminates any possible ambiguity between fields. Two classes can have fields with identical names.
Thus:
When b.Display() and c.Display() are called, the A.Display Sub is invoked.
Interface
Meanwhile:
A base class is a core template of data and functionality. Base classes are not the same as interfaces.
Note:
With Inherits, we implement complex object models that can closely represent,
And:
A MustInherit Class is essentially a template that is part of the classes that inherit from it.
MustInherit
Tip:
A Public Shared field can be used in the same way as a global variable. This is useful for storing settings.
Shared
VB.NET program that uses Shared field
Public Shared _v As Integer
End Class
Sub Main()
Test._v = 1
Console.WriteLine(Test._v)
Test._v = 2
Console.WriteLine(Test._v)
End Sub
End Module
2
Nothing:We compare references to Nothing. The "Is" and "IsNot" operators are most often used with the Nothing constant.
Here:In this example, we see how "IsNot Nothing" and "Is Nothing" are evaluated with a local variable.
Tip:This pattern of code is sometimes useful. It helps if you are not sure the variable is set to something.
VB.NET program that uses Is, IsNot operators Module Module1 Sub Main() Dim value As String = "cat" ' Check if it is NOT Nothing. If value IsNot Nothing Then Console.WriteLine(1) End If ' Change to Nothing. value = Nothing ' Check if it IS Nothing. If value Is Nothing Then Console.WriteLine(2) End If ' This isn't reached. If value IsNot Nothing Then Console.WriteLine(3) End If End Sub End Module Output 1 2Is, IsNot notes. These operators can only be used with reference types. The Nothing constant is a special instance of a reference type. We cannot use the Is-operator to perform casting.
Note:These are most commonly used with the Nothing constant. But any two references can be compared.
And:The result depends on the memory locations—not the object data the references point to.
Partial. This modifier specifies that a class is specified in multiple declarations. With Partial, were open a class and add new parts to it. A Class can span multiple files.
Friend. This modifier makes a member (like a Class, Sub or Function) unavailable outside the present assembly. An assembly is physical file that contains compiled code.
Here:We specify that Display() is a Friend Sub. So it is Public inside the assembly, but not available outside.
VB.NET that uses Friend Class Item Friend Sub Display() Console.WriteLine("Friend Class used") End Sub End Class Module Module1 Sub Main() ' The Display Sub is public if we are in the same assembly. Dim local As Item = New Item() local.Display() End Sub End Module Output Friend Class usedObject. All classes inherit from Object, the ultimate base class. We thus can cast all variables to an Object. The object class provides some helpful methods.
Equals:This compares the contents of two objects. Implementations from derived classes are used.
GetType:This returns a Type object based on the type of the current object. GetType is similar to VarType.
GetHashCode:Returns a hash code integer based on the content of the instance. Derived implementations are often present.
ToString:Converts an Object to a string representation. The exact string returned varies by implementation.
VB.NET that uses Object Class Example ' An empty Example class: it automatically inherits from Object. End Class Module Module1 Sub Main() ' Create instance of the Example class. Dim x As Example = New Example() ' The class inherits from Object, so we can cast it to an Object. Dim y As Object = x ' ... This method is from Object. Console.WriteLine(y.GetHashCode()) ' When we call GetHashCode, the implementation from Object is used. Console.WriteLine(x.GetHashCode()) End Sub End Module Output 46104728 46104728VarType. This function is equivalent to the GetType function. It returns a Type reference for an object instance. Its syntax is different from GetType's.
Modules. A Module has shared data. The Main Subroutine is found in a Module. All fields in a Module are shared, meaning they are not part of an instance. Modules are not types.
Namespaces. With namespaces, we have another way to organize programs. Namespaces contain other types like classes, but are not objects.
發表評論 評論 (0 個評論)