dcsimg
Results 1 to 4 of 4
  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
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    322

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,654

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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


Click Here to Expand Forum to Full Width


×
By using this site, you agree to the Privacy Policy