Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Determine if array is empty

9,766 views
Skip to first unread message

Evertjan.

unread,
Feb 6, 2008, 4:58:29 PM2/6/08
to
=?Utf-8?B?WFA=?= wrote on 06 feb 2008 in
microsoft.public.scripting.vbscript:

> In a VBScript (.vbs) or an HTA file, using VBScript, can someone
> please post generic example code that tests to see if an array is
> empty?

if you mean empty = no members?

> Thanks much in advance for your assistance.

if ubound(myArray) = 0 then

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

XP

unread,
Feb 6, 2008, 5:11:03 PM2/6/08
to
Brilliant in its simplicity. Thanks!

mayayana

unread,
Feb 6, 2008, 8:39:46 PM2/6/08
to
> > In a VBScript (.vbs) or an HTA file, using VBScript, can someone
> > please post generic example code that tests to see if an array is
> > empty?
>
> if you mean empty = no members?
>
>
> if ubound(myArray) = 0 then

if ubound(a) = 0 then s = a(0)

a(0) could be something.

This topic comes up every once in a while
and I can never remember all the details. But
I find that if I declare:
Dim a()
IsEmpty ans IsNull both return False, even though
there actually are no members in that array!

This test seems to work:

On Error Resume Next
i = UBound(a)
MsgBox Err.number & vbCrLf & Err.Description

If Ubound is 0 there's no error, but for a()
it causes error 8, subscript out of range.

Personally I find all of these non-intuitive
array aspects confusing and often just use
an array where I start my data at a(1) and
may use a(0) to store something like the number
of members, or else just leave it unused.


Michael Harris

unread,
Feb 6, 2008, 8:41:47 PM2/6/08
to
Evertjan. wrote:
> =?Utf-8?B?WFA=?= wrote on 06 feb 2008 in
> microsoft.public.scripting.vbscript:
>
>> In a VBScript (.vbs) or an HTA file, using VBScript, can someone
>> please post generic example code that tests to see if an array is
>> empty?
>
> if you mean empty = no members?
>
>> Thanks much in advance for your assistance.
>
> if ubound(myArray) = 0 then

Arrays indices are 0-based so an array with UBound of 0 has 1 element...

Dim myArray

myArray = Array()
wscript.echo ubound(myArray)

An empty array will have a UBound of -1 meaning 0 elements

myArray = Array("hello")
wscript.echo ubound(myArray)

--
Michael Harris


Tom Lavedas

unread,
Feb 7, 2008, 8:57:36 AM2/7/08
to

The trickiest situation I have stumbled over is where an object is
supposed to return an array, but the returned set is actually empty,
as can occur with a WMI query. In such cases, trying to use the
Ubound function as a test raises an error. Rather, the first test
needs to be whether the result is empty - or even an array (I believe
I have seen a one item set returned from some objects as a scaler -
though I can't cite an example off the top of my head). So I tend to
use IsArray or vartype(arrVar) = vbArray for the test.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

mayayana

unread,
Feb 7, 2008, 10:29:50 AM2/7/08
to
>
> Dim myArray
>
> myArray = Array()
> wscript.echo ubound(myArray)
>
> An empty array will have a UBound of -1 meaning 0 elements
>

That's a curious quirk, but I wonder how much value
it has. Dim a() causes an error with UBound and I
can't think of any situation where one would find
it useful to do:

Dim a
a = Array()

But I supppose it could all be put into a test function:

Function IsUsableArray(a)
Dim i
Err.Clear


On Error Resume Next
i = UBound(a)

if (Err.Number = 0) and (i > -1) then IsUsableArray = True
End Function


Paul Randall

unread,
Feb 7, 2008, 11:59:27 AM2/7/08
to

"mayayana" <mayaXX...@mindXXspring.com> wrote in message
news:eWraf5Za...@TK2MSFTNGP02.phx.gbl...

'how much value'?
When you write a function that returns an array of things, I would
assume that you would still return an array even if there were only
one thing in that array. If there are no things to return, wouldn't
it be more logical for the function to return Array(0), rather than
some other type of variant?

-Paul Randall


mayayana

unread,
Feb 7, 2008, 7:02:08 PM2/7/08
to

> 'how much value'?
> When you write a function that returns an array of things, I would
> assume that you would still return an array even if there were only
> one thing in that array. If there are no things to return, wouldn't
> it be more logical for the function to return Array(0), rather than
> some other type of variant?
>

Yes, I would certainly think so. I usually return
an array where array(0) is the number of things
returned. Then I don't have to worry about errors
handling the array. But the question seemed to be
about how to handle an array variable, presumably
not under one's own control, that might actually
have no items.

When I said "not much value" I meant that it's
not much help to test a known array, that might
have no items, for ubound -1 because that only works
if the array was created with:

Dim a
a = Array()

and not with:

Dim a()

So that's why I suggested a function to provide
what MS somehow left out, despite all the "Is----"
functions they provided. That is, a function to test
"Is this a usable array?"


Al Dunbar

unread,
Feb 7, 2008, 10:59:40 PM2/7/08
to

"mayayana" <mayaXX...@mindXXspring.com> wrote in message
news:%237WVwXe...@TK2MSFTNGP06.phx.gbl...

Once we have solved this little problem, who wants to tackle this one: how
to determine if the array is half-empty or half-full?

/Al


mayayana

unread,
Feb 7, 2008, 11:47:21 PM2/7/08
to
> > So that's why I suggested a function to provide
> > what MS somehow left out, despite all the "Is----"
> > functions they provided. That is, a function to test
> > "Is this a usable array?"
>
> Once we have solved this little problem, who wants to tackle this one: how
> to determine if the array is half-empty or half-full?
>

Sorry, buddy, you're in the wrong group. You
want:
microsoft.public.scripting.philosophicalissues

Or if you have trouble with your arrays always
being half empty, and you want to bypass the
philosophical approach, going straight to
psycho-pharmaceuticals, you could try:

scripting.treatment.neurotransmitter-reuptake-inhibitors

Anthony Jones

unread,
Feb 8, 2008, 7:51:00 AM2/8/08
to
"XP" <X...@discussions.microsoft.com> wrote in message
news:05BDBB78-C68F-46CF...@microsoft.com...

> In a VBScript (.vbs) or an HTA file, using VBScript, can someone please
post
> generic example code that tests to see if an array is empty?
>
> Thanks much in advance for your assistance.
>

Arrays do cause quite a bit of confusion as this thread indicates. So I'll
de-mystify them by describing some of the under the hood workings. (Sorry
turned out to be more longwinded than I anticitpated).


VBScript is often described as having typeless variables or only having one
type of variable of the Variant type. A variant can hold any type and can
at any time be re-assigned with a type completely different from the one it
is currently holding.

However VBScript variables actually have two types, Variant and Array of
Variants.

Dim a ' A variant
Dim a() ' An array of variants

The structure of a Variant variable is 16 bytes in size containing in part a
VT field which defines the current type it is holding and the value itself
(if its a fixed size value that fits in 8 bytes, Long or Currency for
example) or a pointer to the value (an object, string or an array).

The structure of an array of variants variable is a pointer to a SAFEARRAY
structure. The SAFEARRAY defines the variant type of its elements, how many
bytes each element is, how many dimensions there are and a pointer to the
beginning of the array data itself. In VBScript the type is always
VT_VARIANT (12) and the element size is therefore always 16 bytes.

The SAFEARRAY structure is extensible in that immediately following it there
are a series (an 'array' you might say) of small structures, one per
dimension. These define the LBound value for the dimension and how many
elements are in the dimension (cElems).

This leads us to why we get "Subscript out of range" when using UBound on
undimensioned arrays.

UBound(a) is in fact short hand for UBound(a, 1) which says get me the
upper bound of the first dimension in the array. The 1 is an index (or
subscript) into that little array of dimension structures that follow the
SAFEARRAY. An undimensioned array has a dimension count of 0 and none of
these structures. Therefore fetching the first element from this 'array' of
dimension structures is 'out of range' for an undimensioned array.


Now take a look at this:-

Dim a
a = Split("", ",")
MsgBox UBound(a)

The array is empty and we get -1 from the UBound what is going on here?

Split is returning a dimensioned array. It has only 1 dimension but that
dimension has an element count of 0 since the input string was empty.
Remember the structure that defines the dimension carries only the LBound
and the element count. The UBound function therefore use this formula to
calculate the UBound :-

lBound + cElems - 1

When both lBound and cElems are 0 then UBound is -1.

This behaviour is actually quite handy, take a look at this:-

Sub ListWords(sLine)
Dim a : a = Split(sLine, " ")
Dim i
For i = 0 To UBound(a)
WScript.Echo a(i)
Next
End Sub

ListWords "Hello World"
ListWords ""

Had Split returned null, empty or an undimensioned array I would have to
take steps in the ListWords procedure to handle that correctly. However as
it is it works fine since For i = 0 to -1 will do nothing.


As Tom pointed out some interfaces can hold properties which if they have a
value will be an array but may also be null. This is another scenario that
may need to be considered when testing for an empty array.

I propose this function for general testing of an array:-


Function TestArray(rvnt)

On Error Resume Next

TestArray = 0
If (VarType(rvnt) And 8192) = 8192 Then
TestArray = UBound(rvnt) - LBound(rvnt) + 1
ElseIf Not (IsEmpty(rvnt) or IsNull(rvnt)) Then
TestArray = -1
End If

End Function


It assumes that a null or empty could potentially be an array but not yet
dimensioned. It returns -1 for if passed a string, long etc, 0 for an empty
array or the number of elements in the first dimension.


Other potentially useful functions might be:-


'Return the number of dimensions in the array, 0 for an non-array.
Function CountDims(rvnt)

On Error Resume Next

Dim i : i = 0
Do While Err.Number = 0
i = i + 1
UBound rvnt, i
Loop

CountDims = i - 1

End Function


and :-


'Return the total number of elements across all dimensions
Function CountElements(rvnt)

On Error Resume Next

CountElements = 1
Dim i : i = 0
Do While Err.Number = 0
i = i + 1
CountElements = CountElements * (UBound(rvnt, i) - LBound(rvnt, i) + 1)
Loop

If i = 1 Then CountElements = 0

End Function

Hope you find this helpful,

--
Anthony Jones - MVP ASP/ASP.NET


Al Dunbar

unread,
Feb 9, 2008, 12:31:31 PM2/9/08
to

"Anthony Jones" <A...@yadayadayada.com> wrote in message
news:OtayCFla...@TK2MSFTNGP02.phx.gbl...

> "XP" <X...@discussions.microsoft.com> wrote in message
> news:05BDBB78-C68F-46CF...@microsoft.com...
>> In a VBScript (.vbs) or an HTA file, using VBScript, can someone please
> post
>> generic example code that tests to see if an array is empty?
>>
>> Thanks much in advance for your assistance.
>>
>
> Arrays do cause quite a bit of confusion as this thread indicates. So
> I'll
> de-mystify them by describing some of the under the hood workings. (Sorry
> turned out to be more longwinded than I anticitpated).

<snip interesting dissertation on arrays>

> Hope you find this helpful,

Definitely interesting, however, perhaps not something one would use in
every day scripting. When a script creates and populates an array, the
knowledge required by the rest of the script to process that array is
generally implicit. The problem seems to arise when one's script receives a
variant result from a COM object or other "black box" source of data.

What your post does indicate very well, though, is that simple questions do
not necessarily always lead to simple answers, and also that we need to be
careful when mixing the "english" meaning of certain words such as "empty"
with what they might mean in the context of a scripting environment. That
was basically my intent for posting my little joke about trying to determine
if an array were half-empty or half full.

We have been discussing the complexities of determining whether or not an
array is "empty", perhaps without specifying which sense of the word we mean
(hence we may be thinking of different meanings). But surely the range of
meanings of various states of "non-emptiness" is even an even more thorny
one.

"full" is normally thought to mean that there is no more room for any
additional content. As applied to a vbscript array, this is practically
meaningless, as each element in an array could be assigned a higher value.
And once every element contained this value, the number of elements could be
increased with REDIM.

Yes, this is silly. But I think it illustrates that sometimes our problems
are mostly semantical.

/Al


ara...@gmail.com

unread,
Apr 5, 2013, 2:55:42 AM4/5/13
to
Hello,

I finally decided to create my own function to check if the array is empty.
Let me know if you find this useful:

Function IsArrayEmpty(checkedArray)
IsArrayEmpty = true
For Each item In checkedArray
IsArrayEmpty = false
If IsArrayEmpty = false Then Exit For
Next
End Function

R.Wieser

unread,
Apr 5, 2013, 6:03:34 AM4/5/13
to
Hello arancy(?),

> I finally decided to create my own function to check if the array is
empty.

My apologies, but I have a few remarks for you:

1) The "If IsArrayEmpty = false Then" check in the loop can be removed, as
can't be anything else than False in it, due to you having set it just above
that line.

2) The code could be written a bit different to make use of the fact that a
funtions result-variable (in this case IsArrayEmpty) is always initialied to
Zero (equals False) when the function is entered:

Function IsArrayEmpty(checkedArray)
For Each item In checkedArray
exit function
Next
IsArrayEmpty = true
End Function

3) Would checking the size of the array not be enough ?

IsArrayEmpty = UBound(checkedArray)=0

Ofcourse, this is assuming you are actually checking an array (not a simple
variable).

Regards,
Rudy Wieser


-- Origional message:
<ara...@gmail.com> schreef in berichtnieuws
41884665-9f87-48de...@googlegroups.com...

Evertjan.

unread,
Apr 5, 2013, 6:04:23 AM4/5/13
to
A bit silly to respond to a posting of 2008 without any quoting,
wouldn't you think?

This is Usenet, not some Google group.

================

Anyway, this is also overdone:

> IsArrayEmpty = false
> If IsArrayEmpty = false Then Exit For

since the second IsArrayEmpty = false is always true,
as you just set it so,
that is the same as:

IsArrayEmpty = false
Exit For

============

even better:

Function IsArrayEmpty(checkedArray)
IsArrayEmpty = true
For Each item In checkedArray
IsArrayEmpty = false
Exit function
Next
End Function

=============

perhaps you would like to be prepared for a non-existing,
read not dimmed as such, array:

Function IsArrayEmptyOrNonexisting(TheArray)
IsArrayEmptyOrNonexisting = true
If Not IsArray(TheArray) Then Exit Function
For Each item In TheArray
IsArrayEmptyOrNonexisting = false
Exit function '' this only makes the function faster
Next
End Function

[Not tested]

Dave "Crash" Dummy

unread,
Apr 5, 2013, 9:30:31 AM4/5/13
to
I am confused about how you define an empty array. Do you mean an
undeclared array with zero items or a finite array filled with zero
length items?
They are not the same thing. In any event, this will test either case
with a
single line of code:

len(join(checkedArray,""))
--
Crash

One man's weed is another man's wildflower.

Mayayana

unread,
Apr 5, 2013, 10:14:25 AM4/5/13
to
I'm guessing the issue is checking whether something
is actually an array before handling it.

| len(join(checkedArray,""))

That's an interesting one, but it doesn't always work.

Dim A1(), A2

MsgBox UBound(A1) ' causes error.

MsgBox len(Join(A1, "")) 'works by returning 0, but UBound
' could be anything, or nothing, so it
' doesn't test whether A1 can be accessed
' as an array.

MsgBox len(Join(A2, "")) ' causes error. When one needs to
' check whether an array was returned
' this is of no value.

I try to plan for any array variable to always have
members. For instance, if a function returns an array
then Return(0) might contain ubound.

If one doesn't do that then I think the only way
to check it is with UBound and error trapping.




Evertjan.

unread,
Apr 5, 2013, 10:33:30 AM4/5/13
to
Dave "Crash" Dummy wrote on 05 apr 2013 in
microsoft.public.scripting.vbscript:

> I am confused about how you define an empty array. Do you mean an
> undeclared array with zero items or a finite array filled with zero
> length items?

btw, array items can hold other types than strings, only strings can be
zero length. Oh, no, they can also hold zero lenth arrays.

I would think an empty array is array without items.

An array with empty strings is not an empty array,
but just an array with empty strings. ;-)

> len(join(checkedArray,""))

Nice, and very usable to check for an array with just strings.

Dave "Crash" Dummy

unread,
Apr 5, 2013, 10:58:51 AM4/5/13
to
Evertjan. wrote:
> Dave "Crash" Dummy wrote on 05 apr 2013 in
> microsoft.public.scripting.vbscript:
>
>> I am confused about how you define an empty array. Do you mean an
>> undeclared array with zero items or a finite array filled with zero
>> length items?
>
> btw, array items can hold other types than strings, only strings can be
> zero length. Oh, no, they can also hold zero lenth arrays.

> I would think an empty array is array without items.
>
> An array with empty strings is not an empty array,
> but just an array with empty strings. ;-)

As I said, what constituted an empty array was not clear.

>
>> len(join(checkedArray,""))
>
> Nice, and very usable to check for an array with just strings.

Arrays are stored as strings, regardless of how they are interpreted.

dim arr(3)
arr(0)=10
arr(1)=21
arr(2)=true
arr(3)=false

msgbox len(join(arr,""))

will yield a length of 13 (2+2+4+5).

Evertjan.

unread,
Apr 5, 2013, 11:34:24 AM4/5/13
to
Dave "Crash" Dummy wrote on 05 apr 2013 in
microsoft.public.scripting.vbscript:

> Evertjan. wrote:
>> Dave "Crash" Dummy wrote on 05 apr 2013 in
>> microsoft.public.scripting.vbscript:
>>
>>> I am confused about how you define an empty array. Do you mean an
>>> undeclared array with zero items or a finite array filled with zero
>>> length items?
>>
>> btw, array items can hold other types than strings, only strings can be
>> zero length. Oh, no, they can also hold zero lenth arrays.
>
>> I would think an empty array is array without items.
>>
>> An array with empty strings is not an empty array,
>> but just an array with empty strings. ;-)
>
> As I said, what constituted an empty array was not clear.

My explanation does not make that realy clear,
as it is just my humble opinion and I am not the OP.

>>> len(join(checkedArray,""))
>>
>> Nice, and very usable to check for an array with just strings.
>
> Arrays are stored as strings, regardless of how they are interpreted.

Array elements perhaps. ;-)

Is this not the effect of len() or join()?

> dim arr(3)
> arr(0)=10
> arr(1)=21
> arr(2)=true
> arr(3)=false
>
> msgbox len(join(arr,""))
>
> will yield a length of 13 (2+2+4+5).

Testing in IE10 overhere returns: 14

Why?

msgbox len(false)

returns: 6

Why?

msgbox false

returns: onwaar

being the nonsensical regionalisation in Dutch,
no one uses in programming. [Du: onwaar = Eng. untrue]

MS does the same nonsense in Excel, btw.

Arrrrch, I prefer the logic of Javascript in Chrome.

PS:

msgbox onwaar

retuns an emty box. Why?

R.Wieser

unread,
Apr 5, 2013, 11:35:51 AM4/5/13
to
Dave,

> Arrays are stored as strings, regardless of how they are interpreted.

I would suggest you re-check that if I where you.

Its actually the other way around: array-elements are "interpreted" in
direct regard to how they are used.

Although that "join" command may *silently, behind-the-scenes* convert
everything to strings (how would a join be able to work otherwise ?), the
contents of those array-elements are stored depending on their types.

Regards,
Rudy Wieser


-- Origional message:
Dave "Crash" Dummy <inv...@invalid.invalid> schreef in berichtnieuws
kjmomn$m9j$1...@dont-email.me...
>
[Snip previous message]

Mayayana

unread,
Apr 5, 2013, 2:32:57 PM4/5/13
to
| Arrays are stored as strings, regardless of how they are interpreted.
|
| dim arr(3)
| arr(0)=10
| arr(1)=21
| arr(2)=true
| arr(3)=false
|
| msgbox len(join(arr,""))
|
| will yield a length of 13 (2+2+4+5).
|

In VBS everything is a variant -- a 16-byte
datatype that stores the data along with a
"subtype" designation. Even an array is a variant.

MsgBox VarType(arr(1))
MsgBox VarType(arr(3))

That code returns 2 and 11. (integer and boolean)
Variants allow ignoring datatypes. WSH will coerce
them as needed, whenever possible. But they still
have a subtype. When you call Join you force WSH
to coerce all variables into string data. When your
array started you had two variants that held 2-byte
integer data and 2 variants that held boolean values.
Those are both numeric data held in the variant
variable. If they had been strings then all of the array
members would have actually held 4-byte pointers to
the string data.


ekkehard.horner

unread,
Apr 6, 2013, 4:04:22 AM4/6/13
to
R.Wieser schrieb:
...
> 3) Would checking the size of the array not be enough ?
>
> IsArrayEmpty = UBound(checkedArray)=0

The UBound of an empty array is -1:

>> WScript.Echo UBound(Array())
>>
-1


R.Wieser

unread,
Apr 6, 2013, 6:39:37 AM4/6/13
to
Hello ekkehard.horner,

> The UBound of an empty array is -1:

Whoops ! You're right. :-\ :-)

Regards,
Rudy Wieser


-- Origional message:
ekkehard.horner <ekkehar...@arcor.de> schreef in berichtnieuws
515fd6e5$0$6633$9b4e...@newsspool2.arcor-online.net...

Mayayana

unread,
Apr 6, 2013, 9:34:20 AM4/6/13
to
| > IsArrayEmpty = UBound(checkedArray)=0
|
| The UBound of an empty array is -1:
|
| >> WScript.Echo UBound(Array())

That's a trivia fact that's not of much value.

Dim A()
MsgBox UBound(A)

The return is an error, not -1. Your example
probably works only because you've used the
Array function, which explicitly creates an
array. But your sample code is not real-world
code. Creating an empty array with the Array
function would be pointless.

The two following examples will return False and
True, indicating that A is not empty and is an
array:

Dim A
A = Array()
MsgBox IsEmpty(A) & vbCrLf & IsArray(A)

Dim A()
MsgBox IsEmpty(A) & vbCrLf & IsArray(A)

But what one really needs to know is whether
the array has a UBound. One needs to either
error-trap that check or design the code such
that all arrays are initialized to at least have
Ubound 0 in all scenarios.


ekkehard.horner

unread,
Apr 6, 2013, 3:35:01 PM4/6/13
to
Mayayana schrieb:
> | > IsArrayEmpty = UBound(checkedArray)=0
> |
> | The UBound of an empty array is -1:
> |
> | >> WScript.Echo UBound(Array())
>
> That's a trivia fact that's not of much value.
>
--------
(1) That "The UBound of an empty array is -1" is not trivial can be seen
from your own posting: "all arrays are initialized to at least have
Ubound 0". So even seasoned VBScripters have to be told from time
to time that the UBound of an empty array is -1.

>> ReDim A(0)
>> A(0) = "UBound of empty array is -1"
>> WScript.Echo UBound(A), A(0)
>>
0 UBound of empty array is -1
--------
> Dim A()
> MsgBox UBound(A)
>
> The return is an error, not -1. Your example
> probably works only because you've used the
> Array function, which explicitly creates an
> array. But your sample code is not real-world
> code. Creating an empty array with the Array
> function would be pointless.
>
--------
(2) Dim A() is bad VBScript and shouldn't be used in real-world code
at all. There are two kinds of arrays in VBScript:

(a) fixed size arrays that can't be redimmed/resized
You use such an array when you know the size at 'compile time'
by writing something like

Dim A(4711)

to get an array with 4712 (empty) slots. The - nonsensical -
meaning of

Dim A()

would be: "please create a fixed array of no size" or "give
me a container that can't hold anything and can't be grown
later".
No wonder VBScript can't handle such an abomination and won't
give
you an UBound for it. It surely is deplorable that you don't get
a syntax error message for that silly declaration and IsArray(),
VarType(), and TypeName() accept such variables, but a
programmer should have and use a brain of his own - not
everything
you can slip by the 'compiler' is decent code.

(b) dynamic arrays that can grow/shrink
You use such an array when the desired size is determined at
and/or changes at run time by writing

nUB = computation/user input/...
ReDim A(nUB)

or

Dim A : A = Array()
For [Each] ..
ReDim Preserve A(UBound(A)+1)
A(UBound(A)) = ...
Next

The second example shows that "A = Array()" - initializing
a variable with/to an empty dynamic array - is far from
pointless.
--------
> The two following examples will return False and
> True, indicating that A is not empty and is an
> array:
>
> Dim A
> A = Array()
> MsgBox IsEmpty(A) & vbCrLf & IsArray(A)
>
> Dim A()
> MsgBox IsEmpty(A) & vbCrLf & IsArray(A)
>
--------
(3) So what? IsEmpty() "Returns a Boolean value indicating whether
a variable has been initialized." That has nothing to do with the
problem whether an array is empty == contains no elements (not
even empty == un-initialized ones).
--------
> But what one really needs to know is whether
> the array has a UBound. One needs to either
> error-trap that check or design the code such
> that all arrays are initialized to at least have
> Ubound 0 in all scenarios.
--------
(4) The UBound of an empty array is -1!
--------

Mayayana

unread,
Apr 6, 2013, 4:50:23 PM4/6/13
to
| (2) Dim A() is bad VBScript and shouldn't be used in real-world code
| at all. There are two kinds of arrays in VBScript:
|
| (a) fixed size arrays that can't be redimmed/resized
| You use such an array when you know the size at 'compile time'
| by writing something like
|
| Dim A(4711)
|
| to get an array with 4712 (empty) slots. The - nonsensical -
| meaning of
|
| Dim A()
|
| would be: "please create a fixed array of no size" or "give
| me a container that can't hold anything and can't be grown
| later".

No. Try it. VarType A() is a variant array.
You *can* use:

Dim A
Redim A(1)

That works because VBS will coerce variable types
whenever possible. Redim tells it that you want A to
be an array. This way is more explicit:

Dim A()
Redim A(1)

A starts as a variable-size array rather than starting
as a variable with no subtype. It *can* be "grown"
later. Why would you think that it can't?

That syntax is the standard in VB for a variable
size array that can be redimmed. The former method
can only accomodate variant arrays. There's no difference
in VBS since all variables are arrays, but Dim A() is more
clear. It avoids unnecessary coercion and more explicitly
shows what the code is doing.



Mayayana

unread,
Apr 6, 2013, 9:09:57 PM4/6/13
to
| That syntax is the standard in VB for a variable
| size array that can be redimmed. The former method
| can only accomodate variant arrays. There's no difference
| in VBS since all variables are arrays, but Dim A() is more
| clear. It avoids unnecessary coercion and more explicitly
| shows what the code is doing.
|

Woops. I meant to say that all variables are variants
in VBS. And all array members are variants. So an array
can be declared as Dim A or as Dim A(). The latter explicity
creates an array variable. If you test VarType you'll see
that Dim A() creates a variable-size array variable with
no members. It should probably return ubound of -1, but
it doesn't.

Maybe the moral of this story is that one needs a method
that works for one's own style of coding. If you always
use the extra step of

A = Array()

when creating an array then you can safely check UBound
to see whether the array has any members. If you use that
method then you will also need to be sure you do the same
when a function returns an array:

Function GetSomething()
'-- you can never do something like this:
If x <> y then Exit Function
'-- you will have to always make sure that you
'-- at least do this before exiting the function:
GetSomething = Array()
End Function

Personally I like to just make sure that I always
redim an array to at least ubound 0 to avoid possible
errors. Then I don't have to worry about calling
UBound. Either way can work, but it has to be
consistent.


ekkehard.horner

unread,
Apr 7, 2013, 7:50:40 AM4/7/13
to
Mayayana schrieb:
....
> Personally I like to just make sure that I always
> redim an array to at least ubound 0 to avoid possible
> errors. Then I don't have to worry about calling
> UBound. Either way can work, but it has to be
> consistent.

As the usenet makes it easy for interested people to read the full text
of contributions, I don't feel any need to comment on your remarks.

What I object to, however, is your insistence of using "UBound 0" in the
context of minimal/empty arrays. An array having a UBound of 0
has one (possibly Empty element), whereas "The UBound of an empty array
is -1".

Mayayana

unread,
Apr 7, 2013, 8:27:21 AM4/7/13
to


--
--
"ekkehard.horner" <ekkehar...@arcor.de> wrote in message
news:51615d5e$0$9508$9b4e...@newsspool1.arcor-online.net...
You "object" to people doing things a different way?
How German of you. :)

You don't seem to have understood what I said. In a
nutshell, all I'm saying is that people can operate in
different ways, but that you need to be aware of the
"gotchas" in your chosen method.

If you want to assume that an empty array
has ubound -1 that will work *only* if you consistently
use your unusual method of Dim A: A = Array(). If you
don't explicitly use the Array function then a UBound
call to an empty array will not return -1. It will cause an
error. You may like to think of an empty array as having
ubound -1 but that behavior is simply not consistent. It's
not my fault. You'll have to take it up with Microsoft.

As I said clearly above, I don't refer to an empty
array as having ubound 0. Rather, I make sure to
redim arrays to at least ubound 0 so that I'm never
referencing an empty array:

Dim A()
Redim A(0)

For instance, in a function return:

Function GetIt()
Dim A()
If NothingToDo then
Redim A(0)
GetIt = A
else
Redim A(numberNeededHere)
' fill the array.
GetIt = A
end if
End Function

I don't say that's the best way. It's just my way.
And it works. I can check how many items are
returned by checking ubound, without worrying
about ubound errors:

B = GetIt
If Ubound(b) > 0 then
' process data here, with no worry that ubound call might cause an error.
End If

If you look in the WSH help under "Dim statement" you'll
see that the method Dim A() is neither unusual nor unsuitable.
It's one of 3 examples given for Dim syntax:

Dim Names() ' Declare a dynamic array.


caleb...@gmail.com

unread,
May 1, 2014, 11:43:13 AM5/1/14
to
Exactly what I was looking for! Thanks

Evertjan.

unread,
May 1, 2014, 1:12:39 PM5/1/14
to
caleb...@gmail.com wrote on 01 mei 2014 in
microsoft.public.scripting.vbscript:

> Exactly what I was looking for! Thanks

Perhaps, however, if you do not quote,
how are we to know what you are talking about?

This is Usenet, not email.

R.Wieser

unread,
May 2, 2014, 5:14:43 AM5/2/14
to
Evertjan,

> however, if you do not quote, how are
> we to know what you are talking about?

There is a "In-Reply-To" in the headers of his message:
Xns9A3CE9B7...@194.109.133.242

Alas, I would not know how to locate the message it points/should point to.
:-\ Do you have any idea - if its possible that is (would be helpfull for
cases like the current one) ?

Regards,
Rudy Wieser


-- Origional message:
Evertjan. <exxjxw.h...@inter.nl.net> schreef in berichtnieuws
XnsA320C36C...@194.109.133.133...

Evertjan.

unread,
May 2, 2014, 5:48:16 AM5/2/14
to
"R.Wieser" <add...@not.available> wrote on 02 mei 2014 in
microsoft.public.scripting.vbscript:

>> however, if you do not quote, how are
>> we to know what you are talking about?
>
> There is a "In-Reply-To" in the headers of his message:
> Xns9A3CE9B7...@194.109.133.242
>
> Alas, I would not know how to locate the message it points/should point to.
> :-\ Do you have any idea - if its possible that is (would be helpfull for
> cases like the current one) ?


Dear Rudy,

On usenet you are responding to the group, [not to a person, thats, where
email comes in.] Given the nature of usenet servers do not even give postings
in the same order, each server has it's own settings and retention time.

I [and we] have no idea what you are responding on, so ALWAYS quote the date
of the posting, name of the poster and relevant part of the text content.

I only can see you are responding on a posting of mine, but that posting
could even be 15 years old.

R.Wieser

unread,
May 2, 2014, 8:09:05 AM5/2/14
to
Hey Evertjan,

> each server has it's own settings and retention time.

Yes, I'm aware of that. But I have never encountered it as a problem
though, as the "I have no clue how to find the origional message" one comes
way before it. :-)

So, do you know how to (attempt to) find the origional message ? I really
would like to know.

By the way, I've tried to prefix such an ID with "news://" , but did not
get far that way (I did not want to add a new newsgroup - which is next to
the question if I would be allowed to actually access it)

> I [and we] have no idea what you are responding on, so
> ALWAYS quote the date of the posting, name of the poster
> and relevant part of the text content.

If you look at my previous and current response I think you can see I adhere
to that method of posting too. The problem is someone didn't, and that is
the (kind of) message I'm trying to make sense of.

> I only can see you are responding on a posting of mine,
> but that posting could even be 15 years old.

I do seem to remember that particular subject as being from quite recently
(interresting discussion, arrays that do not contain any elements v.s.
arrays that contain undefined or empty elements), which is why I thought to
try.

.. Even though the newsgroupserver I'm on (which is text only and has a
retention time of ~60 days) does not have that thread anymore.

Regards,
Rudy Wieser


-- Origional message.
Evertjan. <exxjxw.h...@inter.nl.net> schreef in berichtnieuws
XnsA3217814...@194.109.133.133...

Evertjan.

unread,
May 2, 2014, 8:19:06 AM5/2/14
to
"R.Wieser" <add...@not.available> wrote on 02 mei 2014 in
microsoft.public.scripting.vbscript:

>> each server has it's own settings and retention time.
>
> Yes, I'm aware of that. But I have never encountered it as a problem
> though, as the "I have no clue how to find the origional message" one comes
> way before it. :-)
>
> So, do you know how to (attempt to) find the origional message ? I really
> would like to know.

I am not that much concerned, as it is the duty, well, only at their peril,
of the sender to make sense of their postings, not of the group.

> I do seem to remember that particular subject as being from quite recently
> (interresting discussion, arrays that do not contain any elements v.s.
> arrays that contain undefined or empty elements), which is why I thought to
> try.

That was about javascript arrays, methinks, in the proper NG.

This NG is about VBScript.

R.Wieser

unread,
May 2, 2014, 10:03:45 AM5/2/14
to
Hello Evertjan,

> I am not that much concerned, as it is the duty, well, only at their
peril,
> of the sender to make sense of their postings, not of the group.

I understand your sentiments here and do agree to them. But look at it like
this: Nooone is allowed to be violent to anyone else (at least, thats what
the law says), so there is no need to learn any self-defense ?

The point is that this issue pops up once in a while, and I would like to be
able to satisfy my own curiosity (in what its is the OP was responding to).

But I understand that you do not know either. Alas.

Regards,
Rudy Wieser


-- Origional message:
Evertjan. <exxjxw.h...@inter.nl.net> schreef in berichtnieuws
XnsA32191A7...@194.109.133.133...
0 new messages