Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

UDT collections

Status
Not open for further replies.

BigInch

Petroleum
Jun 21, 2006
15,161
It seems that a User defined type in VBA does not have a collections property. Is this correct? And if so, what is a good workaround for accessing the .count of a UDT array.

17-1058074210T.gif
 
Replies continue below

Recommended for you

I just happen to have been reading about collections this morning in Professional Excel Development. I'm not familiar with their use, but I think you need to create a collection of your UDT objects, and this collection will then have a count property.

This link might be useful:

Doug Jenkins
Interactive Design Services
 
Size can be found from array index limits which can be found with lbound and ubound...
Code:
Type mytype
  x As Double
  y As Double
End Type

Sub testtype()
 Dim myvar() As mytype
 ReDim myvar(3 To 9)
 Debug.Print "Size of myvar is " & UBound(myvar) - LBound(myvar) + 1
End Sub
Gives
output said:
Size of myvar is 7
For multidimensional array lbound and ubound can take a 2nd argument to identify which index is being tested.

=====================================
(2B)+(2B)' ?
 
EPete, Yes, I was familiar with the UL/bound statement, but what I was after was getting the number of items with somthing like myvar.Count and I couldn't get that to work. It works in VB6 and it surprized me when it apparently didn't in VBA, since they are usually very similar.

I'll try IDS's suggestion, read up on it and figure out which will be the most convenient.

Thanks to both of you.

17-1058074210T.gif
 
IDS, I find UDTs very convenient to use in VB6. I'm sure you'll get to like them. You define an attribute of the type and its instantly available everywhere without all the bookkeeping of making a separate unconnected array of object attributes and keep indexing those to the original object array. Very easy to add attributes "on the fly". VBA UDTs seem to be composed of a smaller subset of functions than those available in VB6, that for some reason don't include the ".Count" functionality automatically, and I would imagine some others are missing that I haven't discovered yet as well.

17-1058074210T.gif
 
BigInch - I think you are right, I could well find UDTs useful in situations where I currently tend to use arrays. Many books gloss over Types as being a sort of second-class Class, and I have never really looked into them as a result.

I think my original response was misleading. I was thinking of making a collection of instances of the Type, then that would have a .count property, but if you want a .count of the attributes of the Type itself, I don't know, and a search didn't find anything useful. If I come across anything I'll let you know.

Doug Jenkins
Interactive Design Services
 
I like using Types especially for program development where I tend to not have a well planned attribute list, for example, I'll start with

Type FlowElement
Diameter as ..
Wall_Thickness as ..
Yield_Strength as ..
End Type

Then figure out I need to add FromNode, ToNode, Length, or CV coefficient or something, so I can do some newly thought out calculations I just discovered I need. Really easy to just add them into the definition, define what they are in the sub and automatically keep each attribute indexed to its corresponding item in the FlowElement "array".

No. The article you gave me was good. I never heard about the Dictionary thing before either!

Cheers^2

17-1058074210T.gif
 
There are two separate concepts:

One concept is array vs collection. Collection has count property, array does not.

Another concept is is user defined data type vs other data types. It is entirely different distinction. Either type could be used in array or collection. The fact that an array doesn’t’ have a count property has nothing to do the fact that it is a user-defined data type. Arrays of double, Booleans etc also have no count property.

Maybe this is already understood by all… I’m not sure. If it is understood, then please feel free to ignore my comments.


=====================================
(2B)+(2B)' ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor