Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VB6 - Is array initialised?

0 views
Skip to first unread message

Ron Hope

unread,
Jul 31, 2002, 8:33:59 AM7/31/02
to
Hi all,

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.

Benoit Poirier

unread,
Jul 31, 2002, 10:13:31 AM7/31/02
to
You g_AnalCapCol is not an array, it's a type that contain an array of
Items, no? About the public object module, you have to transfert your
UDT in the default class of an ActiveX dll, then and add a reference
to that dll in you project. Using that dll with multiuse will make it
public.

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)

Matt

unread,
Jul 31, 2002, 10:54:02 AM7/31/02
to
"Ron Hope" <ronf...@yahoo.co.uk> wrote in message
news:303401c2388e$8b264dd0$a5e62ecf@tkmsftngxa07...

> Hi all,
>
> 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:

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


Ron Hope

unread,
Jul 31, 2002, 1:44:34 PM7/31/02
to
I would like to thank those who have taken the time to
reply so far, but I&#8217;m afraid my problem still persists,
although it is clearer now. The extra tips on how to tell
if an array has been initialised or not were very helpful&#8230;
but it seems that my main problem is that I need a way of
being able to pass an array of ANY type to a function.
Benoit suggests that this could be achieved if I were to
make it part of an ActiveX dll&#8230; but is there no other way
to do this? Does anyone know of a way to pass an array of
user defined types to a function? My variable
(g_AnalCapCol) is built like this:

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&#8230; 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 &#8216;VarPtr&#8217; function which doesn&#8217;t accept my array of
user defined types.

If the ActiveX dll is the only way to go, then so be it&#8230;
but this seems to be the real problem. I have many
different arrays of different user defined types you see&#8230;
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

unread,
Jul 31, 2002, 4:41:31 PM7/31/02
to
Puke. I don't know. I played with this for about 10 minutes, and I'm
stumped (not that surprising).

Matt


Benoit Poirier

unread,
Aug 1, 2002, 9:13:29 AM8/1/02
to
>Benoit suggests that this could be achieved if I were to
>make it part of an ActiveX dll&#8230; but is there no other way
>to do this? Does anyone know of a way to pass an array of
>user defined types to a function?

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 Hope

unread,
Aug 3, 2002, 11:01:58 AM8/3/02
to

Thanks Benoit, I'll have to give this a shot... there
doesn't seem to be a generic object which can hold arrays
of UDT's... oh well, can't all be easy options I
guess... ;)

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

>.
>

Graeme

unread,
Aug 4, 2002, 1:53:03 AM8/4/02
to
You're very close to the solution. You need to prep the Type data to a
variant array before passing to the function. The function works with
the data and returns the updated variant array (if required) - then
the variant arry needs to be rolled back into the UDT type array.

----

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.

0 new messages