VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Advanced User Defined Types

by Matthew Roberts (26 Submissions)
Category: Data Structures
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (11 Votes)

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.

Rate Advanced User Defined Types

xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">






Creating Multi-Dimensional 





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


Arial'>FirstName As String


Arial'>LastName As String


Arial'>Phone As String


Arial'>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:


 


Arial'>Dim MyCustomers(4) As Customers 


 


style='mso-tab-count:1'>            MyCustomers(0).FirstName = “John”


style='mso-tab-count:1'>            MyCustomers(0).LastName = “Smith”


 


Arial'>MyCustomers(1).FirstName = “Jane”


style='mso-tab-count:1'>            MyCustomers(1).LastName = “Doe”


 


Arial'>MyCustomers(2).FirstName = “Sue”


Arial'>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:


 


Arial'>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.


 


Arial'>For intCustNumber = 0 to Ubound(MyCustomers) – 1


style='mso-tab-count:2'>                        ListBox1.Add MyCustomer(intCustNumber).FirstName
& MyCustomer(intCustNumber).LastName


Arial'>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: [email protected]





Download this snippet    Add to My Saved Code

Advanced User Defined Types Comments

No comments have been posted about Advanced User Defined Types. Why not be the first to post a comment about Advanced User Defined Types.

Post your comment

Subject:
Message:
0/1000 characters