Author: Dave
Date: 05.25.18 - 6:52pm

Early on in my coding life I started playing with vb6 user defined types. To C users they are known as structs.

They provide a way to group together a bunch of settings into one container . They really seem simple and easy. Their use also marks a certain stage of progression in a vb6 coders development.

public type myUDT
	field1 as byte 
	integer1 as integer
	buffer() as byte 
end type
On the surface they look great. You can define them easily and include multiple structures all in one module. They are compact and you know exactly how much memory they take up.

The problem is that VB6's language support for UDTs is not great. They come with scoping limitations. If you declare a private UDT, you can only use it in that one code module and not return it from any public function. Next thing you know many of your types must be public in a module and you have to search your code to find where they are used and defined. You can never port this code into an ActiveX dll.

UDTs can not be stored in a collection or variant type. You can only store them in individual variables or arrays pre defined to that specific type.

dim mu as myUDT
dim mua() as myUDT
Arrays get annoying quickly with having to call redim preserve ubound + 1 all the time to add new elements Arrays can also be empty in which case Ubound() throws an annoying error that you have to trap. Removing elements from arrays is also sucky leaving either blank spots to fill (which in the case of a udt you have to zero out and mark a flag) or you have to compact the array which is equally annoying.

If you create a addElement type function to wrap dynamic UDT adding..you have to create a new function for each UDT type..because remember they cant be held as variants.

I do not consider UDTs to be generally useful for everyday programming. There is a much better technique to use for VB6, namely classes.

With a class you can still define public variable types and they are all still grouped together in one container. It is a little annoying that each container requires its own text file. This seems bloated and annoying for a small group of settings, but you get used to it. The advantages are great and out weight this annoyance.

You can pass a class around and return it from a function anywhere in your project. You can also easily transfer portions of your project over to an ActiveX dll easily in the future once they are stable and unchanging. Also you can always find the class definition since its a distinct file and has its own node in the project treeview.

Class variables can be held in variant type variables so you can pass different classes to functions and the function can respond differently based on what input type its given. This can be powerful for similar but different classes.

Classes can also be added to a collection. This allows simple key based lookup (arrays do not). Collections also allow you to use for each to walk the elements which is nice and compact and automatically handles the case of an empty collection without throwing an error. Collections also allow you to easily add and remove elements with a single call.

Another benefit to classes over UDTs is that your class can contain a series of related methods that function on the data contained within that class. This logical grouping and inherant access to the class private variables allows these member functions clean design and reduces the number of variables being passed in.

Imagine the class has a public .value as long. Your function prototype can be as simple as isBitSet(bit) instead of a generic isBitSet(value,bit). In a class you can also protect certain values from user manipulation and validate changes to the value to take other actions when one value is set. In this scenario you use a private m_Value and public properties named Value to get and set the real value. Now you can sanity check how its used or ensure read only access to it.

VB6 UDTs do have a place in the language though. There are two places they are great (and even mandatory). Actually I would say these are the two places they were specifically designed for.

The first is for use in windows API and interacting with C Structs. They were a must have here. The second is for serializing data to disk in a binary structure. The UDT packing and unpacking routines when dumping to disk were specially constructed for this task. (Get and Put have special handlers for UDT that are complex and powerful).

I really think these are the two specific use cases UDTs were designed for. While they can be used for general data storage, they dont perform well in this role compared to classes.

Everyone can have their own opinion on the matter, but thats my insight and description of why after working with vb6 for the last 20 years.

Comments: (1)

On 10.20.20 - 11:07am Roy wrote:
This restriction about unable to place a UDT to Variant has been changed in VB6. See article https//www.brainbell.com/tutors/Visual_Basic/User_Defined_Types.htm. You can do it by putting your UDT definition in an ActiveX Dll and use it in your Exe.

Leave Comment:
Email: (not shown)
Message: (Required)
Math Question: 79 + 93 = ? followed by the letter: J 

About Me
More Blogs
Main Site
Posts: (year)
2024 (2)
     ffmpeg voodoo
     RegJump Vb
2023 (9)
     VB6 Virtual Files
     File handles across dlls
     python abort / script timeout
     VB6 Python embed w/debugger
     python embedding
     VB6 IDE Enhancements
     No Sleep
     A2W no ATL
2022 (4)
     More VB6 - C data passing
     Vb6 Asm listing
     Byte Array C to VB6
     Planet Source Code DVDs
2021 (2)
     Obscure VB
     VB6 IDE SP6
2020 (4)
     BSTR from C Dll to VB
     Cpp Memory Manipulation
     ActiveX Binary Compatability
2019 (5)
     Console tricks
     FireFox temp dir
     OCX License
     Extract substring
     VB6 Console Apps
2018 (6)
     VB6 UDTs
     VB6 Debugger View As Hex tooltips
     VB6 - C Share registry data
     VB6 Addin Missing Menus
     VB6 Class Init Params
     VB6 isIn function
2017 (6)
     Python and VB6
     Python pros and cons
     download web Dir
     vc rand in python
     VB6 Language Enhancement
     Register .NET as COM
2016 (22)
     VB6 CDECL
     UDT Tricks pt2
     Remote Data Extraction
     Collection Extender
     VB6 FindResource
     DirList Single Click
     Reset CheckPoint VPN Policy
     VB6 BSTR Oddities Explained
     SafeArrays in C
     BSTR and Variant in C++
     Property let optional args
     Misc Libs
     Enum Named Pipes
     Vb6 Collection in C++
     VB6 Overloaded Methods
     VB6 Syncronous Socket
     Simple IPC
     VB6 Auto Resize Form Elements
     Mach3 Automation
     Exit For in While
2015 (15)
     C# self register ocx
     VB6 Class Method Pointers
     Duktape Debug Protocol
     QtScript 4 VB
     Vb6 Named Args
     vb6 Addin Part 2
     VB6 Addin vrs Toolbars
     OpenFile Dialog MultiSelect
     Duktape Example
     DukTape JS
     VB6 Unsigned
     .Net version
     TitleBar Height
     .NET again
     VB6 Self Register OCXs
2014 (25)
     Query Last 12 Mos
     Progid from Interface ID
     VB6 to C Array Examples
     Human Readable Variant Type
     ScriptBasic COM Integration
     CodeView Addin
     ScriptBasic - Part 2
     Script Env
     MSCOMCTL Win7 Error
     printf override
     History Combo
     Disable IE
     API Hooking in VB6
     Addin Hook Events
     FastBuild Addin
     VB6 MemoryWindow
     Link C Obj Files into VB6
     Vb6 Standard Dlls
     CStr for Pascal
     Lazarus Review
     asprintf for VS
     VB6 GlobalMultiUse
     Scintilla in VB6
     Dynamic Highlight
     WinVerifyTrust, CryptMsgGetParam VB6
2013 (4)
     MS GLEE Graphing
     printf for VB6
     C# App Config
     Tero DES C# Test
2012 (10)
     VC 2008 Bit Fields
     Speed trap
     C# Db Class Generator
     VB6 vrs .NET (again)
     FireFox Whois Extension
     git and vb6
     Code Additions
     Compiled date to string
     C# ListView Sorter
     VB6 Wish List
2011 (7)
     C# Process Injection
     CAPTCHA Bots
     C# PE Offset Calculator
     VB6 Async Download
     Show Desktop
     coding philosophy
     Code release
2010 (11)
     Dll Not Found in IDE
     Advanced MSScript Control
     random tip
     Clipart / Vector Art
     VB6 Callback from C#
     Binary data from VB6 to C#
     CSharp and MsScriptControl
     HexDumper functions
     Js Beautify From VB6 or C#
     vb6 FormPos
     Inline Asm w VB6
2009 (3)
     The .NET Fiasco
     One rub on computers
     Universal extractor