PRB: Operators Do Not Recursively Call Object Default Properties
ID: Q194368
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0
SYMPTOMS
In Visual Basic 5.0, the following code will call the default property of
obj:
Dim obj As Object
If obj = 5 Then
If the Object's default property returns an object, it recursively calls
the default property of that object until it finally finds a non-object
property.
Visual Basic 6.0 only invokes the default property of the second level, at
most. If it is also an object, Visual Basic 6.0 will throw a "Type
Mismatch" error.
CAUSE
The way the comparison operators (=, >, <, etc.) evaluate objects has been
changed in Visual Basic 6.0.
NOTE: The behavior of Visual Basic 5.0 in this case is undocumented and
programmers should not rely on this feature.
RESOLUTION- Add the following function to your project:
Public Function EvaluateObjToLong(obj As Object) As Long
EvaluateObjToLong = obj
End Function
- Replace the following line:
if obj = 5 then
with:
if EvaluateObjToLong(obj) = 5 then
Please note that this is only a workaround for migrating from Visual Basic
5.0 to Visual Basic 6.0. Programmers should not rely on this feature. The
recursive evaluation of the default property of the assignment operator may
be changed in future versions.
STATUS
This behavior change is by design. The reasons for this are:
- This change makes the behavior of late bound objects more closely match
that of early bound objects.
- The behavior of Visual Basic 5.0 permits the possibility of an infinite
loop, which was determined to be much worse than this restriction.
MORE INFORMATIONSteps to Reproduce Behavior
- Create a new ActiveX DLL project in Visual Basic 5. Class1 is created by
default.
- From the Project menu, add two additional classes named Class2 and
Class3 to the project.
- Add following code to Class1:
Public Property Get DefProp() As Object
Set DefProp = New Class2
End Property
- Select Procedure Attributes from the Tools menu. In the dialog box,
click the "Advanced >>" button, and change Procedure ID to "<Default>."
- Add the following code to Class2:
Public Property Get DefProp() As Object
Set DefProp = New Class3
End Property
- Set the DefProp of Class2 to be the default property, as in step 4.
- Add the following code to Class3:
Public Property Get DefProp() As Long
DefProp = 5
End Property
- Set the DefProp of Class3 to be the default property, as in step 4.
- From the File menu, add a Standard EXE project to the project group as
Project2. Form1 is created by default. Set Project2 as the start up
project.
- Add the following code to the General Declarations section of Form1:
Public Function EvaluateObjToLong(obj As Object) As Long
EvaluateObjToLong = obj
End Function
Private Sub Form_Load()
Dim obj As Object
Set obj = CreateObject("project1.Class1")
If obj = 5 Then MsgBox "work"
End Sub
- Press the F5 key to run the project, and note that a message box
displays the word "work."
- Stop the project and save the project group.
- Open the project group in Visual Basic 6.0 and run it. The following
error message will be displayed:
Run Time Error '13':
Type Mismatch
- Change the line:
If obj = 5 Then MsgBox "work"
to:
If EvaluateObjToLong(obj) = 5 Then MsgBox "work"
- Run the project again, and note that the Message box displays "work."
Additional query words:
kbDSupport kbDSD kbVBp kbVBp600 kbActiveX kbVBp500
Keywords : kbGrpVB
Version : WINDOWS:6.0
Platform : WINDOWS
Issue type : kbprb
|