Namespace conflicts with page class - name resolution difference in compiler

Discussion in 'ASP .Net' started by Shadow Lynx, Feb 3, 2006.

  1. Shadow Lynx

    Shadow Lynx Guest

    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.
    Shadow Lynx, Feb 3, 2006
    #1
    1. Advertising

  2. 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.
    --
    Christopher A. Reed
    "The oxen are slow, but the earth is patient."

    "Shadow Lynx" <> wrote in message
    news:...
    > 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.
    >
    Christopher Reed, Feb 4, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew FPGA
    Replies:
    0
    Views:
    944
    Andrew FPGA
    Sep 26, 2005
  2. Replies:
    11
    Views:
    433
    Steven Bethard
    Nov 16, 2005
  3. Microsoft Newsserver

    c# namespace conflicts

    Microsoft Newsserver, Jan 16, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    486
    Alvin Bruney [ASP.NET MVP]
    Jan 17, 2008
  4. rocky
    Replies:
    2
    Views:
    308
    rocky
    Jul 7, 2009
  5. Immortal Nephi
    Replies:
    3
    Views:
    1,335
    Gennaro Prota
    Jul 10, 2010
Loading...

Share This Page