Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1150
  • Last Modified:

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]
0
Taconvino
Asked:
Taconvino
  • 6
  • 4
  • 2
2 Solutions
 
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 EikelmanCommented:
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 EikelmanCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 EikelmanCommented:
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 EikelmanCommented:
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 EikelmanCommented:
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
 
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 EikelmanCommented:
I think I was getting more frustrated cause I knew the answer but I guess I wasn't clear.

Thank : )

Leo
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now