Cry about...
.NET / VB.Net Troubleshooting
'NNNN' is not declared (VB.NET)
Symptom:
When compiling a VB.Net application the compiler generates the following error:
error BC30451: 'NNNN' is not declared. It may be inaccessible due to its protection level.
Visual Studio 2010 simply gives the error:
'NNNN' is not declared. It may be inaccessible due to its protection level.
or with older versions of Visual Studio (i.e. up to and including Visual Studio 2008):
Name 'NNNN' is not declared.
where 'NNNN
' is the name of a variable.
If you are using C# then the error message is slightly different (but means the same thing):
The name 'NNNN' does not exist in the current context
in this case please refer to the C# version of this article "The name NNNN does not exist in the current context".
Possible Cause 1: (see also Possible Cause 2 below)
The compiler does not recognise the variable 'NNNN
'. This
is either because the variable name is misspelt or because the compiler
could not find a corresponding definition.
Remedy:
- A common cause is a simple misspelling where the identifier is used. For example:
dim number as Integer numbr = 1
Here the identifier is defined as '
number
' but used in the code as 'numbr
'. The solution is to correct the spelling.
- If the name is referring to an identifier then it may be that the reference simply needs to be qualified. For example:
Imports System.Web.HttpContext Module Example Function ExampleProc() as String return Application("name") . . [Error] Name 'Application' is not declared.Try replacing "
Application
" with "Current.Application
".
- The most common cause is that the namespace that defines the name
is missing. Identify and import the required namespace - the table below
should help.
For example, with the error:
Name 'Directory' is not declared.
The missing namespace is System.IO, so the solution is to add:
Imports System.IO
at the top of the file.
The following table (which is not exhaustive) lists identifiers together with the namespace and any qualification that might typically be required:
Name Namespace Common Qualified Name Abs Math Math.Abs AnchorStyles System.Windows.Forms Application System.Windows.Forms
For Windows forms applications.System.Web.HttpContext
For web applications.Current.Application ApplicationDeployment System.Deployment.Application Assembly System.Reflection BindingFlags System.Reflection CultureInfo System.Globalization Cache System.Web.Caching CipherMode System.Security.Cryptography ConfigurationManager System.Configuration
Also ensure project contains reference to System.Configuration.dllCurrent System.Web.HttpContext Debug System.Diagnostics Debugger System.Diagnostics Dns System.Net Directory System.IO Encoding System.Text EventLog System.Diagnostics File System.IO FormsAuthentication System.Web.Security HostingEnvironment System.Web.Hosting HtmlEncode System.Web.HttpUtility HttpContext System.Web
Also ensure project contains reference to System.Web.HttpRuntime System.Web HttpStatusCode System.Net HttpUtility System.Web ImageFormat System.Drawing.Imaging Marshal System.Runtime.InteropServices NormalizationForm System.Text PaddingMode System.Security.Cryptography Parallel System.Threading.Tasks Path System.IO Process System.Diagnostics Regex System.Text.RegularExpressions Request System.Web.HttpContext Current.Request Round Math Math.Round SecurityElement System.Security SecurityMode System.ServiceModel SecurityZone System.Security Server System.Web.HttpContext Current.Server ServicePointManager System.Net SmptMail System.Web.Mail SslPolicyErrors System.Net.Security Stopwatch System.Diagnostics ThreadPool System.Threading UnicodeCategory System.Globolization UrlDecode HttpContext.Current.Server.UrlDecode User HttpContext.Current.User UTF8Encoding System.Text VirtualPathUtility System.Web It is my intention to add to this table over time.
Possible Cause 2:
A less likely cause is that the project itself lacks a reference to the necessary library. This can arise when (for example) creating a class library and using web namespaces, for example:
Name 'HttpContext' is not declared
or
Name 'HttpRuntime' is not declared
even though the line "imports System.Web" is included.
Remedy
Add the reference to the project:
- Right click the project in the solution explorer and select "properties".
- On the "References" tab ensure that the required namespace is listed under "References" (do not confuse this with "Imported namespaces", the important thing is that it must be listed under "References".)
The necessary references are listed in the table above (shown under "Possible Cause 1"). For example the solution to "HttpContext (or HttpRuntime) is not declared" even though "Imports System.Web" is included in the file is to ensure that "System.Web" is listed as one of the project References.
These notes are believed to be correct for VB.NET for .NET 4, .NET 3, .NET 2 and .NET 1.1 frameworks, and may apply to other versions as well.
For the corresponding C# version of this article please see "The name NNNN does not exist in the current context".
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.