Index was outside the bounds of the String array

83 views Asked by At

I have this test code in VB:

Dim st As New ScheduledTasks("\\webserver")
Dim tasknames() As String = st.GetTaskNames
ListBox1.Items.Add(tasknames(1))

st.Dispose()

When i run it i get an error on line:

ListBox1.Items.Add(tasknames(1))

Index was outside the bounds of the array.

Does anyone have any suggestions what im doing wrong?

1

There are 1 answers

4
Patrick Hofman On BEST ANSWER

tasknames must contain at least 2 items for your code to work. (Note that arrays are 0-based, so you start counting at 0)

You have to check whether it really contains that amount:

If tasknames.Length >= 2
Then
    ListBox1.Items.Add(tasknames(1))
End If