AddItem and ToString with COM Objects

AddItem and ToString with COM Objects

In Visual Basic 6, the AddItem method is used to add a new entry to a ListBox or ComboBox control. In Visual Basic .NET, the equivalent operation involves calling the Add method of the Items collection for the ComboBox or ListBox Windows Forms controls. This method takes an object as a parameter, instead of a string. Because only strings can be displayed in a ListBox, the Add method implicitly calls the ToString method on the passed object. For variables that have a primitive value, such as a string or a number, the method returns the value as a string. For other objects, however, the ToString method s behavior varies significantly from class to class.

note

Officially, Visual Basic .NET no longer supports implicit default properties. Unofficially, however, this is not universally true. All classes in the .NET Framework derive from System.Object. System.Object defines several methods, one of the most important of which is the ToString method. In some ways, ToString is emerging as a de facto default property. For instance, ComboBoxes display strings in their lists, yet the ComboBox.Items.Add method accepts an object, not a string. How does the Add method populate the list? It calls the object s ToString method with the expectation that the ToString method of any object returns the actual information that is desired.

Consider the following example of adding items to a ListBox from a TextBox in Visual Basic 6. This example is contained in the ListBoxExample sample project on the companion CD. It adds the contents of a TextBox control to a ListBox control. The key here is that we are relying on the default method of the TextBox control (Text) being called when the control is passed to the AddItem method.

Option Explicit ' Button Click Event Handler Private Sub AddButton_Click()    AddText Text1 End Sub ' Adding the control to the ListBox Private Sub AddText(text)    ' We are relying on the default property here    List1.AddItem text End Sub

The problem with this code is its use of the default property of the TextBox object when the object is late-bound. When a TextBox is used in this context, Visual Basic assumes that you want the default property specified in the COM object s type library. In this case, the Text property of the TextBox object is defined as its default property. This means that the following two lines of code are functionally equivalent:

List1.AddItem Text1 List1.AddItem Text1.Text

As we mentioned previously, the use of a Variant for handling the TextBox object in the AddText method prevents the Upgrade Wizard from resolving the default property. The following example shows what the code looks like after upgrading:

' Button Click Event Handler Private Sub AddButton_Click(ByVal eventSender As System.Object, _    ByVal eventArgs As System.EventArgs) Handles AddButton.Click    AddText(Text1) End Sub ' Adding the control to the ListBox 'UPGRADE_NOTE: Text was upgraded to text_Renamed. Private Sub AddText(ByRef text_Renamed As Object)    ' We are relying on the default property here    'UPGRADE_WARNING: Couldn't resolve default property of     'object text_Renamed.    List1.Items.Add(text_Renamed) End Sub

What happens? The code runs as is and does not generate an exception at run time. However, the TextBox s ToString method is called. Unfortunately, the TextBox does not return just the displayed text; it also prepends the string with its class path. So, for example, if the TextBox displays the string Hello World, the ToString method returns System.Windows.Forms.TextBox, Text: Hello World. To fix the problem, you need to modify your code to eliminate the use of default properties. Doing so would change the line in which the items are added to the ListBox to the following:

' This explicitly passes the contents of the Text property to the  ' ListBox Add method. List1.Items.Add(text_Renamed.Text)

It is important to emphasize that if the original sample had not used late binding, no modifications would have been required.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

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