0

I made a constructor in my program but it kept giving me the stack overflow exception. I tried changing parameters but it did not help...

Public Sub New()
    InitializeComponent()

    For i As Integer = 0 To i = 12
        For j As Integer = 0 To i = 9
            atomcode(i, j) = (i * 10000 + j * 1000 + 99)
        Next j
    Next i

End Sub
3
  • Welcome to SO. Please read this on how to ask a question stackoverflow.com/questions/how-to-ask Show the call stack and the exception stack, how is atomcode() defined? We are not mind readers. Commented Jul 6, 2013 at 2:37
  • 1
    If I ask why the last parameter in the second "for" statement is an "i", am I just revealing that I don't understand the problem? Commented Jul 6, 2013 at 2:43
  • 1
    @OldProgrammer atomcode(i,j) is probably a two-dimensional array. VB uses parentheses instead of square brackets for array access. Commented Jul 6, 2013 at 2:54

1 Answer 1

2

I am not sure if you tried typing your code in, or pasted your code. The format of your For statement is wrong and in the second statement you are using j and i both which if it worked would be incrementing j until i is = 9 which would cause your stackoverflow. something like this simple console program example should work. Also the only way your above code will compile is if you have Option Strict Off, do yourself a favor and place Option Strict On at the top of your Class, it will prevent implicit narrowing conversions and save you a lot of grief.

Option Strict On
Module Module1
    Dim atomcode(,) As Integer

    Sub Main()
        ReDim atomcode(12, 9)
        For i As Integer = 0 To 12
            For j As Integer = 0 To 9
                atomcode(i, j) = (i * 10000 + j * 1000 + 99)
            Next j
        Next i

    End Sub 

End Module
9
  • tht din help it still keeps me giving the overflow error I have initialized atomcode as a two dimensional array. is there an issue about it being a constructor? Commented Jul 6, 2013 at 3:28
  • This code works in a Form constructor also., without any errors. You need to show more code, especially where you are creating your array. Usually StackOverflow errors are called by recursively calling a function. Nothing in the code you have shown is doing that. Commented Jul 6, 2013 at 3:34
  • these are all my declarations code Public Class Constructor Dim atomcode(12, 9) As Integer Dim obj As Integer Dim active As Boolean = False Dim obg As New Constructor Dim i, j, k As Integer Commented Jul 6, 2013 at 3:37
  • using option strict anywhere gives me a load of errors which I have never heard about.. Commented Jul 6, 2013 at 3:41
  • That is because you are letting VB do a lot of implicit narrowing conversions. Read the link on Option Strict that I put in the answer Commented Jul 6, 2013 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.