From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Länge eines Array (length of an array)

Hallo,

 

gibt es ähnlich wie bei Kanälen - chnlength - eine Möglichkeit die Länge von Arrays abzufragen?

 

Is there any possibility to get the length of an array like chnlength?

 

Gruß

0 Kudos
Message 1 of 5
(5,693 Views)

Traderhans,

You can use the UBound(array) function to get the largest available index of an array.

-Josh

 

0 Kudos
Message 2 of 5
(5,682 Views)

Hi Traderhans,

 

Note that UBound() returns the upper bound (index) of the array.  Since VBScript arrays are zero-indexed, the number of elements in the array is actually 1+UBound(arrayname).

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 3 of 5
(5,674 Views)

Arrays in vbs are a little tricky.

 

Thats mainly because

dim arr()

does not create an empty array but an unitialized one. So calling Ubound(arr) will raise an error.

It will also fail to loop using "for each".

 

So I would suggest to never use "()" to define an array but

dim arr : arr = Array()

MsgBox "length=" & ubound(arr) + 1

dim arrElem
for each arrElem in arr
  ' here we can do something
Next

Array() will create an initialized Array of length 0. UBound will give -1 as return value.

Usng this returing an array from a funtion works quiet well without having to check for exceptions if array is empty.

Option Explicit

Function GetElems()
  GetElems = Array()
  ' do some stuff
end function

dim elems : elems = GetElems
dim elem
for each elem in elems
  ' here we can do something
Next

If you want to dynamically grow an array you can use redim preserve.

Option Explicit

dim arr : arr = Array()

redim preserve arr(Ubound(arr) + 1)
arr(UBound(arr)) = "Appended text"

MsgBox "length=" & ubound(arr) + 1 & VBCRLF & arr(0)

 

Message 4 of 5
(5,662 Views)

Danke / thanks!

 

0 Kudos
Message 5 of 5
(5,657 Views)