Storing objects in the Tag property

Storing objects in the Tag property

The Tag property exposed by many Windows Common Controls (including TreeView’s Node objects and ListView’s ListItem objects) is more versatile than the ListBox and ComboBox controls’ ItemData property, because it is of Variant type and can therefore accept values of most data types.

However, it can’t be used to store an object reference, probably because of an implementation bug. If you want to associate an item to an object you can use the Tag property as an index into an object array, but fortunately there is a simpler and more efficient method.

When you want to store an object reference into a Tag property, assign it the pointer to the object (a Long value):

' assumes that a CPerson class module is defined' elsewhere in the current projectDim myObject As New CPersonmyObject.FirstName = "Francesco"myObject.LastName = "Balena"' currNode is a TreeView's Node objectcurrNode.Tag = ObjPtr(myObject)

When you later need to rebuild the object reference, you use the CopyMemory API function to manually store the object pointer into a suitable object variable:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _    Any, source As Any, ByVal bytes As Long)Dim myObject As CPersonCopyMemory myObject, CLng(currNode.Tag), 4' here you use myObject's methods and properties' ....' you must manually destroy this reference before it' gets out of scope, or VB will crashCopyMemory myObject, 0&, 4

You can use the same technique with the ItemData property of ListBox and ComboBox controls. It is mandatory, however, that the object be referenced by at least another object variable, because the weak reference held in the Tag property doesn’t keep it alive. If the object is destroyed and you later try to access it in the way described above, you get a runtime error or – worse – a GPF error.

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

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing