Microsoft KB Archive/255630

= BUG: Using Friend Function with Implements Does Not Work in P-Code =

Article ID: 255630

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q255630



SYMPTOMS
When calling a friend procedure you get the following error:

Run-time error 97: Cannot call friend function on object which is not an instance of defining class.



CAUSE
This problem happens if the IMPLEMENTS statement is used in the object where the friend procedure is defined, and only if you compile your project to P-Code.



RESOLUTION
To avoid this problem you need to compile your project to Native Code.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
 Start a new ActiveX DLL project. This project stores the base class. Class1 is created by default. Change the project name to MyBaseDll . Change the name of Class1 to BaseClass1 .  Add the following code to BaseClass1: Option Explicit

Public Function GetLong as Long End Function Public Function GetLongB as Long End Function

 Save the project, and compile the DLL.</li> Start a new ActiveX DLL project. Class1 is created by default.</li> Change the project name to MyImplDll .</li> From the Project menu, select References, select MyBaseDll, and then click OK.</li> Change the name of Class1 to ImplClass1 .</li>  Add the following code to ImplClass1. This code implements BaseClass1. Option Explicit

Implements MyBaseDll.BaseClass1

Friend Function BaseClass1_GetLong As Long BaseClass1_GetLong = 7 End Function

Private Function BaseClass1_GetLongB As Long BaseClass1_GetLongB = BaseClass1_GetLong End Function

</li> Add a new class to this project, and name it ImplClass2 .</li>  Add the following code to ImplClass2: Option Explicit

Public Function GetLong As Long Dim MyObj As New ImplClass1 GetLong = MyObj.BaseClass1_GetLong End Function </li> Save this project, and compile the DLL.</li> Create a new Standard EXE project. This is the client to test the DLL. Form1 is created by default.</li> Place a CommandButton on Form1.</li> From the Project menu, select References, select MyBaseDll and MyImplDll, and then click OK.</li>  In Command1's click event, place the following code: Dim MyObjImpl As New MyImplDll.ImplClass1 Dim MyObjImpl2 As New MyImplDll.ImplClass2 Dim MyObjBase As New MyBaseDll.BaseClass1 Set MyObjBase = MyObjImpl MsgBox MyObjBase.GetLong MsgBox MyObjBase.GetLongB MsgBox MyObjImpl2.GetLong </li> Save this project. Compile the project and close it.</li> Run project1.exe and click on the Command button. Everything should work fine and you should see three message boxes, all of them displaying the value 7.</li> Close project1.exe.</li> Load the MyImplDll project.</li> <li>From the Project menu, select Project Properties. Select the Component tab and set Binary Compatibility to the compiled DLL.</li> <li>Then select the Compile tab and check the Compile to P-Code option.</li> <li>Close the Project Properties window and compile the DLL.</li> <li>Run project1.exe and click the Command button. You should see the first message box followed by an error message:

Run-time error 97: Can not call friend function on object which is not an instance of defining class.

</li></ol>

Keywords: kbbug KB255630

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.