Section D.5. Changes in Object Orientation


D.5. Changes in Object Orientation

Visual Basic has implemented some features of object-oriented programming (OOP) since Version 4. However, the OOP changes made between VB 6 and VB.NET are very significant. Some people did not consider VB 6 (or earlier versions) to be a true object-oriented programming language. VB.NET 2002 includes support for most major OOP concepts, and the general changes introduced with VB.NET appear in this section. Chapter 3 includes a general overview of OOP concepts and introduces VB.NET's specific OOP implementation.

D.5.1. 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, events, and other types of the base class. Since .NET forms are classes, inheritance applies to them as well. This allows new forms to be created based on existing control-laden forms.

D.5.2. Overloading

VB.NET supports a language feature known as function overloading. Within a class, a single method can include multiple argument signature variations. That is, the number and data type of each method argument can vary between the different versions, despite the method having the same name in each version. The following declarations are valid in the same class.

     Public Overloads Sub OpenFile(  )        ' ----- Prompt user for file to open, and open it.     End Sub     Public Overloads Sub OpenFile(ByVal fileToOpen As String)        ' ----- Open the specified file.     End Sub 

D.5.3. Object Creation

VB 6 supports a form of object creation called implicit object creation. If an object variable is declared using the New keyword, as in:

     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. (This behavior was changed somewhat in a Visual Studio 6.0 service pack.)

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.

VB.NET supports object creation in the declaration statement, as with:

     Dim someInstance As SomeClass = New SomeClass 

or the shorter equivalent:

     Dim someInstance As New SomeClass 

If the object's class constructor takes parameters, they can be included, as with:

     Dim someInstance As SomeClass = New SomeClass(arg1, arg2, ...) 

or the shorter equivalent:

     Dim someInstance As New SomeClass(arg1, arg2, ...) 

D.5.4. Properties

There have been a few changes in how VB handles properties, particularly default properties and property declarations.

D.5.4.1. Default properties

VB 6 supports default properties. For instance, if txtQuote is a TextBox control, then:

     txtQuote = "To be or not to be" 

assigns the string "To be or not to be" to the default Text property of txtQuote.

However, this can sometimes lead to ambiguity. For example, if TextBox1 and TextBox2 are TextBox controls on a form, what does the following statement do?

     TextBox1 = TextBox2 

In VB 6, this assigns the default property of TextBox2 (the Text property) to the same property in TextBox1, although it looks like the object itself is being assigned. Object assignment in VB 6 uses the Set keyword, as in the following syntax:

     Set TextBox1 = TextBox2 

But in VB.NET, the Set keyword is no longer used for object assignment. Therefore, to avoid any ambiguity, default properties in VB.NET are not supported unless the property takes one or more parameters. In VB.NET, the line:

     TextBox1 = TextBox2 

is an object assignment statement. To copy the contents of the Text property, the following syntax is required:

     TextBox1.Text = TextBox2.Text 

For object variable comparison, VB.NET uses the Is operator rather than the Equal To (=) comparison operator, as in:

     If (TextBox1 Is TextBox2) Then 

or:

     If Not (TextBox1 Is TextBox2) Then 

D.5.4.2. Property declarations

In VB 6, properties are defined using Property Let, Property Set, and Property Get procedures. VB.NET uses a modified Property statement syntax.

     Property Salary(  ) As Decimal        Get           Salary = employeeSalary        End Get        Set(ByVal value As Decimal)           employeeSalary = value        End Set     End Property 

The former differentiation between Property Let and Property Set no longer exists in VB.NET due to default property changes; only Property Set is retained.

VB.NET does not support ByRef property parameters. All property parameters are passed ByVal.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net