Namespace conflicts with page class - name resolution difference in compiler

S

Shadow Lynx

If this is not the most appropriate group for this question, please let
me know.

I noticed an odd priority difference in resolving names in VS 2005 (VWD
Express) vs. the .NET 2.0 compiler (the one accessed from within VS).

Let me give an example to clarify things.

In a test solution, I created two web form pages with codebehinds and a
class in the App_Code folder:
App_Code\Simple.vb
Default.aspx (and *.vb)
Level2.aspx (and *.vb)

App_Code\Simple.vb:
Namespace Level1.Level2
Public Class Simple
Public ReadOnly Property Message() As String
Get
Return "Hello"
End Get
End Property
End Class
End Namespace

You may have noticed the Level1.Level2 Namespace...
Level2.aspx and *.vb contain default content - the .vb starts with
Class Level2

Default.aspx.vb: (the .aspx file contains default content)
Imports System.Data
Imports System.Data.SqlClient
Imports Level1
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
--> Dim TestInstance As New Level2.Simple
Response.Write(TestInstance.Message)
End Sub
End Class

When writing the code, there is no error flag on the line with the
arrow. VS recognizes Level2.Simple properly (or maybe it's not proper)
because of the Imports Level1 line.

If I attempt to build the site, however, I get the error (due to the
line with the arrow):
C:\Inetpub\wwwroot\ASPX2_Test\Default.aspx.vb(9,0): error BC30002: Type
'Level2.Simple' is not defined.

Of course, the following replacement line makes the build process
happy:
Dim TestInstance As New Level1.Level2.Simple

Which one is in error - the VS inline error checker or the build
process itself? While I prefer the VS priority, the Build priority may
be correct because the page Level2.aspx.vb contains a Level2 class
which is actually in the same namespace (none) as the Default.aspx.vb
page.
 
C

Christopher Reed

Your namespace is Level1.Level2, so you need:

Imports Level1.Level2

Then you'll have

Dim TestInstance As New Simple

Keep in mind that when using the Website model in VWD Express that if you
leave off the namespaces in all files including the class files in the
App_Code directory, then a default namespace is created for you and you
won't need to import a namespace for your Simple class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top