VB.Net/Class/ReadOnly
Содержание
Define and use Properties: ReadOnly
Imports System
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim obj As New MyOwnClass("Firstobject")
Dim x As Integer
Console.WriteLine("This object is: " & obj.Name)
Console.WriteLine("Other objects are: ")
For x = 0 To MyOwnClass.OtherObjectCount - 1
If x <> obj.MyIndex Then
Console.WriteLine(obj(x).Name)
End If
Next
End Sub
End Class
Class MyOwnClass
Implements IDisposable
Private Shared myclasses As New ArrayList()
Public Name As String
Public Sub New(ByVal myname As String)
MyBase.New()
MyClasses.Add(Me)
Name = myname
End Sub
Default Public ReadOnly Property OtherMyClassObjects(ByVal idx As Integer) As MyClass
Get
Return CType(MyClasses.Item(idx), MyClass)
End Get
End Property
Public Shared ReadOnly Property OtherObjectCount() As Integer
Get
Return myclasses.Count
End Get
End Property
Public ReadOnly Property MyIndex() As Integer
Get
Return myclasses.IndexOf(Me)
End Get
End Property
Public Sub Dispose() Implements IDisposable.Dispose
MyClasses.Remove(Me)
End Sub
End Class
Define and use ReadOnly Class Property
Imports System
Imports System.IO
Public Class MainClass
Shared Sub Main()
Dim e As New Employee("Joe", 100000)
Console.WriteLine(e.Name & " has salary " & e.Salary)
e.RaiseSalary(0.2D) "D necessary for decimal
Console.WriteLine(e.Name & " still has salary " & e.Salary)
Console.WriteLine()
End Sub
End Class
Public Class Employee
Private m_Name As String
Private m_Salary As Decimal
Private Const LIMIT As Decimal = 0.1D
Public Sub New(ByVal theName As String, ByVal curSalary As Decimal)
m_Name = theName
m_Salary = curSalary
End Sub
ReadOnly Property Name() As String
Get
Return m_Name
End Get
End Property
ReadOnly Property Salary() As Decimal
Get
Return m_Salary
End Get
End Property
Public Overloads Sub RaiseSalary(ByVal Percent As Decimal)
If Percent > LIMIT Then
"not allowed
Console.WriteLine("Percent > LIMIT is not allowed")
Else
m_Salary = (1 + Percent) * m_Salary
End If
End Sub
Public Overloads Sub RaiseSalary(ByVal Percent As Decimal, _
ByVal Password As String)
If Password = "special" Then
m_Salary = (1 + Percent) * m_Salary
End If
End Sub
End Class
Define ReadOnly Property
Imports System
Public Class MainClass
Public Shared ReadOnly MaxEmployees As Integer = 100
Shared Sub Main(ByVal args As String())
Console.WriteLine(MaxEmployees)
End Sub
End Class
Demo Const and ReadOnly
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim random As Random = New Random()
Dim circle As CircleConstants = New CircleConstants(random.Next(1, 20))
Dim radius As String = Convert.ToString(circle.RADIUS)
Dim output As String = "Radius = " & radius & vbCrLf _
& "Circumference = " + String.Format("{0:N3}", _
circle.RADIUS * 2 * CircleConstants.PI)
Console.WriteLine(output)
End Sub
End Class
" Encapsulate constants PI and radius.
Class CircleConstants
" PI is constant data member
Public Const PI As Double = 3.14159
" radius is uninitialized constant
Public ReadOnly RADIUS As Integer
" constructor of class CircleConstants
Public Sub New(ByVal radiusValue As Integer)
RADIUS = radiusValue
End Sub " New
End Class " CircleConstants