We help IT Professionals succeed at work.

Objects array, IsArray and UBound functions

Taconvino
Taconvino asked
on
Medium Priority
1,245 Views
Last Modified: 2011-09-20
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]
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Leo EikelmanDirector, IT and Business Development

Commented:
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

Leo EikelmanDirector, IT and Business Development

Commented:
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

Author

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
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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...
Leo EikelmanDirector, IT and Business Development

Commented:
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
Leo EikelmanDirector, IT and Business Development

Commented:
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

Author

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

Author

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
Director, IT and Business Development
Commented:
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

Author

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
Leo EikelmanDirector, IT and Business Development

Commented:
I think I was getting more frustrated cause I knew the answer but I guess I wasn't clear.

Thank : )

Leo
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.