Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UDT's v Variables 1

Status
Not open for further replies.

abrewis

Programmer
Oct 16, 2001
37
0
0
GB
I wonder if anyone could let me know what are the main advantages / disadvantages of creating a UDT, for example:

Private Type myPerson
strName As String
intAge As Integer
lngHeight As Long
lngWeight As Long
End Type

over declaring these variables separately as below:

Dim strName As String
Dim intAge As Integer
Dim lngHeight As Long
Dim lngWeight As Long

What would be the difference in memory usage?
Many thanks in advance
 
One of the advantages can be derived from your previous example. Suppost that you need an array for these people, with their names, ages and width and heights, and of different types, in this case, String, Integer, and Long.

One approach is to have four separate arrays

Dim strName(10) As String
Dim intAge(10) As Integer
Dim lngHeight(10) As Long
Dim lngWeight(10) As Long

and of course you have to manage your subscripts to insure that the relationships do not get misaligned.

Consider the UDT approach

Private Type myPerson
strName As String
intAge As Integer
lngHeight As Long
lngWeight As Long
End Type

Dim ThePeople(10) as myPerson

Now there is only one array, and the relationships between name and attributes is not subject to mismanagement of subscripts.

In more general terms, you are established a multi-dimensional array of varying types, with the UDT representing the second dimension of the array. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Oh, for a moment there I though you were asking about ADT's (abstract data types). My professors in the dark ages of computer science talked about them.

Nowadays they're known as objects.
:)

UDT's (the ones you were asking about) are still useful. They're still available in .NET.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top