What is a UDT?
Global Systems Design Home
The quick answer is that UDT stands for User Defined Type. Visual Basic has many built in data types including string, long, single, etc... These are simple data types that represent discrete values held in a variable.

Often it would be more convenient to be able to define a data type that is more complex, having more than one discrete value so that coding the manipulation of data involves less coding or a more intuitive coding scheme.

Visual Basic allows the programmer to define more complex data types using combinations of the simpler data types to construct them.

To dig in a bit further let's consider a scenario where the programmer must deal with records which represent  people and their accompanying attributes.

Many applications need to keep track of a person's name, their address, phone number, and/or other data about the person. The process of loading in all of the attributes of a single person from a file and displaying them in the interface form can get quite unwieldy if the programmer attempts to use single discrete variables for each attribute. He could define several arrays to keep track of each attribute, one for names, one for street address, one for phone number.

Along with each of these arrays if would be necessary to also define an array index variable to keep track of where we are in each array. It should be obvious that it wouldn't take much of a mistake to wind up with the wrong attributes being displayed in a record with a particular name.

User defined types collect these attributes together in a data structure which can be used to declare an array of people which is much easier to keep track of, as there is only one array index value.

To declare a user defined type you will need to add a module to your project as they cannot be defined within a form module. To do so select Insert, Module from the Visual Basic IDE menu. This module has no form interface, just code. Use the View Code button to view the code for the module. In it's General, Declarations section is where you will define your user defined type.

Here is an example of the declaration section from a module which defines a user defined type.

Public Type Person
    BirthName As String
    StreetAddress As String
    City As String
    State As String
    ZipCode As String
    PhoneArea As Integer
    PhonePrefix As Integer
    PhoneSuffix As Integer
    Comments As String
End Type

Public People() As Person

In this sample Person is the user defined type. Using the newly defined type an array of People has been further defined. It now becomes much simpler to refer to any attribute of a particular person in code. Supposing that an array index variable named ArrayPosition has also been defined, we can now assign values to data with dot notation in the following manner.

     People(ArrayPosition).BirthName = "Joseph Jones"
     People(ArrayPosition).StreetAddress = "123 3rd Street"

Ok, even that can get to be quite a bit of repetitious typing while coding, so Visual Basic has kindly provided us with the With statement which makes the process even easier. Consider the following example:

     With People(ArrayPosition)
          .BirthName = "Joseph Jones"
          .StreetAddress = "123 3rd Street"
          .City = "Anytown"
     End With

As you can see it is now much easier to refer to any individual attribute of a person when using a user defined type. Be sure to include the dot before each term within the With statement.

If you are careful to sum the size of the individual elements of the user defined type to arrive at it's total size for each record, and then in turn sum the size of all of the records stored in the People() array you can write the entire array of records out to file in one operation, and then read it back in in one operation as well.

Now that you know how to define a user defined type I'm sure you can think of many other uses for them. To see how to read and write arrays of user defined types to and from a file see Reading and Writing UDTs to files.
 

C Ray Parrish, Cottage Grove, Oregon