type initializer for ... threw an exception?

QuickBooksDev

Member
Joined
Mar 2, 2008
Messages
6
Programming Experience
10+
We have just converted a large VB6 desktop app to VB.NET 2008. First via the VB 2005 (opened the project), then days of fixing errors, etc. Then opened the VB 2005 in VB 2008 so now it is a 2008 solution.
There are about 30 forms with code behind and about 40 other modules.

Message InvalidOperationException was unhandled.

... The type initializer for .... threw an error.

on
AppTest = true

App framework is enabled, it is using .net 3.5.

In a module there is
Public FormMain as frmMain = frmMain
Public AppTest as boolean = false

Public AppTest as boolean = false never gets executed but the FormMain does (as per a breakpoint).

AppTest = true in frmMain crashes with the indicated error.

This works in 6-7 other programs and has for years but crashes with indicated error

There are no errors when we do a build.


need just 1 with FormMain as a reference to frmMain.

This technique worked in many other programs but not this one. Not sure why.


In a working program that uses the same technique.

I hit F8 to step thru.


1. It goes into some CheckChanged events on frmMain so I assume the form is instantiated.


2. It then goes into the module with Public FormMain as frmMain = frmMain where it completes the definitions ok (does the last).

3. Then it goes to frmMain's MyBase.load
4. Program runs fine


In the failing program I hit F8

1. It immediately goes to the module with Public FormMain as frmMain = frmMain
(frmMain probably NOT instantiated)

2. The module definitions do not complete (last statement executed is the FormMain).

3. It goes to other modules for defintitions

4. It now goes to the ..Changed events on frmMain (on working programs this was first).

5. It then goes to the frmMain MyBase.load



So the failing program is taking a different startup path and I do not know why.

What would cause the modules to be executed first before frmMain is instantiated?????


Help!!!!
 
i will look this up for you, but try catch the error like so, just to see if it explaines more:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
'Your code here to catch error!
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Hopefully it'll explaine a bit more
 
We use On Error GoTo ErrX

The error was not captured by the on error when the exception and inner exception was already posted.

Cannot put any code in the declarations section where the Public FormMain as frmMain = frmMain fails.
 
ok the initiated "frmMain" would be called first but by the looks it's accessing [varibles and functions] in the "Modules" this is why when you press"F8" it steps through the [varibles and functions] as their being accessed from the "frmMain", in the case of the "Modules" the "Public FormMain as frmMain = frmMain" is declared in the "Modules" so they can have access to varibles and functions on the "frmMain" too, When you "F8" break through code it will show you whats being initiated in the order of the "Calls\Functions and Varibles" till you get the part of the code where the error is caused, up on reading the lower segment of you post, it does sound as if some code it trying to run before "frmMain" is initiated, try see why the module needs "Public FormMain as frmMain = frmMain" and see what it leads to, why it's initiating an for what reason.
 
How

I hit F8 to start and it goes to Module Base2 where Public FormMain as frmMain = frmMain is (doesn't complete)
I Step Out and it goes to Another Module (aaaBase) where FormMain was but I recently moved it.
I step out of aaaBase and it goes to txtPattern.textChanged which is part of frmMain.
Another Step out and it goes to frmMain's MyBase.load

I just don't know how to determine anything.

How do I find out what triggered it?
 
ok i know you have Stepped through your code, have you tried Placing brekpoints too, see it's InvalidOperationException was unhandled means that you are referencing something that hasn't been loaded. Such as setting a check box or in this case the form hasn't loaded yet? if it's the "AppTest = True" thats giving the exception then NULL it like so 'AppTest = true or try moving it about in the Code Block where it is, Prefebly lower some where just a though?.
 
Yes, I put break points. No I cannot move the code around. It is just too much.

It fails on Public FormMain as frmMain = frmMain as I have stated. I do not know why it goes there before instantiating frmMain.

Don't forget this FormMain worked in about 6 other vb.net programs. Just this one fails.
 
the way in vb.net to Get form values from a moduel is normally

Public frm As New frmMain
OR
in this case
Public frm As frmMain

So its called Publically, the = FrmMain on the end i dont quite understand in vb.net, due to the way it's all be upgraded to VB.NET 2010 maybe the "= frmMain" was just junk from the VB6 upgraded, try remove the = frmMain, Temporary and run a test
 
Workaround found.

We took out the = frmMain from the module and moved it to frmMain itself. That seemed to solve that part. We are now debugging other VB6 conversion issues.

Not sure why the other programs worked.
 
Back
Top