Search notes:

VBA: User defined data types

In addition to the built-in data types, Visual Basic for Applications allows to create user defined datatypes (sometimes referred to as UDT).

Declararing user defined types

A user defined type is declared with the type statement:
type UDT
     e_lng as long
     e_str as string
end  type

dim v as UDT
v.e_lng = 42
…
Types can be declared global or private.

Returning from a function

A type can be returned from a function:
option explicit


private type XYZ ' {
    num as long
    txt as string
end type ' }


sub main ' {
    dim tp as XYZ

    tp = f

    debug.print "tp.num = " & tp.num
    debug.print "tp.txt = " & tp.txt
end sub ' }


function f as XYZ ' {
    f.num =  42
    f.txt = "Hello World"
end function ' }
Github repository about-VBA, path: /language/datatypes/user-defined/return-from-function.vb

Limitations

One of the biggest limitations of user defined data types is, imho, that they cannot be used as values in dictionaries.

See also

Memory layout of user defined datatypes.

Index