Microsoft KB Archive/871182

= You may receive a &quot;System.TypeLoadException&quot; error message when you use forward reference in Visual C++ .NET 2002 or in Visual C++ .NET 2003 =

Article ID: 871182

Article Last Modified on 4/4/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





SYMPTOMS
When you use a forward reference to a user-defined type that does not have the body defined in your code, you may receive the following error message.

An unhandled exception of type 'System.TypeLoadException' occurred in Unknown Module.

This problem occurs when the body is referenced instead of defined. You compile the code by using the common language runtime compilation compiler option (/clr).



RESOLUTION
To resolve this problem, you must define the body for the user-defined type. To define the body for the user-defined type, follow these steps:   Locate the following code in the MyProject.cpp file: typedef struct _t_Struct *myStruct;   Add the following code after the code that you located in step 1: struct _t_Struct { }; 



Steps to reproduce the problem
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project.

The New Project dialog box appears. Under Project Types, click Visual C++ Projects.</li> If you are using Visual Studio .NET 2002, click Managed C++ Application under Templates.

If you are using Visual Studio .NET 2003, click Console Application (.NET) under Templates.</li> In the Name box, type MyProject, and then click OK.</li> In Solution Explorer, right-click MyProject.cpp, and then click Open.</li>  Locate the following code in Code view of the MyProject.cpp file:

using namespace System; </li>  Add the following code after the code that you located in step 7:

using namespace System; typedef struct _t_Struct *myStruct;

myStruct testFunc(myStruct testVar) { //This is a test function. return testVar; } </li>  Replace the existing code in the _tmain procedure with the following code:

myStruct testVar1, testVar2; testVar2=testFunc(testVar1); Console::WriteLine(S&quot;Hello World&quot;); return 0; </li> On the File menu, click Save All.</li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start to run the application.

You may receive the error message that is mentioned in the &quot;Symptoms&quot; section.

Note This is only one of the scenarios where you may receive the &quot;System.TypeLoadException&quot; error message. There may be several other scenarios that may lead to the same error that is mentioned in the &quot;Symptoms&quot; section.

</li></ol>

<div class="references_section">