locked
can't access shared member of a class in a visual studio 'web site' RRS feed

  • Question

  • User2087812504 posted

    Hi,

    I have a simple web site created within visual studio 2005, (not a web application project).  I have two pages, one which contains a shared function in the code-behind, and the other which calls this shared function.  However, when I try to instantiate the class containing the shared member, I get a 'Name <classname> is not declared' error.

    Page 1 code behind:

    Partial Class Page1
        Inherits System.Web.UI.Page
    
        Public Shared Function SomeFunction() As String
            'Do Something
        End Function
    End Class
    Page 2 code-behind:
    Partial Class Page2
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.Write(Page1.SomeFunction())
        End Function
    End Class
     
    Why does this not work?  I know that if I had created this in a web application project rather than a 'visual studio web site' then this would work.  I would rather not have to switch to a web application project as the site is only very small, and a WAP would be overkill.  Any help appreciated
     
    Wednesday, November 5, 2008 10:15 AM

Answers

  • User-158764254 posted

    the new (non wap) web compilation model allows individual pages to be compiled seperately from each other into their own unique assemblies.  so you can actually end up with a different dll per page.  in order to access one page class from another, you need to get references between these dynamically generated assemblies.  that is why you would place shared code in the app_code folder.  the app_code folder is also compiled into an assembly and that one assembly is automatically referenced by all other assemblies (page classes etc..).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 10, 2008 9:52 PM

All replies

  • User68971474 posted

    There may be scope issues with your arrangement.

    You could create a new class file, e.g. Utilities.vb, and move (cut and paste) SomeFunction from Page1 to Utilities.vb.

    Then, reference the function as Utilities.SomeFunction (assuming Utilities.vb contains a class named Utilities).

    HTH

    Wednesday, November 5, 2008 12:32 PM
  • User2087812504 posted

    Hi, thanks for your reply.

    I first created the Utilities class file in the root of the web site and it wouldn't work.  However, when I saved it into the App_Code folder (because VS prompts you to do this when creating a new class file) it works!  So does this mean that if I want common shared functions across my site they all have to be in the one location of App_Code?  I find this quite strange because page code-behind files are also classes just the same yet they don't work.  It seems for code to 'consumed' across a site it has to be in App_Code!

    Wednesday, November 5, 2008 12:50 PM
  • User68971474 posted

    Microsoft does recommend using App_Code as a code library folder.  That is the giste of what I have read in my past research.

    Again, the issue here is scope.  I know project builds look to App_Code as a central location for public shared classes. 

    My experience has been similar to yours (wrong scope) when I move public classes to other folders.

    With Page1 & Page2 in the same folder, you could try Imports Page1 at the very top of Page2.aspx.vb.

    Also, you could use Namespace...End Namespace to group the classes together that way.

    HTH

    Wednesday, November 5, 2008 1:07 PM
  • User2087812504 posted

    Hi, sorry for the late reply.

    Using Imports Page1 did not work.  I messed around with using a namespace to wrap all my UI pages as I realised that Web Application Projects uses a root namespace in the project pages.  However, when I tried setting a namespace in my pages I lost all my designer generated code, ie references to server controls in code behind.  In WAPs there is a code-gen file where all this info is stored, but in Websites this is hidden somewhere but I don't know where!

    I am assuming that Microsoft has created the functionality of Website projects this way to support the separation of application layers.  Looks like App_Code should be used as a simple store for UI-based functions and utilities, but if you need to go further then creating a class library project and referencing it is the way to go.

    Monday, November 10, 2008 4:51 AM
  • User-158764254 posted

    the new (non wap) web compilation model allows individual pages to be compiled seperately from each other into their own unique assemblies.  so you can actually end up with a different dll per page.  in order to access one page class from another, you need to get references between these dynamically generated assemblies.  that is why you would place shared code in the app_code folder.  the app_code folder is also compiled into an assembly and that one assembly is automatically referenced by all other assemblies (page classes etc..).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 10, 2008 9:52 PM
  • User2087812504 posted

    I see, it's all starting to make sense now.  I should have realised, I remember seeing lots of separate dll files with dynamically generated names in the bin folder, and because the page isn't referencing one site-wide dll file, that's where the problem is.

    Thanks for your help.

    Tuesday, November 11, 2008 4:38 AM