Misconceptions on variables and binding

Misconceptions on variables and binding

Consider this line of code:

Dim x as Project1.Class1

Many developers think that we were declaring x as a “Project1.Class1” object. This is wrong. All object variables in VB are interface variables. In this line of code x is declared as the _Class1 interface defined in the Project1 Type library (the underscore is hidden by the kindness of the VB team). Note that in the above line of code you have not made any assumption regarding what object will provide an implementation of the Project1.Class1 interface. You create the actual object only later:

Set x = CreateObject("Project1.Class1") 'ORSet x = New Project1.Class1 'OR

One thing to stress: these 2 lines of codes are totally equivalent regarding early or late binding. There is a common misunderstanding among VB developers. They think that if you use the New keyword you are early-binding and that if you use CreateObject you are late-binding. Again, this is uncorrect. if you want to late-bind to the Project1.Class1 interface (that is use its Dispinterface counterpart) you should have written:

Dim x as Object  ' read Object as Dispatch

The confusion arises from the fact that VB hides you from most of the details of interface and classes definition.

– In the line [Dim x as Project1.Class1] Project1.Class1 is an interface indentifier (IID)
– In the line [Set x = New Project1.Class1] Project1.Class1 is a class identifier (The class CLSID)
– In the line [Set x = CreateObject(“Project1.Class1”)] “Project1.Class1” is the ProgId of the class, the ProgId is the human-readable name of the class that is mapped to the CLSID. This mapping is stored in the registry when you register your COM component (e.g. running regsvr32.exe if the component is a dll).

If you create an object via the New keyword, the CLSID is read from the referenced component type library at build time and hardcoded in your component. If you use CreateObject VB queries the registry at run-time the map the ProgId to the CLSID (The CLSID is what you have to pass to the COM run-time when you ask it to create an object). As you may know you can even write:

Dim x as New Project1.Class1

In this case [Project1.Class1] is a class identifier. You are asking VB to create the Project1.Class1 class, ask to the object its default interface and place it into the x variable.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes