Objects array, IsArray and UBound functions

Hello,

A little observation:

Private Sub Form_Load()
Dim xxx() As Object

MsgBox UBound(xxx)
End Sub

This code above generates a Runtime error 9 (Subscript out of range).  

But this other code:

Private Sub Form_Load()
Dim xxx() As Object

IsArray xxx
MsgBox UBound(xxx)

End Sub

Generates a message box with a value -1 (without the error 9).  

So I went to MSDN (http://msdn2.microsoft.com/en-us/library/88yx3cbt(VS.80).aspx) and checked the definition of IsArray function.  It says that the parameter is passed ByVal, but given the examples above, I'm not really sure.  (if you are wondering, yes, it was me the one who rated the article with a 7 :-)).

Is this a known bug, a definition problem, or what?  I'm asking this, because if it is a bug and gets fixed, some of my code will stop working... I'm using this "issue" to check for UBound value without errors... links to documentation about this "problem" will be appreciated.

Thanks!

+TCV

[EDIT]
Tested on VB 6 SP6)
[/EDIT]
LVL 6
TaconvinoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
The link you posted is for VB.NET, while the code you posted is for vb6 as far as I see...

here is the good link (assuming vb6):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctIsArray.asp

as from there, the argument is taken byref.
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
why are u using

Dim xxx() As Object

instead of

Dim xxx() As Array  ??

if u have an array like this

MusicGenres = Array("Blues", "Classic Rock", "Country",
"Dance", "Disco", "Funk", "Grunge", "Hip -Hop",
"Jazz", "Metal", "New Age", "Oldies", "Other")

UBound(MusicGenres) will work

0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Also, the problem is that it isn't initiallized when you are calling it

try this code

Private Sub Form_Load()
   
Dim xxx(1 To 10) As Object

MsgBox UBound(xxx)


End Sub


It will work because it is initialized

Cheers,

Leo
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TaconvinoAuthor Commented:
angelIII, that makes a lot of sense.  So, if I migrate my application to .NET it will stop working, right? (with an ugly error 9, I presume)

leikelman, I'm declaring the array as Object, because it stores an object... it's not a string array.  For your second post, the array needs to be dynamic, as it gets redimensioned according to some variable parameters.

+TCV
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
note that when adding a debug view on the xxx variable, then the code immediately works...
so there is indeed some implicit initializing happening...

note that migrating to vb.net will change your code anyhow more than this...
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Taconvino,

your problem simply lies on not initializing the object you are trying to use the UBound function on.

Initialize the object before using it.

Leo
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
And.... even if it does get redimensioned, somewhere it needs to be dimensioned in the first place before u use the UBound function.

Cheers,

Leo
0
TaconvinoAuthor Commented:
leikelman, you're missing the point.  The point is that, if I run the array through the IsArray function, somehow it gets changed, when it actually shouldn't, as I was assuming that the IsArray function is taking the parameter by value (which got clarified by angelIII above).  Besides, the code snippet I posted actually demonstrates that initialization is not required after you run an uninitialized dynamic array throug the IsArray function, which is what I'm doing.

Anyway, thanks for your interest.

+TCV
0
TaconvinoAuthor Commented:
angelIII, you are right about the debug window.  I think that IsArray function does this "implicit" initialization, while UBoun function does not.  I would be great to have some of this stuff documented, as I couldn't find any reference to this behavior.

+TCV
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
I don't get it.  Your original question asked if this is a bug.

UBound doesn't except a non initialized array.  This is not a bug.

IsArray doesn't implicitly initialize an array, it does initialize an array.  There is a wide collectino of apps that initiallize arrays through IsArray.  

This is used in many apps that use the UBound function.

Check this link for a reference

http://www.utteraccess.com/forums/showflat.php?Cat=&Number=384026&Main=384026


So the answer to your question.  This is not a bug.


Leo
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TaconvinoAuthor Commented:
Please, there's no need to get mad... and you have to admit that your last post looks a lot more like an answer than your previous ones.  

Again, thanks for your interest (both yours and angelIII's)

+TCV
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
I think I was getting more frustrated cause I knew the answer but I guess I wasn't clear.

Thank : )

Leo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.