Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 Object and data types

Tags:

vb6

I understand that the class Object is at the top of the class hiearchy in an Object Oriented programming langauge, like Java. I also understand that you have reference types and value types in .NET. I also understand type definitions in C based languages.

Despite this; I am struggling to understand what an Object is in VB6( http://msdn.microsoft.com/en-us/library/aa338034%28v=vs.60%29.aspx ) and what exactly a variant is. What is a variant? and how is an object implemented in VB6?

like image 384
w0051977 Avatar asked Apr 10 '12 20:04

w0051977


People also ask

What are the data types in Visual Basic 6?

Data types in Visual Basic 6. The fundamental data types in Visual Basic including variant are integer, long, single, double, string, currency, byte and boolean. Visual Basic supports a vast array of data types. Each data type has limits to the kind of information and the minimum and maximum values it can hold.

What are the data types of variables in VB?

When a variable is declared, a data type is supplied for it that determines the kind of data they can store. The fundamental data types in Visual Basic including variant are integer, long, single, double, string, currency, byte and boolean.

What type of data type is an object variable?

Data Types. The Object data type is a reference type. However, Visual Basic treats an Object variable as a value type when it refers to data of a value type.

What is COM object in VB6?

All objects used by VB6 are COM objects. A COM object is essentially a variable length data structure whose variable length header contains any number of 32 bit pointers to VTables, and sucessive bytes contain the instance data of the object. For instance, Bytes 0-3 VTable1 pointer 4-7 VTable2 pointer 8-11 VTable3 pointer ...


1 Answers

All objects used by VB6 are COM objects. A COM object is essentially a variable length data structure whose variable length header contains any number of 32 bit pointers to VTables, and sucessive bytes contain the instance data of the object. For instance,

Bytes
0-3    VTable1 pointer
4-7    VTable2 pointer
8-11   VTable3 pointer
...
       Instance data

A VTable is an array of 32 bit pointers to functions which all are passed a "this" instance pointer.

Bytes
0-3    Func1(this, ..., ...)
4-7    Func2(this, ..., ...)
8-11   Func3(this, ..., ...)
...

The only other specification is that all VTables MUST inherit from IUnknown, i.e. the first three functions must be:

QueryInterface()
AddRef()
Release()

Essentially, QueryInterface() allows you to find out whether a COM object supports a specific interface (which is represented by an UUID). AddRef() allows the object writer to increment an internal reference count. Release() allows the object writer to decrement the reference counter, destroying the object when the count is zero. You never call these methods in VB - the compiler adds these calls for you (one of the advantages of VB6).

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms680509(v=vs.85).aspx for more details.

A VB 'Object' type is a reference to an object which supports the IDispatch interface (see http://msdn.microsoft.com/en-us/library/windows/desktop/dd318520(v=vs.85).aspx). This is what allows you to do late binding in VB and VBScript. All objects written in VB6 automatically implement an interface that inherits from IDispatch. This is called a dual interface, because it supports early and late binding.

Note that there is no direct type system built into COM. However, you can opt to support the ITypeInfo interface, which allows the users of your object to access the information you want to add about the object (it is easier to use the default implementation which uses type libraries to store this information).

The Variant type, as mentioned by Bob Riemersma, is actually a 16 byte structure which has a 2 byte integer (vt) which indicates what Automation type is being encapsulated, and the latter 8 bytes can be used to contain value types of up to 8 bytes, or a 32 bit pointer to another type. VB does all the necessary conversion between VB types and Variants using its internal functions, and all the necessary memory allocations and deallocations. Variants can contain references to COM objects by copying the pointer to the object into the Variant, and calling the object's AddRef() method.

like image 182
Mark Bertenshaw Avatar answered Oct 08 '22 13:10

Mark Bertenshaw