Chris Lad
Chris Lad

Reputation: 11

System.IndexOutOfRangeException in vb.net when using arrays

Well, I have tried to complete a challenge that requires me to get all of the multiples of 5 or 3 from 0 to 1000 and then get the sum of them, I am new to vb.net so I thought that this would be a nice challenge for me to solve> I'm pretty sure I have the basics right, but I'm not quite sure why I'm getting this error :/.

Module Module1

    Sub Main()
        Dim Counter As Integer = 1
        Dim Numbers() As Integer
        Dim NumbersCounter As Integer = 0
        Dim Total As Integer = 0

        While (Counter <= 1000)

            If (Counter Mod 3 = 0) Then
                Numbers(NumbersCounter) = Counter '<--- The error is located on Numbers.
                NumbersCounter = NumbersCounter + 1
                Counter = Counter + 1

            ElseIf (Counter Mod 5 = 0) Then
                Numbers(NumbersCounter) = Counter
                NumbersCounter = NumbersCounter + 1
                Counter = Counter + 1

            Else
                Counter = Counter + 1
            End If

        End While

        Counter = 0

        While (Counter <= Numbers.Length)
            If (Counter = 0) Then
                Total = Numbers(Counter)
                Counter = Counter + 1
            Else
                Total = Total * Numbers(Counter)
                Counter = Counter + 1
            End If

        End While

        PrintLine(Total)

    End Sub

End Module

Any help or tips would be greatly appreciated! Thanks in advance.

Upvotes: 1

Views: 6943

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

In looking over your code egghead is right in stating that you did not initialize your array. But after doing so I had to change a few other things in your code to get it to run.

Module Module1

    Sub Main()
        Dim Counter As Integer = 1
        Dim Numbers(1000) As Integer          'Initialized the Array so it will be usable.
        Dim NumbersCounter As Integer = 0
        Dim Total As Integer = 0

        While (Counter <= 1000)

            If (Counter Mod 3 = 0) Then
                Numbers(NumbersCounter) = Counter 
                NumbersCounter = NumbersCounter + 1
                Counter = Counter + 1

            ElseIf (Counter Mod 5 = 0) Then
                Numbers(NumbersCounter) = Counter
                NumbersCounter = NumbersCounter + 1
                Counter = Counter + 1

            Else
                Counter = Counter + 1
            End If

        End While

        Counter = 0

        While (Counter <= Numbers.Length - 1)  ' Arrays are zero based so you need to subtract 1 from the length or else you will overflow the bounds
            If (Counter = 0) Then
                Total = Numbers(Counter)
                Counter = Counter + 1
            Else
                Total = Total + Numbers(Counter)  'You were multiplying here not adding creating a HUGE number
                Counter = Counter + 1
            End If

        End While

        Console.WriteLine(Total)  'Changed PrintLine which prints to a file to Console.WriteLine which writes to the screen
        Console.ReadLine          'Added a Console.ReadLine so the Window doesn't close until you hit a key so you can see your answer

    End Sub

End Module

Upvotes: 1

Poltergeist
Poltergeist

Reputation: 101

You need to allocate memory to Numbers array and since the size is known initially you may allocate while declaring:

Dim Numbers(1000) As Integer

Upvotes: 1

Related Questions