You may receive a "System.TypeLoadException" error message when you use forward reference in Visual C++ .NET 2002 or in Visual C++ .NET 2003 (871182)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

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:
  1. Locate the following code in the MyProject.cpp file:
    typedef struct _t_Struct *myStruct;
  2. Add the following code after the code that you located in step 1:
    struct _t_Struct
    {
    };

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.
  3. Under Project Types, click Visual C++ Projects.
  4. 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.
  5. In the Name box, type MyProject, and then click OK.
  6. In Solution Explorer, right-click MyProject.cpp, and then click Open.
  7. Locate the following code in Code view of the MyProject.cpp file:
    using namespace System;
  8. 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;
    }
  9. Replace the existing code in the _tmain procedure with the following code:
    myStruct testVar1, testVar2;
    testVar2=testFunc(testVar1);
    Console::WriteLine(S"Hello World");
    return 0;
  10. On the File menu, click Save All.
  11. On the Build menu, click Build Solution.
  12. On the Debug menu, click Start to run the application.

    You may receive the error message that is mentioned in the "Symptoms" section.

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

REFERENCES

For additional information, visit the following Microsoft Developer Network (MSDN) Web sites:

TypeLoadException class/clr (common language runtime compilation)

Modification Type:MinorLast Reviewed:1/6/2006
Keywords:kberrmsg kbcode kbtshoot kbprb KB871182 kbAudDeveloper