VB .NET Language in a Nutshell: What's New and Different in VB .NET
Pages: 1, 2, 3, 4, 5
Changes in Object-Orientation
As you may know, Visual Basic has implemented some features of object-oriented programming since Version 4. However, in terms of object-orientation, the step from Version 6 to VB .NET is very significant. Indeed, some 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 that term.
Here are the main changes in the direction of object-orientation. We discuss these issues in detail in Chapter 3.
Inheritance
VB .NET supports object-oriented inheritance (but not multiple inheritance). This means that a class can derive from another (base) class, thereby inheriting all of the properties, methods, and events of the base class. Since forms are also classes, inheritance applies to forms as well. This allows new forms to be created based on existing forms. We discuss inheritance in detail in Chapter 3.
Overloading
VB .NET supports a language feature known as function overloading. The idea is simple and yet quite useful. We can use the same name for different functions (or subroutines), as long as the functions can be distinguished by their argument signature. The argument signature of a function (or subroutine) is the sequence of data types of the arguments of the function. Thus, in order for two functions to have the same argument signature, they must have the same number of arguments, and the corresponding arguments must have the same data type. For example, the following declarations are legal in the same code module because they have different argument signatures:
Overloads Sub OpenFile( )
' Ask user for file to open and open it
End Sub
Overloads Sub OpenFile(ByVal sFile As String)
' Open file sFile
End Sub
Object Creation
VB 6 supports a form of object creation called implicit object creation. If an object variable is declared using the New keyword:
Dim obj As New SomeClass
then the object is created the first time it is used in code.
More specifically, the object variable is initially given the value Nothing, and then every time the variable is encountered
during code execution, VB checks to see if the variable is Nothing. If so, the object is created at that time.
VB .NET does not support implicit object creation. If an object
variable contains Nothing when it is encountered,
it is left unchanged, and no object is created.
In VB .NET, we can create an object in the same statement as the object-variable declaration, as the following code shows:
Dim obj As SomeClass = New SomeClass
As a shorthand, we can also write:
Dim obj As New SomeClass
If the object's class constructor takes parameters, then they can be included, as in the following example:
Dim obj As SomeClass = New SomeClass(argument1, argument2,...)
As a shorthand, we can also write:
Dim obj As New SomeClass(argument1, argument2,...)
For details on class constructors, see Chapter 3.
Properties
There have been a few changes in how VB handles properties, particularly with respect to default properties and property declarations.
Default properties
As you know, you can use default properties in VB 6. For instance, if txt is a textbox control, then:
txt = "To be or not to be"
assigns the string "To be or not to be" to the default Text property of the textbox txt.
However, there is a price to pay for default properties: ambiguity. For example, if txt1 and txt2 are object variables referencing two TextBox controls, what does:
txt1 = txt2
mean? Are we equating the default properties or the object variables? In VB 6, this is interpreted as equating the default properties:
txt1.Text = txt2.Text
and we require the Set statement for
object assignment:
Set txt1 = txt2
In VB .NET, default properties are not supported unless the property takes one or more parameters, in which case there is no ambiguity.
As Microsoft points out, default properties occur most commonly with collection classes. For example, in ActiveX Data Objects (ADO), the Fields collection of the Recordset object has a default Item property that returns a particular Field object. Thus, we can write:
rs.Fields.Item(1).Value
or, since the default Item property is parameterized:
rs.Fields(1).Value
Although we may not be used to thinking of this line as using default properties, it does.
Thus, in VB .NET, the line:
txt1 = txt2
is an object assignment. To equate the Text properties, we must write:
txt2.Text = txt1.Text
Since it is no longer needed, the Set
keyword is not supported under VB .NET, nor is the companion Let keyword.
This settles the issue of equating object variables. For object
variable comparison, however, we must use the Is operator, rather than the equal sign, as in:
If txt1 Is txt2 Then
or:
If Not (txt1 Is txt2) Then
|
Related Reading
|
Property declarations
In VB 6, properties are defined using Property Let, PropertySet, and Property Get procedures. However, VB .NET uses a single
property-declaration syntax of the form shown in the following example. Note
also that there is no longer a need to distinguish between Property Let and PropertySet because of the changes in
default property support.
Property Salary( ) As Decimal
Get
Salary = mdecSalary
End Get
Set
mdecSalary = Value
End Set
End Property
Note the use of the implicitly defined Value variable that holds the value being passed into the
property procedure when it is being set.
Note also that VB .NET does not support ByRef property parameters. All property parameters are
passed by value.
View catalog information for VB .NET Language in a Nutshell
Return to .NET DevCenter.


