Archive for October, 2006

This refers to both functions and subroutines. A

Monday, October 30th, 2006

This refers to both functions and subroutines. A function member is also called a method. A class’ constructor is a special type of method. We discuss constructors in detail later in this chapter. Property members A property member is implemented as a Private member variable together with a special type of VB function that incorporates both accessor functions of the property. We discuss the syntax of this special property function in Section 3.3.5 later in the chapter. Type members A class member can be another class, which is then referred to as a nested class. The following CPerson class illustrates some of the types of members: Public Class CPerson ‘ ————- ‘ Data Members ‘ ————- ‘ Member variables Private msName As String Private miAge As Integer ‘ Member constant Public Const MAXAGE As Short = 120 ‘ Member event Public Event Testing( ) ‘ —————- ‘ Function Members ‘ —————- ‘ Method Public Sub Test( ) RaiseEvent Testing( ) End Sub 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 ' Property Property Name( ) As String ' Accessors for the property Get Name = msName
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

and let the computer and the programming language

Monday, October 30th, 2006

The signature of a function is the function name and return type, as well as the names, order, and types of its parameters. A function declaration is simply a clear way of describing the function’s signature. Note that Microsoft does not consider the return type of a function to be part of the function’s signature. By signature, they mean what is generally termed the function’s argument signature. The reasons for doing this become clearer later in the chapter when we discuss overloading, although it would have been better (as usual) if they were more careful with their terminology. Under this more specific definition of interface, the interface for our employee component might be as follows (in part): Function GetFullName(lEmpID As Long) As String Sub SetFullName(lEmpID As Long, sName As String) . . . Sub IncSalary(sngPercent As Single) Sub DecSalary(sngPercent As Single) 3.3 Classes and Objects Generally speaking, a class is a software component that defines and implements one or more interfaces. (Strictly speaking, a class need not implement all the members of an interface. We discuss this later when we talk about abstract members.) In different terms, a class combines data, functions, and types into a new type. Microsoft uses the term type to include classes. 3.3.1 Class Modules in VB .NET Under Visual Studio.NET, a VB class module is inserted into a project using the Add Class menu item on the Project menu. This inserts a new module containing the code: Public Class ClassName End Class Although Visual Studio stores each class in a separate file, this isn’t a requirement. It is the Class…EndClass construct that marks the beginning and end of a class definition. Thus, the code for more than one class as well as one or more code modules (which are similarly delimited by the Module…End Module construct) can be contained in a single source code file. The CPerson class defined in the next section is an example of a VB class module. 3.3.2 Class Members In VB .NET, class modules can contain the following types of members: Data members This includes member variables (also called fields) and constants. Event members Events are procedures that are called automatically by the Common Language Runtime in response to some action that occurs, such as an object being created, a button being clicked, a piece of data being changed, or an object going out of scope. Function 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

and let the computer and the programming language

Monday, October 30th, 2006

and let the computer and the programming language worry about which representation to use and how to perform the given operations.” This is precisely the point behind encapsulation. The details of how signed integers are interpreted by the computer (and the compiler), as well as how their properties and operations are implemented, are encapsulated in the integer data type itself and are thus hidden from us, the users of the data type. Only those portions of the properties and operations that we need in order to work with integers are exposed outside of the data type. These portions form the public interface for the Integer data type. Moreover, encapsulation protects us from making errors. For instance, if we had to do our own negating by taking Boolean complements and adding 1, we might forget to add 1! The encapsulated data type takes care of this automatically. Encapsulation has yet another important feature. Any code that is written using the exposed interface remains valid even if the internal workings of the Integer data type are changed for some reason, as long as the interface is not changed. For instance, if we move the code to a computer that stores integers in one’s-complement representation, then the internal procedure for implementing the operation of negation in the integer data type will have to be changed. However, from the programmer’s point of view, nothing has changed. The code: x = -16 y = -x is just as valid as before. 3.2.3 Interfaces As VB programmers, we must implement encapsulation through the use of software components. For instance, we can create a software component to encapsulate the Employee abstraction discussed earlier. In VB .NET, the methods of an interface are realized as functions. On the other hand, a property, as we see later in this chapter, is realized as a private variable that stores the property’s value together with a pair of public functions one to set the variable and one to retrieve the variable. These functions are sometimes referred to as accessor methods of the property. It is the set of exposed functions (ordinary methods and accessor methods) that constitute the interface for an abstraction. In general, a software component may encapsulate and expose more than one abstraction hence, more than one interface. For example, in a more realistic setting, we might want a software component designed to model employees to encapsulate an interface called IIdentification (the initial “I” is for interface) that is used for identification purposes. This interface might have properties such as Name, Social Security number, Driver’s License number, Age, Birthmarks, and so on. Moreover, the software component might also encapsulate an interface called IEducation for describing the employee’s educational background. Such an interface might implement properties such as Education Level, Degrees, College Attended, and so on. The interface of each abstraction exposed by a software component is also referred to as an interface of the software component. Thus, the Employee component implements at least two interfaces: IIdentification and IEducation. Note, however, that the term interface is often used to refer to the set of all exposed properties and methods of a software component, in which case a component has only one interface. Referring to our original Employee abstraction, its interface might consist of the functions shown in Table 3-1. (Of course, this interface is vastly oversimplified, but it is more than sufficient to illustrate the concepts.)

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

and let the computer and the programming language

Monday, October 30th, 2006

Table 3-1. Members of the Employee interface Type Name Property FullName: GetFullName(), SetFullName( ) Property Address: GetAddress(), SetAddress( ) Property EmployeeID: GetEmployeeID(), SetEmployeeID( ) Property Salary: GetSalary(), SetSalary( ) Method IncSalary( ) Method DecSalary( ) Using the term interface as a set of functions, while quite common, poses a problem. Just listing the functions of the interface by name (as done previously) does not provide enough information to call those functions. Thus, a more useful definition of interface would be the set of signatures of the public functions of a software component. To clarify this, let us discuss one of the most important distinctions in object-oriented programming the distinction between a function declaration and an implementation of that function. By way of example, consider the following sorting function: Function Sort(a( ) as Integer, iSize as Integer) as Boolean For i = 1 to iSize For j = i+1 to iSize If a(j) < a(i) Then swap a(i), a(j) Next j Next I Sort = True End Function The first line in this definition: Function Sort(a( ) as Integer, iSize as Integer) as Boolean is the function declaration. It supplies information on the number and types of parameters and the return type of the function. The body of the function: For i = 1 to iSize For j = i+1 to iSize If a(j) < a(i) Then swap a(i), a(j) Next j Next i Sort = True represents the implementation of the function. It describes how the function carries out its intended purpose. Note that it is possible to alter the implementation of the function without changing the declaration. In fact, the current function implementation sorts the array a using a simple selection-sort algorithm, but we could replace that sorting method with any one of a number of other methods (bubble sort, insertion sort, quick sort, and so on). Now consider a client of the Sort function. The client only needs to know the function declaration in order to use the function. It need not know (and probably doesn't want to know) anything about the implementation. Thus, it is the function declaration, and not the implementation, that forms the interface for the function.

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

Msgbox(GetAverage(1, 2, 3, 4, 5)) Msgbox(GetAverage(1, 2, 3))

Sunday, October 29th, 2006

Chapter 3. Introduction to Object-Oriented Programming In this chapter, we present a brief and succinct introduction to object-oriented programming. Since this is not a book on object-oriented programming per se, we will confine our attention to those topics that are important to VB .NET programming. 3.1 Why Learn Object-Oriented Techniques? As you may know, Visual Basic has implemented some features of object-oriented programming since Version 4. However, in terms of object-orientation, the move from Version 6 to VB .NET has been dramatic. Many people did not consider VB 6 (or earlier versions) to be a truly object-oriented programming language. Whatever your thoughts may have been on this matter, it seems clear that VB .NET is an object-oriented programming language by any reasonable definition of the term. You may be saying to yourself: “I prefer not to use object-oriented techniques in my programming.” This is something you could easily have gotten away with in VB 6. But in VB .NET, the structure of the .NET Framework specifically the .NET Base Class Library as well as the documentation, is so object-oriented that you can no longer avoid understanding the basics of object-orientation, even if you decide not to use them in your applications. 3.2 Principles of Object-Oriented Programming It is often said that there are four main concepts in the area of object-oriented programming: Abstraction Encapsulation Inheritance Polymorphism Each of these concepts plays a significant role in VB .NET programming at one level or another. Encapsulation and abstraction are “abstract” concepts providing motivation for object-oriented programming. Inheritance and polymorphism are concepts that are directly implemented in VB .NET programming. 3.2.1 Abstraction Simply put, an abstraction is a view of an entity that includes only those aspects that are relevant for a particular situation. For instance, suppose that we want to create a software component that provides services for keeping a company’s employee information. For this purpose, we begin by making a list of the items relevant to our entity (an employee of the company). Some of these items are: FullName Address EmployeeID Salary IncSalary DecSalary Note that we include not only properties of the entities in question, such as FullName, but also actions that might be taken with respect to these entities, such as IncSalary, to increase an employee’s salary. Actions are also referred to as methods, operations, or behaviors. We will use the term methods, since this term is used by VB .NET.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Msgbox(GetAverage(1, 2, 3, 4, 5)) Msgbox(GetAverage(1, 2, 3))

Sunday, October 29th, 2006

Of course, we would never think of including an IQ property, since this would not be politically correct, not to mention discriminatory and therefore possibly illegal. Nor would we include a property called HairCount, which gives the number of hairs on the employee’s right arm, because this information is of absolutely no interest to us, even though it is part of every person’s being. In short, we have abstracted the concept of an employee we have included only those properties and methods of employees that are relevant to our needs. Once the abstraction is complete, we can proceed to encapsulate these properties and methods within a software component. 3.2.2 Encapsulation The idea of encapsulation is to contain (i.e., encapsulate) the properties and methods of an abstraction, and expose only those portions that are absolutely necessary. Each property and method of an abstraction is called a member of the abstraction. The set of exposed members of an abstraction is referred to collectively as the public interface (or just interface) of the abstraction (or of the software component that encapsulates the abstraction). Encapsulation serves three useful purposes: It permits the protection of these properties and methods from any outside tampering. It allows the inclusion of validation code to help catch errors in the use of the public interface. For instance, it permits us to prevent the client of the employee software component from setting an employee’s salary to a negative number. It frees the user from having to know the details of how the properties and methods are implemented. Let us consider an example that involves the Visual Basic Integer data type, which is nicely encapsulated for us by VB. As you undoubtedly know, an integer is stored in the memory of a PC as a string of 0s and 1s called a binary string. In Visual Basic, integers are interpreted in a form called two’s-complement representation, which permits the representation of both negative and non-negative values. For simplicity, let us consider 8-bit binary numbers. An 8-bit binary number has the form a7a6a5a4a3a2a1a0, where each of the a1s is a 0 or a 1. We can think of it as appearing in memory as shown in Figure 3-1. Figure 3-1. An 8-bit binary number In the two’s-complement representation, the leftmost bit, a7 (called the most significant bit), is the sign bit. If the sign bit is 1, the number is negative. If the sign bit is 0, the number is positive. The formula for converting a two’s-complement representation a7a6a5a4a3a2a1a0 of a number to a decimal representation is: decimal rep. = -128a7 + 64a6 + 32a5 + 16a4 + 8a3 + 4a2 + 2a1 + a0 To take the negative of a number when it is represented in two’s-complement form, we must take the complement of each bit (that is, change each 0 to a 1 and each 1 to a 0) and then add 1. At this point you may be saying to yourself, “As a programmer, I don’t have to worry about these details. I just write code like: x = -16 y = -x
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Msgbox(GetAverage(1, 2, 3, 4, 5)) Msgbox(GetAverage(1, 2, 3))

Sunday, October 29th, 2006

Msgbox(GetAverage(1, 2, 3, 4, 5)) Msgbox(GetAverage(1, 2, 3)) The following rules apply to the use of ParamArray: A procedure can only have one parameter array, and it must be the last parameter in the procedure. The parameter array must be passed by value, and you must explicitly include ByVal in the procedure definition. The parameter array must be a one-dimensional array. If the type is not declared, it is assumed to be Object. The parameter array is automatically optional. Its default value is an empty one-dimensional array of the parameter array’s data type.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

On the other hand, if we modify the

Sunday, October 29th, 2006

Upon return from GetText, t is now pointing to TextBox2, so the MsgBox function displays the string “TextBox2.” 2.7.3 Optional Arguments In VB .NET, parameters can be declared as optional using the Optional keyword, as shown in the following code: Sub Calculate(Optional ByVal Switch As Boolean = False) In VB .NET, all optional parameters must declare a default value, which is passed to the procedure if the calling program does not supply that parameter. The following rules apply to optional arguments: Every optional argument must specify a default value, and this default must be a constant expression (not a variable). Every argument following an optional argument must also be optional. Note that in earlier versions of VB, you could omit the default value and, if the parameter was of type Variant, you could use the IsMissing function to determine if a value was supplied. This is not possible in VB .NET, and the IsMissing function is not supported. 2.7.4 ParamArray Normally, a procedure definition specifies a fixed number of parameters. However, the ParamArray keyword, which is short for Parameter Array, permits us to declare a procedure with an unspecified number of parameters. Therefore, each call to the procedure can use a different number of parameters. Suppose, for instance, that we want to define a function to take the average of a number of test scores, but the number of scores may vary. Then we declare the function as follows: Function GetAverage(ParamArray ByVal Scores( ) As Single) As Single Dim i As Integer For i = 0 To UBound(Scores) GetAverage = GetAverage + CSng(Scores(i)) Next GetAverage = GetAverage / (UBound(Scores) + 1) End Function Now we can make calls to this function with a varying number of arguments:
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

On the other hand, if we modify the

Sunday, October 29th, 2006

GetText is called, passing t by value. Since t contains the address aaaa of the TextBox1 object, the local variable txt is given the value aaaa, as shown in Figure 2-4. Figure 2-4. Passing an object by value The single line of code in GetText is executed, which now causes txt to point to TextBox2, as shown in Figure 2-5. Figure 2-5. Assigning a new object reference Upon return from GetText, t is unaffected, so the MsgBox function displays the string “TextBox1.” Now suppose we change the ByVal keyword to ByRef in GetText. Here is what happens: The TextBox variable t is assigned to TextBox1, as shown previously in Figure 2-3. GetText is called, passing t by reference. Hence, txt is t. This is quite different from txt and t containing the same value, as in the ByVal case. The situation is shown in Figure 2-6. Figure 2-6. Passing an object by reference The single line of code in GetText is executed, which now causes txt (and hence t) to point to TextBox2, as shown in Figure 2-7. Figure 2-7. Assigning a new object reference
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services

On the other hand, if we modify the

Sunday, October 29th, 2006

On the other hand, if we modify the definition of the procedure Inc, replacing ByVal with ByRef, the story is different: Sub Inc(ByRef x As Integer) x = x + 1 End Sub In this case, what is passed to the procedure Inc is a reference to the argument iAge. Hence, the procedure actually operates on the variable passed to it, incrementing the value of iAge to 21. Put another way, the variable represented by the parameter x is actually the passed variable iAge. In VB .NET, the default method of argument passing for arguments is by value. This is a change from earlier versions of VB, in which the default method was by reference. 2.7.2 Passing Objects There is a subtlety in argument passing with parameters of any object type. Actually, the subtlety occurs because an object variable is a pointer ; that is, it contains a reference to (or the address of) the object. If we pass an object variable by value, we are passing the contents of the variable, which is the address of the object. Thus, any changes made in the called procedure affects the object itself, not a copy of the object. This seems like passing by reference, but it is not. Think of it this way: passing the value of an object’s address is passing a reference to the object. On the other hand, if we pass an object variable by reference, we are passing the address of the variable. In other words, we are passing the address of the address of the object! In languages that support pointers, this is referred to as a double pointer. Let us illustrate with an example. Consider the following code, and imagine that the form containing this code has two textboxes: TextBox1 with text “TextBox1″ and TextBox2 with text “TextBox2″: Public Function GetText(ByVal txt As TextBox) As String ‘ Change reference to textbox txt = Textbox2 End Function Sub Doit Dim t As TextBox t = TextBox1 GetText(t) msgbox(t.Text) ‘ Displays TextBox1 when ByVal, _ ‘ TextBox2 when ByRef End Sub Now, here is what happens when we execute DoIt. Note that the argument is passed to GetText by value in this case. The TextBox variable t is assigned to TextBox1, as shown in Figure 2-3. Figure 2-3. Assigning an object reference
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services