Solved

Classes vs. UDTs

Posted on 2001-08-09
15
571 Views
Last Modified: 2012-05-04
Hi there,

a couple of small ones :

1. Is it possible to have collections of UDT's (User-Defined-Types)

2. Which is 'better' to use, Collections of Classes, or Arrays of UDT's. (or collections of UDT's if possible)


Can anyone point me towards a white paper, or a VB standards document which outlines the pros and cons of using the different methods.

Lots of detail will be appreciated, that's why I'm offering 100 points for a non-techy question...


Thanks in advance,

Davey.
0
Comment
Question by:DaveyByrne
  • 4
  • 3
  • 2
  • +4
15 Comments
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6367451
1.) No this is not possible ..., but you can paste the UDT in a class module

2.) Collections of class types are very simple to handle, but when you decide to use no object based methodology then UDTs are your way.
But in same circumstances the using of UDTs are needed. For instance:  
Type definitions for Win32 api calls from VB code

WoK
0
 
LVL 6

Expert Comment

by:Nitin Sontakke
ID: 6367457
Give me no points but my personal opinion is as follows:

I prefer UDTs over classes. Primarily because the scope. I might be wrong, but as i remember, UDTs cannot be public and cannot be used as properties. This limits their usage and passing them between forms becomes very difficult.

Classes on the other hand are definitely heavier than UDTs but they give much cleaner implementation choice, properties which can be used for validation before assignment, and are easy to maintain.

0
 
LVL 5

Expert Comment

by:KDivad
ID: 6367541
UDTs can be public and can be used as parameters (or return types). The trick is that the UDT and the function/sub it's passed to must both be Public in a .bas module.

Collections of UDTs:
I think VB6 will let you (was given VB6 code that did it), but VB5 won't.

Collections of Classes vs. Array of UDTs:
Depends on what you're doing with it. Collections are slower and bigger than Arrays. Classes are slower and bigger than UDTs. Collections give you easier access than Arrays. Classes allow you to react to the data passed/requested whereas a UDT doesn't.

Personal opinion?:
Use the array of UDTs unless you can't work without some of the features of classes or collections.
0
 
LVL 3

Accepted Solution

by:
Joebob earned 100 total points
ID: 6367859
And now you will get the real answer:

UDT's and Classes can both be added to collections.  Anything that can be converted into a variant can be placed in a collection.  The catch is that a UDT can only be converted into a variant if it is in a public class module in an ActiveX DLL, EXE, or OCX.

Which object is better depends on what you are doing.  If you simply need a data structure, I would use a UDT because they require much less system resources and are therefore much faster.  But if you need to enforce business rules such as restricting the possible values for a particular data element, this is the purpose of classes, data encapsulation.  A UDT is just a REALLY simple class.

The other part of your question was array or collection.  A dynamic array is slightly faster than a collection so if you need performance, but don't need to few extra features that a collection exposes, use the array.  If you care more about ease of programming than about performance, or if you need the features of a collection, use the collection.
0
 
LVL 1

Author Comment

by:DaveyByrne
ID: 6367980
Anyone got a link to a white paper, or some article about this?
0
 
LVL 1

Author Comment

by:DaveyByrne
ID: 6368026
Anyone got a link to a white paper, or some article about this?
0
 
LVL 5

Expert Comment

by:rkot2000
ID: 6368666
you can find some info on support.microsoft.com

i am using classes for the following reason :

PRB: Passing a UDT To Or From an ActiveX EXE May Fail on NT



SYMPTOMS
In Visual Basic 6.0, user-defined types (UDTs) can be arguments or return types of public properties or methods. Trying to pass or receive a UDT using an ActiveX EXE (out of process server)on NT 4.0 will fail if Service Pack 4 for NT 4.0 is not installed.

or this :

When you open a project and do anything that uses IntelliSense, you receive a "permission denied" error message when you try to recompile the project. The project defines a public User Defined Type (UDT) that it uses as a parameter to a public function, and binary compatibility is set.
 

other links :
http://support.microsoft.com/support/kb/articles/Q230/1/14.ASP?LN=EN-US&SD=gn&FR=0&qry=udt&rnk=30&src=DHCS_MSPSS_gn_SRCH&SPR=VBB

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 22

Expert Comment

by:rspahitz
ID: 6368831
Personal experience:

We have an app created in VB using a UDT to represent a DB structure.  Over the years, this UDT has been expanded.  Each expansion has caused great distress because it requires reconfiguring.  However, that wasn't too bad.

The problem came in when we found that a field defined as integer should have been a single, and all hell broke loose.  Since the UDT was reading and writing files, we lost compatibility when we changed the definition and some of our customers' installations broke with our new software.

If we were using a DB instead of a UDT, this wouldn't have happened.

So...
simply be careful of how you use your UDT.

Personally, I'd rather use classes over UDTs because they are encapsulated (i.e. stand alone and easier to maintain as such.)  I'd only use UDT for speed purposes, which doesn't come up often these days.
0
 
LVL 3

Expert Comment

by:Joebob
ID: 6369285
rspahitz, I agree with most of what you said.  One exception is that if I/you/anyone need to maintain a block of data and don't plan on encapsulating business rules and such, a class is going overboard, particularly if I/you/etc. need to have an array of say 1000+ elements.  I have a project now that lets the developer choose what they want returned to them, a dynamic array of UDT's takes about 10ms to return, a dynamic array of classes takes 2 to 3 seconds!  The performance difference is HUGE!

Regarding the ease of changing a element from 1 datatype to another.  Regardless of whether you are using properties in a class or elements of a UDT, changing a datatype requires breaking compatibility and a very high risk of destroying your app if it is not done carefully.
0
 
LVL 3

Expert Comment

by:Joebob
ID: 6369290
The above performance statement is from a recordset with around 6000 records.
0
 
LVL 5

Expert Comment

by:rkot2000
ID: 6369298
joebob - can you use just a recordset? why do you want to convert to array or something else if you don't have bus rules?
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6369308
I said, "I'd only use UDT for speed purposes"

You said, "a dynamic array of UDT's takes about 10ms to return, a dynamic array of classes takes 2 to 3 seconds!  The performance difference is HUGE!"

I agree!!!

--
And you're right than in my case, the program would have broken whether I used UDTs or classes.  That's why I'm pushing for a DB, inspite of the extra overhead.  It's really a business decision based on using the proper tool for the job.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 6369783
<< The catch is that a UDT can only be converted into a variant if it is in a public class module in an ActiveX DLL, EXE, or OCX. >>

Perhaps. In VB5, that UDT could not be Public. It would have to be Private. Also, VB5 doesn't like converting UDTs to Variants regardless of the method you use. I've have tried this with several people, using several methods. As far as I can tell, it simply isn't possible...
0
 
LVL 3

Expert Comment

by:Joebob
ID: 6372349
rspahitz,
The thing I don't like with using the recordset deal is that you get the same basic thing as a UDT in the sense that you can't enforce business rules, but your client can also potentially change data in the database.

KDavid,
I don't know about in VB5, but in VB6, UDT's that are in public classes in activex projects can be converted to variants.  Maybe enough people complained about the lack of functionality in VB5 and MS decided to add it for VB6.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 6374354
I know it can be done in VB6, but I also know it can't be done in VB5. It's highly annoying! I had to completely overhaul a project because of this. I'm not sure why it can't be done. Even QB kinda allowed UDT->Variant!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA Excel: fill / replace values based on a config sheet. 5 57
z = x + y – 1 6 58
DIR issue 7 47
Microsoft Access combo box help 2 30
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now