UBound returning -1

This forum is meant for requesting technical support or reporting bugs.

Moderators: time-killer-games, Vengeance66, Candle, reneuend, GM-Support

UBound returning -1

Postby reneuend » Thu Jan 14, 2010 5:09 am

Have you ever had UBound return -1 ???

I can't figure out why I'm getting a -1. This is causing an "subscript out of range" error when I try to use the array.

(Update) After running some tests, I found that if I initialize a variant variable to an empty string: List = ""
Then if I use split when it is empty, I get a ubound of -1.
If I set List = " " (so there is a space in the string)
Then it works properly.

Please tell my why its like this! :?
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Lyberodoggy » Thu Jan 14, 2010 1:59 pm

It's probably caused because of an uninitialized variable. " " isn't equal to "", because the second one is the null object. Trying to split it won't return a properly instantiated Array.
You can prevent this by adding an if statement before the rest of your code:
Code: Select all
if ubound(arrayname)>-1 then
code
end if
User avatar
Lyberodoggy
Administrator
 
Posts: 2526
Joined: Sat Feb 17, 2007 3:31 pm
Location: Athens

Postby reneuend » Thu Jan 14, 2010 2:35 pm

isn't setting the List variable to an empty string initializing it? My gripe is that the split function doesn't handle an empty string, but it's fine with just a single space and no delimiter.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Lyberodoggy » Thu Jan 14, 2010 3:55 pm

To explain it better, the null object isn't the same as any other value.
From the return value section of msdn's definition for ubound in VB:
The highest value the subscript for the specified dimension can contain. If Array has only one element, UBound returns 0. If Array has no elements, for example if it is a zero-length string, UBound returns -1.


That's because, internally, strings are arrays which store one ascii character value in each subscript and the ending character '\0'
An empty string doesn't contain any values except for '\0', which cannot be read in a string, it's just a sentinel to show that the string is over. Therefore, splitting '\0' will result in an array of unistantiated strings
User avatar
Lyberodoggy
Administrator
 
Posts: 2526
Joined: Sat Feb 17, 2007 3:31 pm
Location: Athens

Postby reneuend » Thu Jan 14, 2010 6:20 pm

Good explanation, but the Split function could still have been written to handle empty strings! :)
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Lyberodoggy » Thu Jan 14, 2010 11:10 pm

Most of those functions exist by definition. That means they couldn't be changed and still be efficient. You can find them in languages like C++ and Java, doing the exact same things, like the -1 return value.
What you can do, of course, is create a new function that uses split() and handle null strings separately inside the function.

Example:

Code: Select all
Function SplitText(text, delimeter)
if text="" then
code to handle empty strings
SplitText=resultofpreviouscode
else
SplitText=split(text,delimeter)
end if


Although I don't know if AM's vbs functions can return Arrays. It could require ByRef use of "global" Arrays or a custom ocx function
User avatar
Lyberodoggy
Administrator
 
Posts: 2526
Joined: Sat Feb 17, 2007 3:31 pm
Location: Athens

Postby reneuend » Fri Jan 15, 2010 12:30 am

Thanks Lyberodoggy. Actually, I did build a new function with split() in it. I'm returning a delimited string. If I had more time, I'd test returning an array.

Anyway, thanks for the discussion. You bring back memories of my college days...over 25 years ago! :shock:
Once I starting working in IT, I lost a lot of how and why we do the things we do!
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Lyberodoggy » Fri Jan 15, 2010 2:07 pm

Hehe, yeah I know. I 've read all this 3 years ago and I keep forgetting things so I have to search them online every time...
User avatar
Lyberodoggy
Administrator
 
Posts: 2526
Joined: Sat Feb 17, 2007 3:31 pm
Location: Athens


Return to Adventure Maker Technical Support and Bug Reports

Who is online

Users browsing this forum: No registered users and 0 guests

cron