Share via


User-Defined Data Type

Holds data in a format you define. The Structure statement defines the format.

Previous versions of Visual Basic support the user-defined type (UDT). The current version expands the UDT to a structure. A structure is a concatenation of one or more members of various data types. Visual Basic treats a structure as a single unit, although you can also access its members individually.

Remarks

Define and use a structure data type when you need to combine various data types into a single unit, or when none of the elementary data types serve your needs.

The default value of a structure data type consists of the combination of the default values of each of its members.

Declaration Format

A structure declaration starts with the Structure Statement and ends with the EndStructure statement. The Structure statement supplies the name of the structure, which is also the identifier of the data type the structure is defining. Other parts of the code can use this identifier to declare variables, parameters, and function return values to be of this structure's data type.

The declarations between the Structure and EndStructure statements define the members of the structure.

Member Access Levels

You must declare every member using a Dim Statement (Visual Basic) or a statement that specifies access level, such as Public (Visual Basic), Friend (Visual Basic), or Private (Visual Basic). If you use a Dim statement, the access level defaults to public.

Programming Tips

  • Memory Consumption. As with all composite data types, you cannot safely calculate the total memory consumption of a structure by adding together the nominal storage allocations of its members. Furthermore, you cannot safely assume that the order of storage in memory is the same as your order of declaration. If you need to control the storage layout of a structure, you can apply the StructLayoutAttribute attribute to the Structure statement.

  • Interop Considerations. If you are interfacing with components not written for the .NET Framework, for example Automation or COM objects, keep in mind that user-defined types in other environments are not compatible with Visual Basic structure types.

  • Widening. There is no automatic conversion to or from any structure data type. You can define conversion operators on your structure using the Operator Statement, and you can declare each conversion operator to be Widening or Narrowing.

  • Type Characters. Structure data types have no literal type character or identifier type character.

  • Framework Type. There is no corresponding type in the .NET Framework. All structures inherit from the .NET Framework class System.ValueType, but no individual structure corresponds to System.ValueType.

Example

The following paradigm shows the outline of the declaration of a structure.

[Public | Protected | Friend | Protected Friend | Private] Structure structname
    {Dim | Public | Friend | Private} member1 As datatype1
    ' ...
    {Dim | Public | Friend | Private} memberN As datatypeN
End Structure

See Also

Concepts

Efficient Use of Data Types

Reference

Data Type Summary (Visual Basic)

ValueType

Type Conversion Functions

Conversion Summary

Structure Statement

Widening

Narrowing

StructLayoutAttribute

Other Resources

Structures: Your Own Data Types