Advanced User Defined Types
Follow up to my first article on User Defined Types. Shows how to really put them to work. If you liked the first one, you will LOVE this one.
Original Author: Matt Roberts
Code
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
style='font-size:16.0pt;mso-bidi-font-size:12.0pt;mso-bidi-font-family:Arial'>Creating
Multi-Dimensional
User Defined Types
This is a follow-up
for my tutorial “href="http://www.planetsourcecode.com/xq/ASP/txtCodeId.8370/lngWId.1/qx/vb/scripts/ShowCode.htm">Create
your own User Defined Types – A Basic User Defined Type Tutorial.”style="mso-spacerun: yes"> You should read it first if you are not
familiar with User Defined Types.
In my first
article, I showed how to easily create you own custom data storage types. With
these, you could keep related pieces of information in one easy to use place.
For example, you could have a “Customer “ user defined type and store
information like this:
style='mso-tab-count:1'> Customer.FirstName = “John”
style='mso-tab-count:1'> Customer.LastName = “ Smith”
In addition to
being able to tie these pieces of data together in one variable name
(customer), you also have the really cool ability to see your choices in a
drop-down list just like the built-in Visual Basic object properties.
In this article, I
would like to show you how to expand that capability to multiple instances of
the user defined type. Let me explain. It is nice to have a variable in your
application that holds similar information, but what if you are working with
three different customers and want to manage information for all of them? There
are a couple of ways to accomplish this:
Consider what you
do if you want to hold several strings separately:
Dim strOne As
String
Dim strTwo As
String
Dim strThree As
String
You can define as
many variables as you like with the type of “string” because “string” is a
Visual Basic data type. Well VB gives you the power to create your own data
types, made up of standard variable types.
To do the same
thing with a user defined type, do this:
Type Customer
FirstName As String
LastName As String
Phone As String
DOB as Date
End Type
Now you can create
multiple variables of the same type:
Dim Customer1 As
Customer
Dim Customer2 As
Customer
Dim Customer3 As
Customer
Just as with any
other variable type, you can add different information to each and it will
remain with the variable you assigned it to:
Customer1.Phone =
“555-1234”
Customer1.FirstName=”John”
Customer1.LastName=”Smith”
Customer2.Phone =
“555-1111”
Customer1.FirstName=”Jane”
Customer1.LastName=”Doe”
Customer3.Phone =
“123-4567”
Customer3.FirstName=”Jane”
Customer3.LastName=”Doe”
How is that for
cool?
But wait, it gets
better. What if you don’t know how many customers you will be working with?
What then? Do you create 100 of these variables and hope you never need more?
Certainly not! Again, think about how you would do it with a string variable:
style='mso-special-character:line-break'>
Dim strTest(4) As
String
This creates a
string array with 4 elements. You can access each element by changing the index
number:
style='mso-tab-count:1'> StrTest(0) = “Hello”
style='mso-tab-count:1'> StrTest(1) = “How”
style='mso-tab-count:1'> StrTest(2) = “Are”
style='mso-tab-count:1'> StrTest(3) = “You?”
Doing this:
style='mso-tab-count:1'> For intTest = 0 To 3
style='mso-tab-count:2'> Msgbox strTest(intTest)
style='mso-tab-count:1'> Next intTest
Will loop through
this array and put each element in its own message box. Why? I have no idea…but
I am trying to make a point here.
You can do the same
thing by defining the type YOU created as an array:
Dim MyCustomers(4) As Customers
style='mso-tab-count:1'> MyCustomers(0).FirstName = “John”
style='mso-tab-count:1'> MyCustomers(0).LastName = “Smith”
MyCustomers(1).FirstName = “Jane”
style='mso-tab-count:1'> MyCustomers(1).LastName = “Doe”
MyCustomers(2).FirstName = “Sue”
MyCustomers(2).LastName = “Thomas”
style='mso-tab-count:1'> MyCustomers(3).FirstName = “Al”
style='mso-tab-count:1'> MyCustomers(3).LastName = “Anderson”
style='mso-tab-count:1'> For intTest = 0 To 3
style='mso-tab-count:2'> Msgbox
MyCustomers(intTest).FirstName
style='mso-tab-count:1'> Next intTest
At this point, your
User Defined Type starts to resemble a recordset in many ways, but requires
much less overhead than a recordset object does. If you use your imagination,
you can see how this would be very powerful when you substitute a variable in
your array declaration like this:
Dim MyCustomers(intCustomerCount) as Customers
Then you can loop
through the collection by incrementing an index variable. In this example, you
would add all customers to a listbox control on a form.
For intCustNumber = 0 to Ubound(MyCustomers) – 1
style='mso-tab-count:2'> ListBox1.Add MyCustomer(intCustNumber).FirstName
& MyCustomer(intCustNumber).LastName
Next intCustNumber
I have found many
uses for this concept in my applications and am sure that if you are curious
enough, you will as well. If you come up with some novel uses for it, please
email me and let me know: mmroberts@usa.net
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.