5 Replies - 1232 Views - Last Post: 06 November 2013 - 04:23 AM Rate Topic: -----

#1 YeaH_IKnoW13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Error: "Index was outside the bounds of the array."

Posted 06 November 2013 - 03:52 AM

I created a random insult generator and I am getting this arror if I'm trying to write this code.
  ' this assigns the randomization to the adjectives and noun variables.
        nounInt = randomNum.Next(0, 7)
        adjInt1 = randomNum.Next(0, 11)
        adjInt2 = randomNum.Next(0, 11)
        adjInt3 = randomNum.Next(0, 11)
        ' By putting the msgbox at the start of the code, it doesn't show any lines of adjected and nouns.
        If txtInput.Text = "" Then
            MsgBox("Please enter the victim's name!")
            txtInput.Clear()
            txtOutput.Clear()
            txtInput.Text = ""
            cboAdjLength.SelectedIndex = 0
            Exit Sub ' Exit sub stops the script from displaying the adjectives and nouns after the msgbox is shown.
        End If

        ' -------------------------------------------------------------------------------------------------------'
        Dim aryNoun() As String = {"Donkey", "Rat", "Douchebag", "Retard", "Bum", "Dork"}
        Dim aryAdj1() As String = {"stinky", "lazy", "obnoxious", "spoiled", "vulgar", "careless", "lousy", "dull", "abhorrent", "bigheaded"}
        Dim aryAdj2() As String = {"stinky", "lazy", "obnoxious", "spoiled", "vulgar", "careless", "lousy", "dull", "abhorrent", "bigheaded"}
        Dim aryAdj3() As String = {"stinky", "lazy", "obnoxious", "spoiled", "vulgar", "careless", "lousy", "dull", "abhorrent", "bigheaded"}


        nounOutput = aryNoun(nounInt)
        adjOutput1 = aryAdj1(adjInt1) ' Keeps highlighing this line after error.
        adjOutput2 = aryAdj2(adjInt2)
        adjOutput3 = aryAdj3(adjInt3)


Is This A Good Question/Topic? 0
  • +

Replies To: Error: "Index was outside the bounds of the array."

#2 Peter O   User is offline

  • D.I.C Regular

Reputation: 128
  • View blog
  • Posts: 300
  • Joined: 19-October 13

Re: Error: "Index was outside the bounds of the array."

Posted 06 November 2013 - 03:59 AM

If randomNum.Next(0, 7) returns 6 you will be accessing aryNoun out of bounds because aryNoun only have 6 elements.

This post has been edited by Peter O: 06 November 2013 - 04:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: Error: "Index was outside the bounds of the array."

Posted 06 November 2013 - 04:01 AM

So what do you think the error might mean?

adjInt1 = randomNum.Next(0, 11)

This generates a number between 0 and 10 (inclusive) but your array has items indexed from 0..9.

This post has been edited by andrewsw: 06 November 2013 - 04:02 AM

Was This Post Helpful? 2
  • +
  • -

#4 YeaH_IKnoW13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Error: "Index was outside the bounds of the array."

Posted 06 November 2013 - 04:03 AM

View Postandrewsw, on 06 November 2013 - 04:01 AM, said:

So what do you think the error might mean?

adjInt1 = randomNum.Next(0, 11)

This generates a number between 0 and 10 (inclusive) but your array has items indexed from 0..9.

DUDE YOU ARE AWESOME I CHANGED THE Random numbers to this : for nouns: (0,5), and adjectives to (0,9) and it works! THANKS FOR PoIntinG it Out!!!!!
Was This Post Helpful? 0
  • +
  • -

#5 Peter O   User is offline

  • D.I.C Regular

Reputation: 128
  • View blog
  • Posts: 300
  • Joined: 19-October 13

Re: Error: "Index was outside the bounds of the array."

Posted 06 November 2013 - 04:18 AM

The maximum return value of randomNum.Next(0, 5) and randomNum.Next(0, 9) are 4 and 8, so if you use that you'll never get the last word in any of the arrays.
Was This Post Helpful? 1
  • +
  • -

#6 YeaH_IKnoW13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: Error: "Index was outside the bounds of the array."

Posted 06 November 2013 - 04:23 AM

View PostPeter O, on 06 November 2013 - 04:18 AM, said:

The maximum return value of randomNum.Next(0, 5) and randomNum.Next(0, 9) are 4 and 8, so if you use that you'll never get the last word in any of the arrays.

I just noticed it! Thanks again. I changed that to randomNum.next(0,6) and (0,10)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1