End Get Set(ByVal Value As Integer) End Set

Private Shared miInstanceCount As Integer ‘ Increment the count in the constructor ‘ (If there are additional constructors, ‘ this code must be added to all of them.) Sub new( ) miInstanceCount += 1 End Sub ‘ Supply a function to retrieve the instance count Shared Function GetInstanceCount( ) As Integer Return miInstanceCount End Function ‘ Decrement the count in the destructor Overrides Protected Sub Finalize( ) miInstanceCount -= 1 MyBase.Finalize End Sub Now, code such as the following accesses the shared variable: Dim steve As New CPerson( ) MsgBox(CPerson.GetInstanceCount) ‘ Displays 1 Dim donna As New CPerson( ) MsgBox(CPerson.GetInstanceCount) ‘ Displays 2 3.3.7 Class Constructors When an object of a particular class is created, the compiler calls a special function called the class’ constructor or instance constructor. Constructors can be used to initialize an object when necessary. (Constructors take the place of the Class_Initialize event in earlier versions of VB.) We can define constructors in a class module. However, if we choose not to define a constructor, VB uses a default constructor. For instance, the line: Dim APerson As CPerson = New CPerson( ) invokes the default constructor of our CPerson class simply because we have not defined a custom constructor. To define a custom constructor, we just define a subroutine named New within the class module. For instance, suppose we want to set the Name property to a specified value when a CPerson object is first created. Then we can add the following code to the CPerson class: ‘ Custom constructor Sub New(ByVal sName As String) Me.Name = sName End Sub Now we can create a CPerson object and set its name as follows: Dim APerson As CPerson = New CPerson(”fred”) or: Dim APerson As New CPerson(”fred”)
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

Comments are closed.