Unable to get size of globally defined array of a UDT.

silentquasar

New Member
Joined
May 21, 2010
Messages
3
Hi,

In one of the modules for a workbook of mine, I have The following type defined:
Code:
Public Type Mode_S_Message_Record
    Mode_S_ID As String
    DF As String
    Msg_Type As String
    NACv As String
    ADSB_ID As String
    OTGI As String
    NACp As String
    SIL As String
    Lat As String
    Lon As String
    Alt As String
    CPR As String
    Mode_S_Message As String
End Type
and then the following declaration of an array of this type:

Code:
Public Mode_S_Message_Records() As Mode_S_Message_Record
I have a subroutine in that module that populates the Mode_S_Message_Records array; then some other functions in the module can use the array.

What I'd also like to be able to do is have the UDFs check if the array is empty (hasn't been populated) and run the sub to populate the array if need be. The problem I run into is as follows: If I try to do the following in as sub or function:

Code:
If IsEmpty(Mode_S_Message_Records) Then
    Populate_Mode_S_Message_Records
End If
I get this error:
"Compiler error: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound function".
Can anyone tell me what I'm doing wrong? All I want to do is find out if my array is empty!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The IsEmpty function is a latebound function by its very nature because the only type that works with that function is a variant...

Here is an off the cuff alternative that may do well enough...

Code:
If IsEmpty2(Mode_S_Message_Records) Then
    Populate_Mode_S_Message_Records
End If

Function IsEmpty2(Arg() As Mode_S_Message_Record) As Boolean
    Dim a As Integer
    On Error Resume Next
    a = UBound(Arg)
    IsEmpty2 = (Err.Number = 9)
End Function
 
Last edited by a moderator:
Upvote 0
Huh. Thanks for the help. That definitely works for me! I have a few takeaways from this:

1 - I don't entirely understand what happened here, but I understand a little better about latebinding/earlybinding.
2 - I can (though it may be considered a bit of a kludge) use a particular expected error to work functionally to my own ends.

Why does UBound return an "index out of range" error, anyway? It sounds like UBound tries to iterate through the array or something to find the size, and then faults when it can't do any iteration at all. That seems odd to me. Oh well. Thanks again.
 
Upvote 0
Me? Create kludgy code? Never! :) The IsEmpty function is a latebound function because variants can contain more than one type. There is no way to compile the arguments before the code is executed because the compiler cannot know what the variant will hold. Does that make more sense?

Mr Kludge has left the building...
 
Upvote 0
Hey, if it works, it works!

Yes, that does help. Sometime later I'll chew on that a little more, when I'm not trying to get work done. :-)
 
Upvote 0

Forum statistics

Threads
1,223,710
Messages
6,174,019
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top