# Thread: [RESOLVED] If Index was outside the bounds of the array dont read

1. ## [RESOLVED] If Index was outside the bounds of the array dont read

Code:
```input = Console.ReadLine
Dim split As String() = input.Split("#")
pilihan = split(0)
word = split(1)
word2 = split(2)```
So word 2 only applies when there is a third index
Example input
1#array
2#array#rayndor

2. ## Re: If Index was outside the bounds of the array dont read

One option would be to use UBound.

Code:
```If UBound(split) >= 1 Then
word = split(1)
End IF
If UBound(split) >= 2 Then
word2 = split(2)
End If```

3. ## Re: If Index was outside the bounds of the array dont read

I kind of feel like saying "leave it as the array".

Since you have three variables, it shows you expect up to three values. Which of these looks easier?
Code:
```Dim tokens() As String = input.Split("#")
word1 = input(0)
word2 = input(1)
If tokens.Length = 3 Then
word3 = input(2)
End If

--------

Console.WriteLine("I got the words:")
Console.WriteLine(word1)
Console.WriteLine(word2)
If word2 IsNot Nothing Then
Console.WriteLine(word3)
End If```
Code:
```Dim tokens() As String = input.Split("#")

-------

Console.WriteLine("I got the words:")
For Each word As String In tokens
Console.WriteLine(word)
Next```
There are perhaps philosophical reasons to have individual variables, but knowing the larger problem can lead to a more elegant solution than array decomposition.

4. ## Re: If Index was outside the bounds of the array dont read

I have to agree with Sitten Spynne. If you have a variable number of values then having a fixed number of variables is just a bad idea. You should use a data structure that is intended to store a variable number of values, e.g. an array. Oh look, you already have an array. Problem solved.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured