Resolved System.InvalidOperationException: 'The form referred to itself

zunebuggy65

Active member
Joined
Oct 12, 2023
Messages
42
Programming Experience
3-5
I have not come across this error. When compiling my code, I got this error:

VB.NET:
System.InvalidOperationException: 'The form referred to itself during construction from a default instance, which led to infinite recursion.  Within the Form's constructor refer to the form using 'Me.''

I have a Form named frm_Main and a Module named mod_Main. I have 6 Radiobuttons in a GroupBox that I have as Image type and I created 12 custom images that I have included in Resources

Here is the code that will not compile:

VB.NET:
Module mod_Main
    Sub Change_active(myCheck)
        Select Case myCheck
            Case 1
                frm_Main.rdo_Button1.Image = My.Resources.button_1_on
                frm_Main.rdo_Button2.Image = My.Resources.button_2_off
                frm_Main.rdo_Button3.Image = My.Resources.button_3_off
                frm_Main.rdo_Button4.Image = My.Resources.button_4_off
                frm_Main.rdo_Button5.Image = My.Resources.button_5_off
                frm_Main.rdo_Button6.Image = My.Resources.button_6_off
            Case 2
                frm_Main.rdo_Button1.Image = My.Resources.button_1_off
                frm_Main.rdo_Button2.Image = My.Resources.button_2_on
                frm_Main.rdo_Button3.Image = My.Resources.button_3_off
                frm_Main.rdo_Button4.Image = My.Resources.button_4_off
                frm_Main.rdo_Button5.Image = My.Resources.button_5_off
                frm_Main.rdo_Button6.Image = My.Resources.button_6_off
            Case 3
                frm_Main.rdo_Button1.Image = My.Resources.button_1_off
                frm_Main.rdo_Button2.Image = My.Resources.button_2_off
                frm_Main.rdo_Button3.Image = My.Resources.button_3_on
                frm_Main.rdo_Button4.Image = My.Resources.button_4_off
                frm_Main.rdo_Button5.Image = My.Resources.button_5_off
                frm_Main.rdo_Button6.Image = My.Resources.button_6_off
            Case 4
                frm_Main.rdo_Button1.Image = My.Resources.button_1_off
                frm_Main.rdo_Button2.Image = My.Resources.button_2_off
                frm_Main.rdo_Button3.Image = My.Resources.button_3_off
                frm_Main.rdo_Button4.Image = My.Resources.button_4_on
                frm_Main.rdo_Button5.Image = My.Resources.button_5_off
                frm_Main.rdo_Button6.Image = My.Resources.button_6_off
            Case 5
                frm_Main.rdo_Button1.Image = My.Resources.button_1_off
                frm_Main.rdo_Button2.Image = My.Resources.button_2_off
                frm_Main.rdo_Button3.Image = My.Resources.button_3_off
                frm_Main.rdo_Button4.Image = My.Resources.button_4_off
                frm_Main.rdo_Button5.Image = My.Resources.button_5_on
                frm_Main.rdo_Button6.Image = My.Resources.button_6_off
            Case 6
                frm_Main.rdo_Button1.Image = My.Resources.button_1_off
                frm_Main.rdo_Button2.Image = My.Resources.button_2_off
                frm_Main.rdo_Button3.Image = My.Resources.button_3_off
                frm_Main.rdo_Button4.Image = My.Resources.button_4_off
                frm_Main.rdo_Button5.Image = My.Resources.button_5_off
                frm_Main.rdo_Button6.Image = My.Resources.button_6_on
        End Select
    End Sub
End Module

Can anyone help me get past this?

Thank you.
 
The error message tells you exactly what the problem is and how to fix it. Inside frm_Main, it is referring to its own default instance using frm_Main when it should be referring to the current instance using Me. It's a pretty simple thing to look for the text "frm_Main" inside frm_Main.

I'm not sure why a module would be an issue. All I can think of is that you're calling that method, which refers to the default instance, from within frm_Main. It's a pretty terrible method anyway though. Why is it in a module in the first place, rather than in the form?

You could also reduce that code quite a bit. It's also very bad that you're extracting those resources multiple times, particularly if the code is executed a lot. Every time you get one of those properties, the data is extracted and a new Image object created and none of them are disposed. If you're going to keep creating new ones, at least dispose the old ones. Better still, don't create new ones at all. Just get each property once and assign the value to a variable, then use the variables each time.

Even better would be to actually make use of the fact that VB is an OO language. Create your own class that inherits Button and adds some properties: one for the On image, one for the Off image and one for the state. You can then simply toggle that state property and have it use the appropriate Image internally.
 
Back
Top