article

Advanced User Defined Types

Email
Submitted on: 1/10/2015 11:13:00 PM
By: Matthew Roberts (from psc cd)  
Level: Intermediate
User Rating: By 11 Users
Compatibility: VB 6.0
Views: 895
 
     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.


 
				






Creating Multi-Dimensional 
<--[if gte mso 9]>
 
 Matt Roberts
 Matt Roberts
 3
 37
 2001-01-11T22:44:00Z
 2001-01-11T23:01:00Z
 2
 624
 3557
 Televox Software
 29
 7
 4368
 9.3821
 




Creating Multi-Dimensional

User Defined Types

 

 

This is a follow-up for my tutorial “Create your own User Defined Types – A Basic User Defined Type Tutorial.” 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:

 

Customer.FirstName = “John”

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:

Dim strTest(4) As String

 

This creates a string array with 4 elements. You can access each element by changing the index number:

 

StrTest(0) = “Hello”

StrTest(1) = “How”

StrTest(2) = “Are”

StrTest(3) = “You?”

 

 

Doing this:

 

For intTest = 0 To 3

Msgbox strTest(intTest)

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

 

MyCustomers(0).FirstName = “John”

MyCustomers(0).LastName = “Smith”

 

MyCustomers(1).FirstName = “Jane”

MyCustomers(1).LastName = “Doe”

 

MyCustomers(2).FirstName = “Sue”

MyCustomers(2).LastName = “Thomas”

 

MyCustomers(3).FirstName = “Al”

MyCustomers(3).LastName = “Anderson”

 

For intTest = 0 To 3

Msgbox MyCustomers(intTest).FirstName

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

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


Other 25 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.