Results 1 to 4 of 4

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Resolved [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

    Sorry bad english

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,301

    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
  •  



Click Here to Expand Forum to Full Width