Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

Classes vs. UDTs

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
DaveyByrne
Asked:
DaveyByrne
  • 4
  • 3
  • 2
  • +4
1 Solution
 
WolfgangKoenigCommented:
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
 
Nitin SontakkeDeveloperCommented:
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
 
KDivadCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
JoebobCommented:
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
 
DaveyByrneAuthor Commented:
Anyone got a link to a white paper, or some article about this?
0
 
DaveyByrneAuthor Commented:
Anyone got a link to a white paper, or some article about this?
0
 
rkot2000Commented:
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
 
rspahitzCommented:
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
 
JoebobCommented:
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
 
JoebobCommented:
The above performance statement is from a recordset with around 6000 records.
0
 
rkot2000Commented:
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
 
rspahitzCommented:
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
 
KDivadCommented:
<< 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
 
JoebobCommented:
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
 
KDivadCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now