Cry about...
.Net / VB.Net Troubleshooting
Type '<component-name>' is not defined
Symptom:
When compiling a VB.Net application the compiler generates the following error:
Type 'CCCC' is not defined.'
If you are using C# then the error is slightly different:
The type or namespace name 'CCCC' could not be found (are you missing a using directive or an assembly reference?)
in both cases 'CCCC
' is the name of a
component.
The definition of the component exists in the project and in the code behind the component is being created dynamically such as:
Dim aControl As MyComponent = LoadControl("MyComponent.ascx")
or simply referenced such as:
Private _mine As New List(Of MyComponent)
either of these giving rise to the error:
Type 'MyComponent' is not defined.'
To further complicate things whilst this error may be observed when building the project in some cases the project can be built without error with the error only manifesting itself when the project is published.
If you get this error when it is not related to a component defined in your project then see my notes for "Type <type-name> is not defined (or which namespace do I need for type X?)".
Whilst these notes are primarily aimed at VB.Net they are also applicable to C#.
Cause:
The compiler cannot find the component of type 'CCCC
'.
(Why it might sometimes be able to find the component when building but not when publishing is a mystery to me!)
Remedy:
- If the component is being dynamically created in the code behind
for a form or for another component then add an explicit reference
to the control in the form's (or control's) definition.
Do this by adding a "
<% Register ...
" directive at the top of the page, for example:<%@ Register src="MyComponent.ascx" tagname="MyComponent" tagprefix="uc11" %>
This example is correct assuming the component is called "
MyComponent
" and the source code for it is in the file "MyComponent.ascx
"An even easier way of doing this is to drop the component onto the form (or component) and then delete the instance off the form. This will leave behind the necessary register directive.
These notes are believed to be correct for VB.Net for Visual Studio 2008 and Visual Studio 2010 and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.