vb.net - Why is my array out of range? -
option strict on alternative explicit on module module1 sub main() dim howmany integer dim namearray(howmany) string dim hourlywagearray(howmany) double dim hoursworkedarray(howmany) integer dim grosspayarray(howmany) double dim x integer = 0 console.writeline("how many employees?") howmany = cint(console.readline()) while x < howmany console.writeline("employee name: ") namearray(x) = cstr(console.readline()) console.writeline("hourly rate: ") hourlywagearray(x) = cdbl(console.readline()) console.writeline("hours worked: ") hoursworkedarray(x) = cint(console.readline()) if hoursworkedarray(x) <= 40 grosspayarray(x) = hourlywagearray(x) * hoursworkedarray(x) elseif hoursworkedarray(x) > 40 grosspayarray(x) = ((hoursworkedarray(x) - 40) * (hourlywagearray(x) * 1.5)) + (40 * hourlywagearray(x)) end if x = x + 1 loop console.writeline("{0,12:c} {1,12} {2,12:c} {3,12:c}", namearray, hourlywagearray, hoursworkedarray, grosspayarray) console.readline() end sub end module
at line 17, namearray(x) = cstr(console.readline()), receive error "an unhandled exception of type 'system.indexoutofrangeexception' occurred"
no matter value input howmany, error occurs when trying come in 2nd employee name.
you defining howmany after declare arrays. namearray defined on line three, when howmany 0, namearray have 0 entries. should read howmany console before defining namearray.
vb.net
No comments:
Post a Comment