User Type vs. Variable

R

RyanH

I have several user forms that I use to calculate costs of different types of
products. One userform per product. I would like to know the advantages and
disadvantages of using variables or my own user types. For example,

' user types in standard module
Type Sign
SignHeight As Double
SignWidth As Double
End Type

' userform button code
Sub Calculate()

Dim EMC As Sign

With EMC
.SignHeight = TextBox1 + TextBox2
.SignWidth = TextBox3 + TextBox4
End With

End Sub

VS.

Sub Calculate()

Dim SignHeight As Double
Dim SignWidth As Double

SignHeight = TextBox1 + TextBox2
SignWidth = TextBox3 + TextBox4

End Sub
 
J

Jim Thomlinson

User defined types are useful when you want to store mutiple pies of
information about a single thing. It gives you some of the benefits of
creating objects without having to go to the trouble of creating objects.

You can pass a single variable which stores multiple pieces of info about
that thing. In your example if you wanted to pass the sign to another
procedure you would have to pass the height and width and ... as individual
variables. If you want to add another variable to your sign you will need to
change your sub procedure definitions. As a Type you can pass just the single
variable and all of the info goes to the sub.

The other benefit is that it makes arrays easier to deal with. Instead of
having a multi dimensional array you can have a single dimensional array.
There are 2 benefits here. Multi dim arrays are a pain to debug as it is not
obvious what each dimension holds. Arrays hold groups of like data so you can
not mix strings and numbers unless you declare the array as type variant. If
you want to add another dimension with a UDT you just change the UDT
definition and you are ready to go. With multi dim array you may need to
change counters and such used to traverse through the array. With arrays of
UDT's you can redim easily. With Multi dim you can on redim the final
dimension which can be limiting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Force proper case on user form text box 7
Create a public variable 4
Help 8
Input box question 6
named range problem 4
Help with code please its urgent 1
userform and formula 5
Reset a User Defined Types Values 7

Top