User Defined Types (UDT) in VB

Level:
Level1

Written By TheVBProgramer.

User-defined Types (UDTs) are VB's way of implementing data structures. In C/C++, they are called structs, in Pascal and COBOL they are called records (they are also called "group" items in COBOL).

 

The following rules apply to UDTs:

  • UDTs may be declared only at the module-level (you may not declare a UDT in an individual Sub or Function)
  • UDTs may have Public (project-level) or Private (module-level) scope. If the keyword Public or Private is omitted, the default is Public.
  • UDTs with Public scope may only be defined in standard modules, not forms.

 

The syntax for defining a UDT is:

 

[Public | Private] Type TypeName

Variable1 As datatype

...

Variablen As datatype

End Type

 

For example, to define a UDT for an employee record, you might code the following:

 

Public Type EmployeeRecord

strEmpName As String

dtmHireDate As Date

sngHourlyRate As Single

End Type

 

However, the definition alone is not enough. The Type definition is basically a "template" on which other variables are defined; the template itself does not store any data. To use a UDT, you must define a variable "As" the name following the keyword "Type" (in this case, "EmployeeRecord"). For example:

 

Dim udtEmpRec As EmployeeRecord

 

The above defines a variable called "udtEmpRec" which has the attributes defined by the structure "EmployeeRecord". Thus, it is "udtEmpRec" which you refer to in your procedural statements, NOT "EmployeeRecord". To reference an individual element of the structure, you must qualify that element with the "udt" variable that you defined. For example, the following code places data in the individual elements of udtEmpRec:

 

udtEmpRec.strEmpName = "JOE SMITH"

udtEmpRec.dtmHireDate = #1/15/2001#

udtEmpRec.sngHrlyRate = 25.50

 

You can declare any number of variables "As" the UDT that you have defined. For example:

 

Dim udtEmpRec2 As EmployeeRecord

Dim audtEmpRec(1 To 10) As EmployeeRecord

 

Note that the second definition above declares an array of the EmployeeRecord type. To refer to an individual field (such as strEmpName) in a particular "record number", you would code something like the following:

 

audtEmpRec(5).strEmpName = "BILL JONES"

 

You can also have an array at the elementary level. For example:

 

Public Type EmployeeRecord

strEmpName As String

dtmHireDate As Date

sngHourlyRate As Single

dblQuarterlyEarnings(1 To 4) As Double

End Type

 

 

If you have this declaration:

 

Dim udtEmpRec As EmployeeRecord

 

Then a valid reference would be:

 

Print udtEmpRec.dblQuarterlyEarnings(2)

 

 

 

If you have this declaration:

 

Dim udtEmpRec(1 To 5) As EmployeeRecord

 

Then a valid reference would be:

 

Print udtEmpRec(3).dblQuarterlyEarnings(2)

 

 

You can have nested UDTs. Consider the following declarations:

 

Public Type EmployeeName

FirstName As String

MidInit As String

LastName As String

End Type

 

Public Type EmployeeRecord

udtEmpName As EmployeeName

dtmHireDate As Date

sngHourlyRate As Single

dblQuarterlyEarnings(1 To 4) As Double

End Type

 

Dim udtEmpRec As EmployeeRecord

 

You would reference the EmployeeName fields as follows:

 

udtEmpRec.udtEmpName.FirstName

udtEmpRec.udtEmpName.MidInit

udtEmpRec.udtEmpName.LastName

 

If you enjoyed this post, subscribe for updates (it's free)

Using a Type as a parameter or return value on a class method

Could you explain how I might use a user-defined Type (UDT) as a parameter or a return value on a Class method? I would like to do the following but run into errors:

Form code...

Private Sub cmdSendRequest_Click()
   Dim request As WebRequest   ' this is a class defined below
   Set request = New WebRequest
 
   Dim resp as Response   ' this is a UDT defined below within my class
   resp = request.Post("http://www.somewebsite.com")
 
End Sub

Class 'WebRequest' is defined with the following UDT and methods...

Public Type Response
   StatusCode as Integer
   StatusText as String
   ResponseBody as String
End Type
 
Public Function Post(ByVal url as String) as Response
   Dim resp as Response
 
   '... code to perform web request goes here
   '... after web request is performed the response is populated
 
   resp.StatusCode = 200
   resp.StatusText = "OK"
   resp.Body = (body text read from the web request code above is assigned here)
 
   Post = resp
 
End Function

When I go to run this code, I get the following Compile Error:

Cannot define a Public user-defined type within a private object module

Thanks for you help,
Brian

UDT parameter in a Class method

The syntax for defining a UDT is:

[Public | Private] Type TypeName
Variable1 As datatype
...
Variablen As datatype
End Type

In a Bas module define your as Public.

In your Class define your Method as Friend. You can now pass your UDT as a parameter.

I had been looking for this

I had been looking for this solution but could not find a suitable response on any other website. Your method of declaring the class as 'Friend' worked for me.

Thanks a lot

how to create delay in vb8??

how to create delay in vb8??

User defined type

HI,

is there an easy way to assign value to user-defined type. For example,

Public Type EmployeeRecord
strEmpName As String
dtmHireDate As Date
sngHourlyRate As Single
End Type

Dim Test(10) As EmployeeRecord

is there a easy way to assign values to the Type Array during program instead of assigning it individually?

Test(0).strEmpName = "William"
Test(0).dtmHireDate = "03-Oct-09"
Test(0).sngHourlyRate = 30

is there a single program statement that can achieve the same as above?

Do a Loop... or use UDT

Do a Loop... or use UDT within another UDT

How to calcultae the Size of Array of UDT

Can anyone help me, in getting the size of array of udt. Example code included.

public type MyUDT
FirstName as string * 255
LastName as string * 255
end Type

dim Test as MyUDT

msgbox Len(Test)= 510

But how to get the size of array of MyUDT

dim Test() as myUDT
Redim Test(0 to 1)

msgbox Len(Test)=4 ' Here Instead of 1020 only 4 is displayed..

Please guide me. where i m wrong....

i dont know! sorry

i dont know! sorry

UDT's

This is very helpful, thank you.
Programming this simple Winsock-based multiplayer game should now be a breeze.

User Defined Types (UDT) in VB

I'm currently using user defined type as an event return value ( as an event parameter ) but I'm an getting an error message:

"Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules or as fields of public user defined types"

Any ideas? thanks

UDT

Try using "Friend" in lue of Public
something like this

Friend Property Get BarFont() As UDT_BARTEXTFONT
BarFont = cBarTextFont
End Property

Friend Property Let BarFont(bfont As UDT_BARTEXTFONT)
cBarTextFont = bfont
End Property

This stinks

grr... for some god-forsaken reason, a Type cannot reference itself. Modern VB (structures) can... but VB6 is really ghetto... grr

So if you hope to do something like this:

Public Type MyType

id as Integer
children() as MyType

End Type

... you can forget it

Why forget it? Try using the

Why forget it? Try using the "modern" structures available within VB6. If you want to define that type of relationship, define a class.