Under The Hood in a VB Method Call
There is actually quite a bit of stuff going on behind the scenes in a VB method call. I am going to try to lay out the big picture here, as I understand it. Here goes.....
When you implement a VB class object, the code that is that class is loaded into memory. As you create more instances of the class, you might expect more instances of the code for the methods and properties of the object to be loaded into memory. This is not the case. What happens is each new instance gets a new area in memory that holds all of the private data associated with it. However, there is only one instance of the actual code. This is a very efficient memory model. When a method call is made into the code block, a pointer to the correct instance is passed as the first parameter to the call. In C and C++, the pointer is known as the 'this' pointer in VB it is the 'Me' object. This parameter is hidden from you by VB but is referenced behind the scenes whenever module level data is accesses in a method call. The memory model looks something like this:
If VB didn't hide the pointer to the instance data, your method calls in your classes would look something like the following.
Public Sub SomeMethod(ByVal Me As cYourObject, ByVal OtherParams......)
When the compiler compiles your code, it automatically resolves any references to module level data for you using the 'this' pointer. If we replace a pointer in the Vtable of an object and point it at a function in a VB BAS module, we need to allocate a parameter for the pointer to the instance data that is passed in the method call. Here is the declaration for the IEnumVARIANT_Next function in the MIEnumVARIANT.bas module of the CSuperCollection sample project.
Public Function IEnumVARIANT_Next(ByVal this As IEnumVReDef.IEnumVARIANTReDef, _
ByVal cElements As Long, _
avObjects As Variant, _
ByVal nNumFetched As Long) As Long
Notice the first parameter. This 'this' pointer points to the data in the calling instance of the class and is a fully referenced pointer that can be used to access any of the members of the interface (in this case the IEnumVARIANTReDef interface).
vbVision Home | Super Collections