Hopefully this will be a quick and easy one for you
guys. :)
In my project I have many arrays. As you all know, using a
function like Ubound on an uninitialised array causes VB
to have a fit. Therefore, I thought I would write a little
function to check it for me. I did it like this:
Public Function ArrayIsInit(aTemp() As Items) As Boolean
Dim iDummy As Integer
On Error GoTo ErrHdlr_Array
iDummy = LBound(aTemp)
'if we make it this far, then the array is initialised
ArrayIsInit = True
Exit_Out:
Exit Function
ErrHdlr_Array:
'the array is not initialised
ArrayIsInit = False
Resume Exit_Out
End Function
Items is a user defined type. This works fine. However, I
have many arrays of different types, and what I want is
one function to check them all, seeing as Ubound can be
used on all arrays (I presume :)
So, I did this instead:
Public Function VArrayIsInit(aTemp() As Variant) As Boolean
Dim iDummy As Integer
On Error GoTo ErrHdlr_Array
iDummy = LBound(aTemp)
'if we make it this far, then the array is initialised
VArrayIsInit = True
Exit_Out:
Exit Function
ErrHdlr_Array:
'the array is not initialised
VArrayIsInit = False
Resume Exit_Out
End Function
I thought this would then handle any array. However, when
I try to send it an array of user defined types, I get
this error message:
Type mismatch: array or user-defined type expected
I called it like this:
bGoAnal = VArrayIsInit(g_AnalCapCol)
The definition of g_AnalCapCol is:
Public Type g_aCapt
aItem(4) As Items
End Type
The definition of Items is:
Type Items
Col(2) As Variant
End Type
I then tried changing the definition of the function to:
Public Function VArrayIsInit(aTemp As Variant) As Boolean
I then got this ponderous message:
Only public user defined types defined in public object
modules can be used as parameters or return types for
public procedures of class modules or as fields of public
user defined types
So what I would really like to know is: is there any way
of being able to send as a parameter to a function an
array of ANY type, even user defined types?
Of course, at the end of the day, the first problem was
checking to see if an array has been initialised, so if
anyone knows a flash way of doing this, that would be
great too.
Hope that makes sense, and many thanks…
Ron.
In your case you may split your logical code from your main project
and put it in an activeX and use it in you project.
Also, when you do Dim A(5) as Variant, the array A is dimensionned.
So your 2 types contain dimensionned array.
To check if an array is dimensionned I use that function:
Public Function IsArrayDimensioned(vrn_Var As Variant) As Boolean
Dim lngTest As Long
On Error Resume Next
lngTest = UBound(vrn_Var)
IsArrayDimensioned = IIf(Err = 0, True, False)
If I were you I'd go to www.mvps.org/vbnet/ and search on array. It will
come up with lots of hits. The 2nd one down is a very useful link -- click
on it. That's the very simple way. But, if you look in the "Related"
section, that will send you to a page that has a more robust (read
complicated but cool) method for telling you not only if an array is
initailized, but also how many dimensions it has. I like it a lot. And it
won't cause the IDE to break on an error when you are really just testing to
see if the array is initialized.
Matt
Global g_AnalCapCol() As g_aCapt
The definition of g_aCapt is:
Public Type g_aCapt
aItem(4) As Items
End Type
The definition of Items is:
Type Items
Col(2) As Variant
End Type
So my array consists of user defined types. I have been
trying to pass it to a function defined like this:
Public Function VArrayIsInit(aTemp() As Variant) As Boolean
VB hates this. (Kindly see my previous posting for more
details on this).
Basically what I want to be able to do is to pass an array
of ANY type to a function… an array of user defined types,
or of strings, or whatever.
Matt pointed me in the direction of a very good example
which uses pointers, which I tried, but sadly it uses
the ‘VarPtr’ function which doesn’t accept my array of
user defined types.
If the ActiveX dll is the only way to go, then so be it…
but this seems to be the real problem. I have many
different arrays of different user defined types you see…
if I were to use separate functions for each one they
would number many indeed.
Once again, many thanks, and apologies if my posting
earlier was a little confusing.
Ron.
Matt
I don't think so, but maybe you can find an answer by searching on
http://groups.google.com.
There's the more interresting post that I have find:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=39AA5EB1.5142E435%40hfx.andara.com&rnum=3&prev=/groups%3Fq%3DOnly%2Bpublic%2Buser%2Bdefined%2Btypes%2Bdefined%2Bin%2Bpublic%2Bobject%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D39AA5EB1.5142E435%2540hfx.andara.com%26rnum%3D3
Ron.
>-----Original Message-----
>
>I don't think so, but maybe you can find an answer by
searching on
>http://groups.google.com.
>
>There's the more interresting post that I have find:
>http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-
8&threadm=39AA5EB1.5142E435%
40hfx.andara.com&rnum=3&prev=/groups%3Fq%3DOnly%2Bpublic%
2Buser%2Bdefined%2Btypes%2Bdefined%2Bin%2Bpublic%2Bobject%
26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%
3D39AA5EB1.5142E435%2540hfx.andara.com%26rnum%3D3
>.
>
----
Public Type ItemUDT
Counter As Long
DateTime As Date
End Type
Public Enum ItemConstants
ItemCounter
ItemDateTime
End Enum
'!! These functions can be made to work with UDT arrays -
' I'm just too lazy to sit down and work the code - sorry! Gotta
' leave something for you to do! ;)
Public Function RItem(Arr As Variant) As ItemUDT
With RItem
.Counter = Arr(0)
.DateTime = Arr(1)
End With
End Function
Public Function CItem(ItemType As Item) As Variant
With ItemType
CItem = Array(.Counter, .DateTime)
End With
End Function
----
The other solution is to use classes - this will avoid the re & post
ops required.
Hope this helps!
Graeme.