Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
'Index was outside the bounds of the array.'

What I have tried:

Private Sub TextBox11_MouseHover(sender As Object, e As EventArgs) Handles TextBox11.MouseHover

        Dim inputNumber As Integer
        inputNumber = Val(TextBox11.Text)
        Dim years As Integer
        years = inputNumber \ 365

        Dim remainingDays As Integer
        remainingDays = inputNumber Mod 365

        Dim months As Integer
        months = remainingDays \ 30

        Dim days As Integer
        days = remainingDays Mod 30

        'تحديد عدد الأيام في الشهور المختلفة
        Dim daysInMonth(11) As Integer
        daysInMonth(0) = 31
        daysInMonth(1) = 28
        daysInMonth(2) = 31
        daysInMonth(3) = 30
        daysInMonth(4) = 31
        daysInMonth(5) = 30
        daysInMonth(6) = 31
        daysInMonth(7) = 31
        daysInMonth(8) = 30
        daysInMonth(9) = 31
        daysInMonth(10) = 30
        daysInMonth(11) = 31

        If months >= 2 Then
            daysInMonth(1) = 29
        End If

        
        While days >= daysInMonth(months - 1) '--problem here---('Index was outside the bounds of the array.') 
            days -= daysInMonth(months - 1)
            months += 1
            If months = 13 Then
                years += 1
                months = 1
            End If

        End While
  Label29.Text = years & " years, " & months & " months, " & days & " days"

End Sub
Posted
Updated 16-Feb-23 1:35am

How many months are there in a year? :laugh:
Look at your code:
VB
Dim daysInMonth(11) As Integer
daysInMonth(0) = 31
daysInMonth(1) = 28
daysInMonth(2) = 31
daysInMonth(3) = 30
daysInMonth(4) = 31
daysInMonth(5) = 30
daysInMonth(6) = 31
daysInMonth(7) = 31
daysInMonth(8) = 30
daysInMonth(9) = 31
daysInMonth(10) = 30
daysInMonth(11) = 31
YOu declare an array as holding 11 integers, then you try to fill indexes 0 to 11 inclusive - which needs twelve "slots" for integers.
Change the 11 to a 12 and the error will go away:
VB
Dim daysInMonth(12) As Integer
daysInMonth(0) = 31
daysInMonth(1) = 28
daysInMonth(2) = 31
daysInMonth(3) = 30
daysInMonth(4) = 31
daysInMonth(5) = 30
daysInMonth(6) = 31
daysInMonth(7) = 31
daysInMonth(8) = 30
daysInMonth(9) = 31
daysInMonth(10) = 30
daysInMonth(11) = 31
But it's a bad idea to use "magic numbers" anyway ...
 
Share this answer
 
Comments
Richard Deeming 16-Feb-23 6:31am    
"You declare an array as holding 11 integers..."
Not so. In VB.NET, the number in the array declaration is the upper bound, not the number of elements.

This is a holdover from VB6, when arrays were declared using a lower and upper bound - eg: daysInMonth(1 To 12) - with the lower-bound being optional.

IIRC, when VB.NET was being designed, there was a proposal to change the array declaration syntax to bring it in-line with other languages, since you can no longer directly specify a lower bound for an array. That was rejected on the grounds that it would break too much VB6 code, and cause confusion for developers working in both languages.
Quote:
VB.NET
While days >= daysInMonth(months - 1) '--problem here
You'll need to debug your code, but the position of the error suggests that the months variable is initially set to 0.

A quick check shows that any number between (365×N)+0 and (365×N)+29 will end up with months = 0.

NB: You cannot convert a number of days into days, months, and years without specifying a starting date. For example, 30 days from 1st January would be 30 days, whereas 30 days from 1st February would be 1 month and either 1 or 2 days, depending on the year.
 
Share this answer
 
As Richard mentioned, you are out of bounds because you are subtracting 1 from the months variable when accessing the daysInMonth array in the While loop. This can cause the index to become negative, or to exceed the bounds of the array.

For example, when months is equal to 1, the expression months - 1 will evaluate to 0, which is a valid index for the daysInMonth array. However, when months is equal to 0, the expression months - 1 will evaluate to -1, which is an invalid index for the array.

To fix this issue, you can adjust the condition in the While loop to check if days is greater than or equal to the number of days in the current month -

While days >= daysInMonth(months - 1)
    days -= daysInMonth(months - 1)
    months += 1
    If months = 13 Then
        years += 1
        months = 1
    End If
Wend
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900