Archive for October, 2006

NotOverridable Prohibits overriding of the member. This is

Tuesday, October 31st, 2006

However, in the second case, the variable of type Class1 holds a reference to an object of the derived class, Class1Derived. In this case, Me refers to an object of type Class1Derived, whereas MyClass still refers to the base class Class1 wherein the keyword MyClass appears. Thus, Me.IncSalary returns 12000 whereas the following: MyClass.IncSalary returns 10000. 3.5 Interfaces, Abstract Members, and Classes We have alluded to the fact that a class may implement all, some, or none of the members of the interfaces that it defines. Any interface member that does not have an implementation is referred to as an abstract member. The purpose of an abstract member is to provide a member signature (a template, if you will) that can be implemented by one or more derived classes, generally in different ways. Let us clarify this with an example. Recall from our discussion of inheritance that the CEmployee class defines and implements an IncSalary method that increments the salary of an employee. Recall also that the CExecutive and CSecretary derived classes override the implementation of the IncSalary method in the base class CEmployee. Suppose that, in a more complete employee model, there is a derived class for every type of employee. Moreover, each of these derived classes overrides the implementation of the IncSalary method in the base class CEmployee. In this case, the implementation of IncSalary in the base class will never need to be called! So why bother to give the member an implementation that will never be used? Instead, we can simply provide an empty IncSalary method, as shown here: ‘ Employee class Public Class CEmployee . . . Public Overridable Sub IncSalary(ByVal sngPercent As Single) End Sub End Class Alternatively, if we want to require that all derived classes implement the IncSalary method, we can use the MustOverride keyword, as shown here: ‘ Employee class Public MustInherit Class CEmployee . . . Public MustOverride Sub IncSalary(ByVal sngPercent As Single) End Class As mentioned earlier, when using MustOverride, there is no EndSub statement associated with the method. Note also that when using the MustOverridekeyword, Microsoft requires that the class be

Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Virtualwebstudio jsp web hosting provider

NotOverridable Prohibits overriding of the member. This is

Tuesday, October 31st, 2006

NotOverridable Prohibits overriding of the member. This is the default for Public members of a class. MustOverride Must be overridden. When this keyword is used, the member definition is restricted to just the declaration line, with no implementation and no EndSub or EndFunction line. For example: Public MustOverride Sub IncSalary( ) Note also that when a class module contains a MustOverride member, then the class itself must be declared as MustInherit. Overrides Unlike the other modifiers, this modifier belongs in the derived class and indicates that the modified member is overriding a base class member. For example: Overrides Sub IncSalary( ) 3.4.3 Rules of Inheritance In many object-oriented languages, such as C++, a class can inherit directly from more than one base class. This is referred to as multiple inheritance . VB .NET does not support multiple inheritance, and so a class can inherit directly from at most one other class. Thus, code such as the following is not permitted: ‘ Executive Class Public Class CExecutive ‘INVALID Inherits CEmployee Inherits CWorker . . . End Class On the other hand, Class C can inherit from Class B, which, in turn, can inherit from Class A, thus forming an inheritance hierarchy. Note also that a class can implement multiple interfaces through the Interface keyword. We discuss this issue later in this chapter. 3.4.4 MyBase, MyClass, and Me The keyword MyBase provides a reference to the base class from within a derived class. If you want to call a member of the base class from within a derived class, you can use the syntax: MyBase.MemberName where MemberName is the name of the member. This will resolve any ambiguity if the derived class also has a member of the same name. The MyBase keyword can be used to call the constructor of the base class in order to instantiate a member of that class, as in: MyBase.New(…) Note that MyBase cannot be used to call Private class members.

Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Virtualwebstudio jsp web hosting provider

demand. For this purpose, the Base Class Library

Monday, October 30th, 2006

Debug.Writeline(”Sec before: ” & CStr(MySecretary.Salary)) MySecretary.IncSalary(0.3) Debug.Writeline(”Sec after: ” & CStr(MySecretary.Salary)) The output in this case is: Pres before: 1000000 Pres after: 1450000 Sec before: 30000 Sec after: 39600 The notion of inheritance is quite simple, as put forth in Microsoft’s documentation: If Class B inherits from Class A, then any object of Class B is also an object of Class A and so includes the public properties and methods (that is, the public interface) of Class A. In this case, Class A is called the base class and Class B is called the derived class. On the other hand, in general, the derived class can override the implementation of a member of the base class for its own use. We have seen in the previous example that inheritance is implemented using the Inherits keyword. 3.4.1 Permission to Inherit There are two keywords used in the base class definition that affect the ability to inherit from a base class: NotInheritable When this is used to define a class, as in: Public NotInheritable Class InterfaceExample the class cannot be used as a base class. MustInherit When this is used to define a class, as in: Public MustInherit Class InterfaceExample objects of this class cannot be created directly. Objects of a derived class can be created, however. In other words, MustInherit classes can be used as base classes and only as base classes. 3.4.2 Overriding There are several keywords that control whether a derived class can override an implementation in the base class. These keywords are used in the declaration of the member in question, rather than in the class definition: Overridable Allows but does not require a member to be overridden. Note that the default for a Public member is NotOverridable. Here is an example: Public Overridable Sub IncSalary( )

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

demand. For this purpose, the Base Class Library

Monday, October 30th, 2006

Public Class CExecutive Inherits CEmployee ‘ Calculate salary increase based on 5% car allowance as well Overrides Sub IncSalary(ByVal sngPercent As Single) Me.Salary = Me.Salary * CDec(1.05 + sngPercent) End Sub End Class There are two things to note here. First, the line: Inherits CEmployee indicates that the CExecutive class inherits the members of the CEmployee class. Put another way, an object of type CExecutive is also an object of type CEmployee. Thus, if we define an object of type CExecutive: Dim ceo As New CExecutive then we can invoke the Salary property, as in: ceo.Salary = 1000000 Second, the keyword Overrides in the IncSalary method means that the implementation of IncSalary in CExecutive is called instead of the implementation in CEmployee. Thus, the code: ceo.IncSalary raises the salary of the CExecutive object ceo based on a car allowance. Note also the presence of the Overridable keyword in the definition of IncSalary in the CEmployee class, which specifies that the class inheriting from a base class is allowed to override the method of the base class. Next, we define the CSecretary class, which also inherits from CEmployee but implements a different salary increase for secretary objects: ‘ Secretary Class Public Class CSecretary Inherits CEmployee ‘ Secretaries get a 2% overtime allowance Overrides Sub IncSalary(ByVal sngPercent As Single) Me.Salary = Me.Salary * CDec(1.02 + sngPercent) End Sub End Class We can now write code to exercise these classes: ‘ Define new objects Dim ThePresident As New CExecutive( ) Dim MySecretary As New CSecretary( ) ‘ Set the salaries ThePresident.Salary = 1000000 MySecretary.Salary = 30000 ‘ Set Employee to President and inc salary Debug.Writeline(”Pres before: ” & CStr(ThePresident.Salary)) ThePresident.IncSalary(0.4) Debug.WriteLine(”Pres after: ” & CStr(ThePresident.Salary))

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

demand. For this purpose, the Base Class Library

Monday, October 30th, 2006

demand. For this purpose, the Base Class Library defines a second destructor called Dispose. Its general syntax and usage is: Class classname Implements IDisposable Public Sub Dispose( ) Implements IDisposable.Dispose ‘ cleanup code goes here ‘ call child objects’ Dispose methods, if necessary, here End Sub ‘ Other class code End Class Note that classes that support this callable destructor must implement the IDisposable interface hence the Implements statement just shown. IDisposable has just one member, the Dispose method. It is important to note that it is necessary to inform any clients of the class that they must call this method specifically in order to release resources. (The technical term for this is the manual approach!) 3.4 Inheritance Perhaps the best way to describe inheritance as it is used in VB .NET is to begin with an example. The classes in a given application often have relationships to one another. Consider, for instance, our Employee information application. The Employee objects in the class CEmployee represent the general aspects common to all employees name, address, salary, and so on. Of course, the executives of the company will have different perquisites than, say, the secretaries. So it is reasonable to define additional classes named CExecutive and CSecretary, each with properties and methods of its own. On the other hand, an executive is also an employee, and there is no reason to define different Name properties in the two cases. This would be inefficient and wasteful. This situation is precisely what inheritance is designed for. First, we define the CEmployee class, which implements a Salary property and an IncSalary method: ‘ Employee class Public Class CEmployee ‘ Salary property is read/write Private mdecSalary As Decimal Property Salary( ) As Decimal Get Salary = mdecSalary End Get Set mdecSalary = Value End Set End Property Public Overridable Sub IncSalary(ByVal sngPercent As Single) mdecSalary = mdecSalary * (1 + CDec(sngPercent)) End Sub End Class Next, we define the CExecutive class: ‘ Executive Class

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Virtualwebstudio j2ee web hosting services

End Get Set(ByVal Value As Integer) End Set

Monday, October 30th, 2006

Note that because VB .NET supports function overloading (discussed later in this chapter), we can define multiple constructors in a single class, provided each constructor has a unique argument signature. We can then invoke any of the custom constructors simply by supplying the correct number and type of arguments for that constructor. Note also that once we define one or more custom constructors, we can no longer invoke the default (that is, parameterless) constructor with a statement such as: Dim APerson As New CPerson( ) Instead, to call a parameterless constructor, we must specifically add the constructor to the class module: ‘ Default constructor Sub New( ) … End Sub 3.3.8 Finalize, Dispose, and Garbage Collection In VB 6, a programmer can implement the Class_Terminate event to perform any clean up procedures before an object is destroyed. For instance, if an object held a reference to an open file, it might be important to close the file before destroying the object itself. In VB .NET, the Terminate event no longer exists, and things are handled quite differently. To understand the issues involved, we must first discuss garbage collection. When the garbage collector determines that an object is no longer needed (which it does, for instance, when the running program no longer holds a reference to the object), it automatically runs a special destructor method called Finalize. However, it is important to understand that, unlike with the Class_Terminate event, we have no way to determine exactly when the garbage collector will call the Finalize method. We can only be sure that it will be called at some time after the last reference to the object is released. Any delay is due to the fact that the .NET Framework uses a system called reference-tracing garbage collection, which periodically releases unused resources. Finalize is a Protected method. That is, it can be called from a class and its derived classes, but it is not callable from outside the class, including by clients of the class. (In fact, since the Finalize destructor is automatically called by the garbage collector, a class should never call its own Finalize method directly.) If a class’ Finalize method is present, then it should explicitly call its base class’ Finalize method as well. Hence, the general syntax and format of the Finalize method is: Overrides Protected Sub Finalize( ) ‘ Cleanup code goes here MyBase.Finalize End Sub The benefits of garbage collection are that it is automatic and it ensures that unused resources are always released without any specific interaction on the part of the programmer. However, it has the disadvantages that garbage collection cannot be initiated directly by application code and some resources may remain in use longer than necessary. Thus, in simple terms, we cannot destroy objects on cue. We should note that not all resources are managed by the Common Language Runtime. These resources, such as Windows handles and database connections, are thus not subject to garbage collection without specifically including code to release the resources within the Finalize method. But, as we have seen, this approach does not allow us or clients of our class to release resources on
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

End Get Set(ByVal Value As Integer) End Set

Monday, October 30th, 2006

End Get Set(ByVal Value As Integer) End Set End Property Note the Value parameter that provides access to the incoming value. Thus, if we write: Dim cp As New CPerson( ) cp.Age = 20 then VB passes the value 20 into the Property procedure in the Value argument. 3.3.6 Instance and Shared Members The members of a class fall into two categories: Instance members Members that can only be accessed through an instance of the class, that is, through an object of the class. To put it another way, instance members “belong” to an individual object rather than to the class as a whole. Shared (static) members Members that can be accessed without creating an instance of the class. These members are shared among all instances of the class. More correctly, they are independent of any particular object of the class. To put it another way, shared members “belong” to the class as a whole, rather than to its individual objects or instances. Instance members are accessed by qualifying the member name with the object’s name. Here is an example: Dim APerson As New CPerson( ) APerson.Age = 50 To access a shared member, we simply qualify the member with the class name. For instance, the String class in the System namespace of the .NET Base Class Library has a shared method called Compare that compares two strings. Its syntax (in one form) is: Public Shared Function Compare(String, String) As Integer This function returns 0 if the strings are equal, -1 if the first string is less than the second, and 1 if the first string is greater than the second. Since the method is shared, we can write: Dim s As String = “steve” Dim t As String = “donna” MsgBox(String.Compare(s, t)) ‘ Displays 1 Note the way the Compare method is qualified with the name of the String class. Shared members are useful for keeping track of data that is independent of any particular instance of the class. For instance, suppose we want to keep track of the number of CPerson objects in existence at any given time. Then we write code such as the following: ‘ Declare a Private shared variable to hold the instance count
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

End Get Set(ByVal Value As Integer) End Set

Monday, October 30th, 2006

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

This refers to both functions and subroutines. A

Monday, October 30th, 2006

End Get Set(ByVal Value As String) msName = Value End Set End Property ‘ Overloaded constructor Overloads Sub New( ) End Sub ‘ Constructor that initializes name Overloads Sub New(ByVal sNewName As String) msName = sNewName End Sub Sub Dispose( ) ‘ Code here to clean up End Sub End Class 3.3.3 The Public Interface of a VB .NET Class We have seen that, when speaking in general object-oriented terms, the exposed members of a software component constitute the component’s public interface (or just interface). Now, in VB .NET, each member of a class module has an access type, which may be Public, Private, Friend, Protected, or ProtectedFriend. We discuss each of these in detail later in this chapter. Suffice it to say, a VB .NET class module may accordingly have Public, Private, Friend, Protected, and ProtectedFriend members. Thus, we face some ambiguity in defining the concept of the public interface of a VB .NET class. The spirit of the term might indicate that we should consider any member that is exposed outside of the class itself as part of the public interface of the class. This would include the Protected, Friend, and Protected Friend members, as well as the Public members. On the other hand, some might argue that the members of the public interface must be exposed outside of the project in which the class resides, in which case only the Public members would be included in the interface. Fortunately, we need not make too much fuss over the issue of what exactly constitutes a VB .NET class’ public interface, as long as we remain aware that the term may be used differently by different people. 3.3.4 Objects A class is just a description of some properties and methods and does not have a life of its own (with the exception of shared members, which we discuss later). In general, to execute the methods and use the properties of a class, we must create an instance of the class, officially known as an object. Creating an instance of a class is referred to as instancing, or instantiating, theclass. There are three ways to instantiate an object of a VB .NET class. One method is to declare a variable of the class’ type: Dim APerson As CPerson and then instantiate the object using the New keyword as follows: APerson = New CPerson( ) We can combine these two steps as follows:
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

This refers to both functions and subroutines. A

Monday, October 30th, 2006

Dim APerson As New CPerson( ) or: Dim APerson As CPerson = New CPerson( ) The first syntax is considered shorthand for the second. 3.3.5 Properties Properties are members that can be implemented in two different ways. In its simplest implementation, a property is just a public variable, as in: Public Class CPerson Public Age As Integer End Class The problem with this implementation of the Age property is that it violates the principle of encapsulation; anyone who has access to a CPerson object can set its Age property to any Integer value, even negative integers, which are not valid ages. In short, there is no opportunity for data validation. (Moreover, this implementation of a property does not permit its inclusion in the public interface of the class, as we have defined that term.) The “proper” object-oriented way to implement a property is to use a Private data member along with a special pair of function members. The Private data member holds the property value; the pair of function members, called accessors, are used to get and set the property value. This promotes data encapsulation, since we can restrict access to the property via code in the accessor functions, which can contain code to validate the data. The following code implements the Age property. Private miAge As Integer Property Age( ) As Integer Get Age = miAge End Get Set(ByVal Value As Integer) ‘ Some validation If Value < 0 Then MsgBox("Age cannot be negative.") Else miAge = Value End If End Set End Property As you can see from the previous code, VB has a special syntax for defining the property accessors. As soon as we finish typing the line: Property Age( ) As Integer the VB IDE automatically creates the following template: Property Age( ) As Integer Get
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services